文章目錄
書接上回,《Spring Boot 揭秘與實戰 源碼分析 - 工作原理剖析》。為了更好的理解 Spring Boot 的 自動配置和工作原理,我們自己來實現一個簡單的自動配置模塊。
假設,現在項目需要一個功能,需要自動記錄項目發布者的相關信息,我們如何通過 Spring Boot 的自動配置,更好的實現功能呢?
實戰的開端 – Maven搭建
先創建一個Maven項目,我來手動配置下 POM 文件。
- <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>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>1.3.3.RELEASE</version>
- </parent>
- <groupId>com.lianggzone.demo</groupId>
- <artifactId>springboot-action-autoconfig</artifactId>
- <version>0.1</version>
- <packaging>jar</packaging>
- <name>springboot-action-autoconfig</name>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-autoconfigure</artifactId>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- </project>
參數的配置 - 屬性參數類
首先,我們定義一個自定義前綴,叫做 custom 吧。之前說到,這里的配置參數,可以通過 application.properties 中直接設置。那么,我們創建一個作者的字段,設置默認值為 LiangGzone。
- @ConfigurationProperties(prefix = "custom")
- public class AuthorProperties {
- public static final String DEFAULT_AUTHOR = "LiangGzone";
- public String author = DEFAULT_AUTHOR;
- public String getAuthor() {
- return author;
- }
- public void setAuthor(String author) {
- this.author = author;
- }
- }
那么,聰明的你,應該想到了,我們在 application.properties 中配置的時候,就要這樣配置了。
- #custom
- custom.author = 梁桂釗
真的很簡單 - 簡單的服務類
- public class AuthorServer {
- public String author;
- public String getAuthor() {
- return author;
- }
- public void setAuthor(String author) {
- this.author = author;
- }
- }
你沒有看錯,真的是太簡單了,沒有高大上的復雜業務。它的主要用途就是賦值。
自動配置的核心 - 自動配置類
@ConditionalOnClass,參數中對應的類在 classpath 目錄下存在時,才會去解析對應的配置類。因此,我們需要配置 AuthorServer 。
@EnableConfigurationProperties, 用來加載配置參數,所以它應該就是屬性參數類 AuthorProperties。
- @Configuration
- @ConditionalOnClass({ AuthorServer.class })
- @EnableConfigurationProperties(AuthorProperties.class)
- public class AuthorAutoConfiguration {
- @Resource
- private AuthorProperties authorProperties;
- @Bean
- @ConditionalOnMissingBean(AuthorServer.class)
- @ConditionalOnProperty(name = "custom.author.enabled", matchIfMissing = true)
- public AuthorServer authorResolver() {
- AuthorServer authorServer = new AuthorServer();
- authorServer.setAuthor(authorProperties.getAuthor());
- return authorServer;
- }
- }
authorResolver方法的作用,即 AuthorProperties 的參數賦值到AuthorServer 中。
spring.factories 不要遺漏
我們需要實現自定義自動裝配,就需要自定義 spring.factories 參數。所以,我們需要在 src/main/resources/ META-INF/spring.factories 中配置信息,值得注意的是,這個文件要自己創建。
- # CUSTOM
- org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
- com.lianggzone.springboot.autoconfig.author.AuthorAutoConfiguration
功能打包與配置依賴
好了,我們已經實現了一個簡單的自動配置功能。那么,我們需要將這個項目打成 jar 包部署在我們的本地或者私服上。然后,就可以用了。
我們在另外一個項目中,配置 Maven 依賴。
- <dependency>
- <groupId>com.lianggzone.demo</groupId>
- <artifactId>springboot-action-autoconfig</artifactId>
- <version>0.1</version>
- </dependency>
測試,測試
- @RestController
- @EnableAutoConfiguration
- public class AuthorAutoConfigDemo {
- @Resource
- private AuthorServer authorServer;
- @RequestMapping("/custom/author")
- String home() {
- return "發布者:"+ authorServer.getAuthor();
- }
- }
運行起來,我們看下打印的發布者信息是什么?
我們在 application.properties 中配置一個信息。
- #custom
- custom.author = 梁桂釗
運行起來,我們看下打印的發布者信息是什么?
源代碼
相關示例完整代碼: springboot-action
(完)
