Spring Boot 揭秘與實戰 自己實現一個簡單的自動配置模塊


文章目錄

  1. 1. 實戰的開端 – Maven搭建
  2. 2. 參數的配置 - 屬性參數類
  3. 3. 真的很簡單 - 簡單的服務類
  4. 4. 自動配置的核心 - 自動配置類
  5. 5. spring.factories 不要遺漏
  6. 6. 功能打包與配置依賴
  7. 7. 測試,測試
  8. 8. 源代碼

書接上回,《Spring Boot 揭秘與實戰 源碼分析 - 工作原理剖析》。為了更好的理解 Spring Boot 的 自動配置和工作原理,我們自己來實現一個簡單的自動配置模塊。

假設,現在項目需要一個功能,需要自動記錄項目發布者的相關信息,我們如何通過 Spring Boot 的自動配置,更好的實現功能呢?

實戰的開端 – Maven搭建

先創建一個Maven項目,我來手動配置下 POM 文件。

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <parent>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-starter-parent</artifactId>
  7. <version>1.3.3.RELEASE</version>
  8. </parent>
  9. <groupId>com.lianggzone.demo</groupId>
  10. <artifactId>springboot-action-autoconfig</artifactId>
  11. <version>0.1</version>
  12. <packaging>jar</packaging>
  13. <name>springboot-action-autoconfig</name>
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-autoconfigure</artifactId>
  18. </dependency>
  19. </dependencies>
  20. <build>
  21. <plugins>
  22. <plugin>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-maven-plugin</artifactId>
  25. </plugin>
  26. </plugins>
  27. </build>
  28. </project>

參數的配置 - 屬性參數類

首先,我們定義一個自定義前綴,叫做 custom 吧。之前說到,這里的配置參數,可以通過 application.properties 中直接設置。那么,我們創建一個作者的字段,設置默認值為 LiangGzone。

  1. @ConfigurationProperties(prefix = "custom")
  2. public class AuthorProperties {
  3. public static final String DEFAULT_AUTHOR = "LiangGzone";
  4. public String author = DEFAULT_AUTHOR;
  5. public String getAuthor() {
  6. return author;
  7. }
  8. public void setAuthor(String author) {
  9. this.author = author;
  10. }
  11. }

那么,聰明的你,應該想到了,我們在 application.properties 中配置的時候,就要這樣配置了。

  1. #custom
  2. custom.author = 梁桂釗

真的很簡單 - 簡單的服務類

  1. public class AuthorServer {
  2. public String author;
  3. public String getAuthor() {
  4. return author;
  5. }
  6. public void setAuthor(String author) {
  7. this.author = author;
  8. }
  9. }

你沒有看錯,真的是太簡單了,沒有高大上的復雜業務。它的主要用途就是賦值。

自動配置的核心 - 自動配置類

@ConditionalOnClass,參數中對應的類在 classpath 目錄下存在時,才會去解析對應的配置類。因此,我們需要配置 AuthorServer 。

@EnableConfigurationProperties, 用來加載配置參數,所以它應該就是屬性參數類 AuthorProperties。

  1. @Configuration
  2. @ConditionalOnClass({ AuthorServer.class })
  3. @EnableConfigurationProperties(AuthorProperties.class)
  4. public class AuthorAutoConfiguration {
  5. @Resource
  6. private AuthorProperties authorProperties;
  7. @Bean
  8. @ConditionalOnMissingBean(AuthorServer.class)
  9. @ConditionalOnProperty(name = "custom.author.enabled", matchIfMissing = true)
  10. public AuthorServer authorResolver() {
  11. AuthorServer authorServer = new AuthorServer();
  12. authorServer.setAuthor(authorProperties.getAuthor());
  13. return authorServer;
  14. }
  15. }

authorResolver方法的作用,即 AuthorProperties 的參數賦值到AuthorServer 中。

spring.factories 不要遺漏

我們需要實現自定義自動裝配,就需要自定義 spring.factories 參數。所以,我們需要在 src/main/resources/ META-INF/spring.factories 中配置信息,值得注意的是,這個文件要自己創建。

  1. # CUSTOM
  2. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  3. com.lianggzone.springboot.autoconfig.author.AuthorAutoConfiguration

功能打包與配置依賴

好了,我們已經實現了一個簡單的自動配置功能。那么,我們需要將這個項目打成 jar 包部署在我們的本地或者私服上。然后,就可以用了。

我們在另外一個項目中,配置 Maven 依賴。

  1. <dependency>
  2. <groupId>com.lianggzone.demo</groupId>
  3. <artifactId>springboot-action-autoconfig</artifactId>
  4. <version>0.1</version>
  5. </dependency>

測試,測試

  1. @RestController
  2. @EnableAutoConfiguration
  3. public class AuthorAutoConfigDemo {
  4.  
  5. @Resource
  6. private AuthorServer authorServer;
  7.  
  8. @RequestMapping("/custom/author")
  9. String home() {
  10. return "發布者:"+ authorServer.getAuthor();
  11. }
  12. }

運行起來,我們看下打印的發布者信息是什么?

我們在 application.properties 中配置一個信息。

  1. #custom
  2. custom.author = 梁桂釗

運行起來,我們看下打印的發布者信息是什么?

源代碼

相關示例完整代碼: springboot-action

(完)

 

微信公眾號


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM