思维导图:
一.引言
我对Spring Boot的理解就是利用约定大于配置的原则以达到简化开发的目的。最后的结果就是当我们通过Spring Boot框架使用Spring的时候,可以很简单的向Spring 中添加例如Mybatis,Spring MVC等等其他所需要的组件。
本文主要分为三个大的部分:
- 如何创建一个Spring Boot项目
- Spring Boot 约定大于配置的原理是什么
- 如何创建自定义的Starter配置
二.创建Spring Boot项目
2.1利用IDEA创建Spring Boot项目
通过IDEA,可以经过如下步骤创建一个集成了Spring MVC及Tomcat的Spring项目。
Web中已经含有web项目中所需要的框架,例如Tomcat和Spring MVC
2.2通过POM文件创建Spring Boot项目
通过IDEA创建的POM文件内容如下,可以直接在POM文件中添加一下内容以创建Spirng Boot 项目
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--需要测试时,如果packaging是pom,则需要改为jar或者war,指的是项目的打包方式-->
<packaging>war</packaging>
<!--通过此Parent,标识这是一个Spring Boot项目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>
<!--自己项目的名称信息-->
<groupId>com.zhouhao.study</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>
<!--自动配置了web相关的框架,包括Spring MVC和Tomcat-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--自动配置了测试所需的框架,包括了JUnit-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
三.Spring Boot的原理
3.1配置方式
我们接下来通过源码了解Spring Boot是如何把其他的框架自动配置入Spring的。
- 这是我们Spring Boot项目的启动类。可以看到,其核心注解就是@SpringBootApplication。
/**
* Spring Boot 启动类
*
* @author zhouhao
* @date 2019/03/26
*/
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
- 再看看这个注解的原理是什么,这个注解分为三个功能部分。
-
@SpringBootConfiguration : 其本质就是@Configuration注解。
-
@ComponentScan : 组件扫描
-
@EnableAutoConfiguration : 开启自动配置,我觉得这就是Spring Boot的核心注解了。
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {...}
- 进入@EnableAutoConfiguration注解,可以发现,有两个功能性注解
-
@AutoConfigurationPackage :这个注解指定了组件扫描的起始位置是此SpringbootApplication类所在的包及其以下的包。所以,如果想要@Component在Spring Boot项目中生效,使用了@Component的类必须和启动类的包的等级低或同级。
-
@Import({AutoConfigurationImportSelector.class}) : 这就引入了一个配置类,他会扫描符合条件的配置类即使用了@Configuration的配置类。
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration{...}
- 进入AutoConfigurationImportSelector可以发现,需要自动配置的配置类都放在了一个叫spring.factories的文件中,文件中有如下部分,
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
...还有很多,不写完
- 随意进入一个配置类,可以看到,这个自动配置类是否使用是由一些Spirng的条件创建bean所决定的。就像这些Condition的名称所表示的一样,这个配置类会在特定的条件下启用。
@Configuration
@ConditionalOnClass({EnableAspectJAutoProxy.class, Aspect.class, Advice.class, AnnotatedElement.class})
@ConditionalOnProperty(
prefix = "spring.aop",
name = {"auto"},
havingValue = "true",
matchIfMissing = true
)
public class AopAutoConfiguration{...}
- 综上所述 Spring Boot的启动类其实就是三个核心注解,@Configuration@ComponentScan,@EnableAutoConfiguration,其中@EnableAutoConfiguration的作用就是声明spring.factories文件中的配置类
3.2 执行流程
当然,3.1节中的配置只是起了声明作用。启用类中的main方法才是Spirng Boot项目的执行起点。其大致就分为四个步骤:
- 收集配置信息
- 初始化Environment
- 初始化ApplicationContext
- refresh ApplicationContext
四.自定义Starter
通过POM文件可以看到,我们是通过名为<artifactId>spring-boot-starter-web</artifactId>的一个Sstarter启动器开启自动配置的。所以我们也可以自定义一个starter启动器以自动配置我们所需要的组件。目标很简单,获取配置文件的一个参数并输出的网页上。代码如下所示:
4.1参数对象类
/**
* 参数对象类
* 使用@ConfigurationProperties注解以定义一个参数对象,并指定参数文件application.properties中的前缀
*
* @author : zhouhao
* @date : Created in 2019/4/1 7:49
*/
@ConfigurationProperties(prefix = "hello")
public class HelloPropertyties {
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
4.2 简单服务类
/**
* 服务类,可以获取msg信息
*
* @author : zhouhao
* @date : Created in 2019/4/1 7:57
*/
public class HelloService {
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
4.3配置类
/**
* 配置类
* 使用@EnableConfigurationProperties以启用参数类
* 使用@ConditionalOnClass 表明当HelloService.class在classpath下时启用此配置类
*
* @author : zhouhao
* @date : Created in 2019/4/1 7:59
*/
@Configuration
@EnableConfigurationProperties(HelloPropertyties.class)
@ConditionalOnClass(HelloService.class)
public class HelloServiceAutoConfiguration {
@Autowired
HelloPropertyties helloPropertyties;
@Bean
public HelloService helloService(){
HelloService helloService = new HelloService();
helloService.setMsg(helloPropertyties.getMsg());
return helloService;
}
}
4.4 配置声明
在spring.fatories中添加自动自动配置声明
#声明自定义的starter的配置类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.zhouhao.study.hello.HelloServiceAutoConfiguration
由此,一个自定义的starter就完成了。只需要利用maven工具install到本地的仓库中,然后在需要使用的项目里添加依赖即可。
4.5使用自定义starter
4.5.1添加依赖
<!--自定义启动器的依赖-->
<dependency>
<groupId>com.zhouhao.study</groupId>
<artifactId>hello-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
4.5.2 在application.properties中添加所需参数
#参数的获取定义在我们自定义的starter中
hello.msg=springstartertest
4.5.3 输出到网页
/**
* Spring Boot 启动类
*
* @author zhouhao
* @date 2019/03/26
*/
@SpringBootApplication
@RestController
public class SpringbootApplication {
@Autowired
HelloService helloService;
@RequestMapping("/")
public String index(){
return helloService.getMsg();
}
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}