Spring Boot
在我们使用IDEA新建一个Spring Boot项目时会发现自动生成的pom.xml,今天深入剖析一下它的源码:
pom.xml
pom.xml的代码:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.chen</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
发现它的主体有 <parent> </parent>
(父依赖)和许多的 spring-boot-starter-xxx
(场景启动器),而父依赖本质也是一个启动器,点进父依赖的 spring-boot-starter-parent
源码看看。
- spring-boot-starter-parent
源码主体由配置的资源过滤和管理插件组成:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
<!-- 资源过滤:Spring Boot推荐使用.yml配置 -->
<includes>
<include>**/application*.yml</include>
<include>**/application*.yaml</include>
<include>**/application*.properties</include>
</includes>
<!-- 插件管理 -->
<pluginManagement>
...
...
...
</pluginManagement>
发现 spring-boot-starter-parent
的源码里面还有一个重要的依赖管理 spring-boot-dependencies
,我们再点开 spring-boot-dependencies
的源码看看。
- spring-boot-dependencies
源码里面是各种常用的依赖,所以Spring Boot可以自动帮我们配置依赖,解决了版本不兼容问题:
<properties>
...
...
</properties>
<dependency>
...
...
</dependency>
主启动类
在我们使用IDEA新建一个Spring Boot项目时会发现自动生成的主启动类,简单几行代码,却有很强大的作用,今天深入剖析一下它的源码。
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootWeb01Application.class, args);
}
}
剖析主启动类就是剖析这两个:
- 主类构造器 SpringApplication
- 注解 @SpringBootApplication
@SpringBootApplication
的源码里面这四个元注解我们不做剖析
@Target:用于描述注解的使用范围(即:被描述的注解可以用在什么地方)
@Retention:表示需要在什么级别保存该注释信息 , 用于描述注解的生命周期(SOURCE < CLASS < RUNTIME)
@Document:说明该注解将被包含在javadoc中
@Inherited:说明子类可以继承父类中的该注解
了解源码还需要了解一个知识点:JavaConfig
传统的配置文件:(applicationContext.xml)
<bean id="" class="" />
<import re />
<context: component-scan />
再Spring4以后推荐使用JavaConfig编写配置文件:
@Configuration
@Import(OtherConfig.class) //引入其他配置
@ComponentScan("com.chen.service") //扫描包
class MyConfig{
@Bean //注册Bean (id = "user", class = User)
public User user(){
return new user();
}
}
上面两个等价,所以在Spring Boot 中没有配置文件!
思维导图剖析:
//下篇再见…谢谢