Springboot
中的自动配置确实方便,减小了咱们开发上的复杂性,那么自动配置原理是什么呢?以前我也写过了一篇文章进行了分析。
Springboot 系列(三)Spring Boot 自动配置。php
因为自动配置用到了配置文件的绑定,若是你还不知道常见的配置文件的用法,能够参考这篇文章。
Springboot 系列(二)Spring Boot 配置文件。java
在这一次,经过学习 Springboot
自动配置模式,编写一个本身的 starter
,用来加深对自动配置的理解。git
熟悉模式,有助于提高编写的 starter
的规范性,编写本身的 starter
以前先来学习 Springboot
官方 starter
以及常见框架的整合 starter
的编写方式 ,能够领略到其中的奥秘。github
选择一个官方的自动配置进行分析,这里就选择常见的配置端口号配置。web
使用端口号以前咱们须要先引入 web 依赖。面试
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
复制代码
若是你观察 starter
多的话,也许你发已经发现了一个模式,Springboot
官方的 starter
的名字都是 spring-boot-starter-xxxx
命名的。spring
查看 spring-boot-starter-web
会发现,其实这个依赖只是一个空盒子,除了依赖其余 pom
以外,没有一行代码。数据库
这时,发现了另一个模式:starter
只依赖其余 pom
,不作代码实现。apache
那么 spring-boot-starter-web
到底依赖了哪些内容?springboot
观察这个依赖信息,而后再参照其余的官方 starter
,能够找到几个固定的引入,能够被称之为模式的依赖引入。
spring-boot-starter
。spring-boot-autoconfigure
。引入依赖只有配置端口号,像这样。
server.port=8090
复制代码
IDEA 中能够经过点击 server.port
找到这个配置绑定的类文件。能够看到配置最终会注入到类ServerProperties
类的 port
属性上。
那么这个 ServerProperties
究竟是哪里使用的呢?继续查找,找到一个和 Servlet
的有关的调用。
发现是被 ServletWebServerFactoryCustomizer
类进行了调用,这个类里面定义了
private final ServerProperties serverProperties;
复制代码
用来使用配置的属性。
继续查看这个类的调用,发现只有一个类使用这个类,这个类是ServletWebServerFactoryAutoConfiguration
。
根据咱们对注解的理解,这个类就是自动配置主要类了。同时自动配置类都是以 AutoConfiguration
结尾。
看这个类的几个注解的意思。
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
复制代码
ServletRequest
类存在和是 Web 应用时生效。@ConditionalOnClass(ServletRequest.class)
@ConditionalOnWebApplication(type = Type.SERVLET)
复制代码
ServerProperties
的配置绑定。@EnableConfigurationProperties(ServerProperties.class)
复制代码
@Import({ ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class,
ServletWebServerFactoryConfiguration.EmbeddedTomcat.class,
ServletWebServerFactoryConfiguration.EmbeddedJetty.class,
ServletWebServerFactoryConfiguration.EmbeddedUndertow.class })
复制代码
同时注入配置到 Bean 工厂以供其余地方调用。
@Bean
public ServletWebServerFactoryCustomizer servletWebServerFactoryCustomizer(ServerProperties serverProperties) {
return new ServletWebServerFactoryCustomizer(serverProperties);
}
复制代码
自动配置仅仅是这些东西吗?根据以前文章里的分析,咱们知道不止代码,至少还有一个指定自动配置类的配置文件须要读取。也就是 spring.factories
文件。
若是你不知道,能够先看这篇文章。Springboot 系列(三)Spring Boot 自动配置 。
事实确实如此,能够在 spring.factories
中找到上面跟踪到的类。 也就是 ServletWebServerFactoryAutoConfiguration
.
根据上面的分析,能够发现 Springboot
官方 starter
的几个模式。
XXXProperties
自动绑定 XXX
开头的配置信息,如:ServerProperties
。XXXProperties
定义到要使用的类中,如:ServletWebServerFactoryCustomizer
。XXXAutoConfiguration
,开启 XXXProperties
的自动配置,限定生效场景,建立须要的类到 Bean
工厂。如:ServletWebServerFactoryAutoConfiguration
。Springboot
官方若是把全部的框架都编写成 starter
,是不现实的。所以不少第三方框架须要主动集成到 springboot
,因此咱们选择一个经常使用的框架分析它的 starter
实现。由于已经看过了 springboot
官方 starter
是如何配置的, 第三方框架也是相似,因此在下面观察的过程当中会直接指出相同点,而再也不作对比详细对比。
这里选择 mybatis-spring-boot-starter
进行学习分析。
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
复制代码
这里 mybatis
框架的 starter
依赖符合必定的规则,即 xxx-spring-boot-starter.
观察这个 starter
,发现它也没有作任何的代码实现,这一点和 springboot
官方一致。
查看 mybatis-spring-boot-starter
的依赖项,有不少,其中和自动配置有关的主要是。
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-autoconfigure</artifactId>
</dependency>
复制代码
查看 mybatis-spring-boot-autoconfigure
的内容发现和 springboot
官方的 autoconfigure
结构上是差很少的。
mybatis
的自动配置也是经过 spring.factories
来指明自动配置,而后经过 XxxAutoConfiguration
绑定 XxxProperties
来进行自动配置.
在原理上,和上面 springboot
官方的 starter
是相同的,因此不作过多的介绍了。
说了那么多,终于到了实操环节,经过上面的介绍,咱们能够大体得出编写本身的 starter
步骤。
xxx-spring-boot-starter
的启动器项目。xxx-spring-boot-autoconfigure
的项目。
xxxProperties
.xxxProperties
.XXXAutoConfiguration
注入配置。spring.factories
文件,用于指定要自动配置的类。xxx-spring-boot-autoconfigure
等其余依赖。starter
,配置须要配置的信息。因为启动器不须要代码实现,只须要依赖其余项目,因此直接建立一个空的 maven 项目。可是名字要规范。
这里建立的 starter
是 myapp-spring-boot-starter
。
pom 文件很是简单,只须要引入接下来要建立的 myapp-spring-boot-autoconfigure
.
<?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>
<groupId>net.codingme.starter</groupId>
<artifactId>myapp-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 启动器 -->
<dependencies>
<!-- 引入自动配置项目 -->
<dependency>
<groupId>net.codingme.starter</groupId>
<artifactId>myapp-spring-boot-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
复制代码
结合上面对 starter
的分析,直接建立一个名字为 myapp-spring-boot-autoconfigure
的项目。项目中只引入 springboot
父项目以及 spring-boot-starter
。
<?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.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>net.codingme.starter</groupId>
<artifactId>myapp-spring-boot-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>myapp-spring-boot-autoconfigure</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</artifactId>
</dependency>
</dependencies>
</project>
复制代码
项目的整体结构看图。
在 HelloProperties
中经过注解 @ConfigurationProperties(prefix = "myapp.hello")
让类中的属性与 myapp.hello
开头的配置进行绑定。
/** * <p> * * @Author niujinpeng * @Date 2019/10/29 23:51 */
@ConfigurationProperties(prefix = "myapp.hello")
public class HelloProperties {
private String suffix;
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
复制代码
而后在 HelloService
中的 sayHello
方法使用 HelloProperties
中自动绑定的值。
public class HelloService {
HelloProperties helloProperties;
public String sayHello(String name) {
return "Hello " + name + "," + helloProperties.getSuffix();
}
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
}
复制代码
为了让 HelloService
能够自动注入且能正常使用 HelloProperties
,因此咱们在
HelloServiceAutoConfiguration
类中把 HelloProperties.class
引入,而后把 HelloService
注入到 Bean
。
/** * web应用才生效 */
@ConditionalOnWebApplication
/** * 让属性文件生效 */
@EnableConfigurationProperties(HelloProperties.class)
/*** * 声明是一个配置类 */
@Configuration
public class HelloServiceAutoConfiguration {
@Autowired
private HelloProperties helloProperties;
@Bean
public HelloService helloService() {
HelloService helloService = new HelloService();
helloService.setHelloProperties(helloProperties);
return helloService;
}
}
复制代码
最后在 spring.factories
中只须要指定要自动配置的类便可。
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
net.codingme.starter.HelloServiceAutoConfiguration
复制代码
到这里,自动配置项目就完成了。能够在 myapp-spring-boot-autoconfigure
项目执行 mvn install
把自动配置项目打包到本地仓库,而后使用相同的命令把 myapp-spring-boot-starter
安装到仓库。由于后者依赖于前者项目,因此这里前者须要先进 mvn install
。
建立一个 springboot
项目myapp-spring-boot-starter-test
。
引入 web
依赖,引入本身编写的 myapp-spring-boot-starter
.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入本身的 starter -->
<dependency>
<groupId>net.codingme.starter</groupId>
<artifactId>myapp-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
复制代码
编写一个 HelloController
注入自动配置里的 HelloService
用于测试。
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String sayHello(String name) {
return helloService.sayHello(name);
}
}
复制代码
因为 autoConfigure
项目中定义了 sayHello
方法会输出“Hello”+传入的 name + 配置的 hello.suffix
,因此咱们在 springboot
配置文件中配置这个属性。
myapp.hello.suffix=早上好
复制代码
运行测试项目,访问 /hello 路径传入一个 name 看看自动配置有没有生效。
从测试结果能够看到自动配置的早上好已经生效了。到这里本身编写的 starter
也已经完工。
项目已经传到 Github.
github.com/niumoo/spri…
Springboot 系列(一)Spring Boot 入门
Springboot 系列(二)Spring Boot 配置文件
Springboot 系列(三)Spring Boot 自动配置
Springboot 系列(四)Spring Boot 日志框架
Springboot 系列(五)Spring Boot web 开发之静态资源和模版引擎
Springboot 系列(六)Spring Boot web 开发之拦截器和三大组件
Springboot 系列(七)web 开发之异常错误处理机制剖析
Springboot 系列(八)动态Banner与图片转字符图案的手动实现
Springboot 系列(九)使用 Spring JDBC 和 Druid 数据源监控
Springboot 系列(十)使用 Spring data jpa 访问数据库
Springboot 系列(十一)使用 Mybatis 访问数据库
Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件
Springboot 系列(十三)使用邮件服务
Springboot 系列(十四)迅速启用 HTTPS 加密你的网站
Springboot 系列(十五)如何编写本身的 Springboot starter
<完>
我的网站:www.codingme.net
若是你喜欢这篇文章,能够关注公众号,一块儿成长。 关注公众号回复资源能够没有套路的获取全网最火的的 Java 核心知识整理&面试核心资料