微信公众号「后端进阶」,专一后端技术分享:Java、Golang、WEB框架、分布式中间件、服务治理等等。
老司机倾囊相授,带你一路进阶,来不及解释了快上车!java
SpringBoot自动化配置源码分析从源码的角度讲解了 SpringBoot 自动化配置的原理,知道了它最终要干的事情不过是读取 META-INF/spring.factories 中的自动化配置类而已。git
SpringBoot 项目就是由一个一个 Starter 组成的,一个 Starter 表明该项目的 SpringBoot 起步依赖,除了官方已有的 Starter,若是你须要将本身的项目支持 SpringBoot,那么就须要把它制做成一个 Starter。这篇博客依据 SpringBoot 的自动化配置原理,开发一个属于本身的 Starter。github
自动化配置需知足两个条件:spring
在这里建立一个 spring-boot-starter-helloworld 项目做为例子,实现以上两点。后端
引入 SpringBoot 自动化配置依赖:springboot
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>1.5.9.RELEASE</version> </dependency> </dependencies>
spring-boot-starter-helloworld 只是做为例子演示自定义 starter 的过程,实现的功能很简单就是建立一个 HelloworldService 的,并配置 sayHello() 方法打印的语句。微信
public class HelloworldService { private String words; private String getWords() { return words; } public void setWords(String words) { this.words = words; } public String sayHello() { return "hello, " + words; } }
建立属性类,prefix = "helloworld"表明该项目在属性文件中配置的前缀,便可以在属性文件中经过 helloworld.words=springboot,就能够改变属性类字段 words 的值了。app
@ConfigurationProperties(prefix = "helloworld") public class HelloworldProperties { public static final String DEFAULT_WORDS = "world"; private String words = DEFAULT_WORDS; public String getWords() { return words; } public void setWords(String words) { this.words = words; } }
建立自动化配置类,这个至关于就是一个普通的 Java 配置类,能够在这里建立 Bean,并可得到与 application.properties 属性文件相对应的属性类的 Bean。框架
// 至关于一个普通的 java 配置类 @Configuration // 当 HelloworldService 在类路径的条件下 @ConditionalOnClass({HelloworldService.class}) // 将 application.properties 的相关的属性字段与该类一一对应,并生成 Bean @EnableConfigurationProperties(HelloworldProperties.class) public class HelloworldAutoConfiguration { // 注入属性类 @Autowired private HelloworldProperties hellowordProperties; @Bean // 当容器没有这个 Bean 的时候才建立这个 Bean @ConditionalOnMissingBean(HelloworldService.class) public HelloworldService helloworldService() { HelloworldService helloworldService = new HelloworldService(); helloworldService.setWords(hellowordProperties.getWords()); return helloworldService; } }
在 META-INF 目录下建立 spring.factories,这个属性文件可重要啦,由于 SpringBoot 自动化配置最终就是要扫描 META-INF/spring.factories 来加载项目的自动化配置类。分布式
# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.objcoding.starters.helloworld.HelloworldAutoConfiguration
为了引入 starter,我在这里再建立一个 spring-boot-starter-helloworld-sample 项目。
添加 spring-boot-starter-helloworld 起步依赖:
<dependency> <groupId>com.objcoding</groupId> <artifactId>spring-boot-starter-helloworld</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
在 application.properties 中添加属性:
helloworld.words=springboot
在 SpringBoot 主程序中 注入 helloworldService
@RestController @SpringBootApplication public class HelloworldApplication { @Autowired private HelloworldService helloworldService; @RequestMapping("/") public String sayHello() { return helloworldService.sayHello(); } public static void main(String[] args) { SpringApplication.run(HelloworldApplication.class, args); } }
访问 http://localhost:8080/,打印如下结果:
要源码的同窗点击这里获取: https://github.com/objcoding/spring-boot-starter-tutorial