栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot源码解读一【框架引用】

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot源码解读一【框架引用】

SpringBoot源码解读

SpringBoot已经成为众多企业开发Java项目的必备框架,即使是微服务框架SpirngCloud也基于SpringBoot。对SpringBoot的研究可以进一步提高我们的专业技能。本人利用业余时间研究下SpringBoot底层实现。代码这东西看的时候能懂,但看完容易忘。因此,做个笔记,也分享给有需要的同学


文章目录
  • SpringBoot源码解读
  • 准备
  • 一、项目构建
    • 构建方式
    • 代码
    • 运行
  • 二、分析
    • 1. spring-boot-starter-web不需要指定版本
      • dependencyManagement应用场景
    • 2.如果当前项目已经有父项目,如何引入SpringBoot
  • 总结


准备

版本信息

  • JDK 1.8
  • SpringBoot 2.6.7 (不同版本底层有些差异)
  • 开发工具 IDEA

一、项目构建 构建方式

方法一:生成maven项目、添加springboot依赖

方法二:IDEA插件 Spring Assistant,通过Spring助手生成SpringBoot项目

代码

项目信息

  • groupId(组织标识):com.walker
  • artifactId(项目标识):spring-boot-study
  • version(版本):0.0.1-SNAPSHOT

pom.xml

	

    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.7
         
    
    com.walker
    spring-boot-study
    0.0.1-SNAPSHOT
    spring-boot-study
    spring-boot-study
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    




SpringBootStudyApplication

package com.walker.springboot.study;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootStudyApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootStudyApplication.class, args);
    }

}

编写Controller:HelloController

package com.walker.springboot.study;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@Slf4j
public class HelloController {


    @GetMapping("/")
    String index() {
        return "Hello Spring boot!";
    }

}

运行

执行SpringBootStudyApplication的main函数即可

访问地址:127.0.0.1:8080/

二、分析 1. spring-boot-starter-web不需要指定版本

我们可以看到引入的时候没有写version标签


    org.springframework.boot
    spring-boot-starter-web

点开spring-boot-starter-parent 的poml文件,内容如下

  • 有一个父项目spring-boot-dependencies
  • resources标签下定义了一些资源文件
  • pluginManagement 定义了一些插件信息 包括spring-boot-maven-plugin

在这里面没有发现版本的定义信息,打开dependencies的pom文件,发现里面定义了很多包的版本信息,且依赖放在dependencyManagement标签里面,内容太多就不贴了

打开maven官网
依赖介绍
对dependency management的定义如下:

The dependency management section is a mechanism for centralizing dependency information. When you have a set of projects that inherit from a common parent, it’s possible to put all information about the dependency in the common POM and have simpler references to the artifacts in the child POMs.

简单来讲就是:集中管理依赖信息,抽取公共依赖,简化子项目的依赖配置。

dependencyManagement应用场景

当我们项目中不同模块依赖相同jar包时,把共同依赖定义在父项目的dependencyManagement标签中,这样在进行依赖包的版本管理时,只需要修改一个地方。 简化了依赖包的版本管理

2.如果当前项目已经有父项目,如何引入SpringBoot

引入spring-boot-dependencies


        
            org.springframework.boot
            spring-boot-starter-web
        
    
    
        
            
                org.springframework.boot
                spring-boot-dependencies
                2.6.7
                pom
                import
            
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    
  • type 是必须要有的。默认是:jar,可以不填写
  • scope:import 可以导入托管的依赖。简单来讲,就是把dependencies管理的依赖合并到当前项目中
总结

本文主要描述了SpringBoot框架的使用,下一篇将正式进入源码的讲解。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/866554.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号