前言
在之前的博文(Spring Boot自動配置原理與實踐(一))中,已經介紹了Spring boot的自動配置的相關原理與概念,本篇主要是對自動配置的實踐,即自定義Starter,對原理與概念加深理解。
本篇是我在實際工作中配置的用於弱口令檢查的Starter,能方便嵌入到用戶模塊中的相關密碼接口或方法,對弱口令進行檢查並反饋,當然由於是公司內部代碼,部分代碼省略。
一、Starter實踐
1、配置Maven依賴
Spring Boot自動化配置主要依賴如下兩個包:
- spring-boot-starter:打包starter主要依賴
- configuration-processor:自動化配置主要依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
2、創建實體類映射配置信息
眾所周知,SpringBoot Starter最厲害的就是可以通過最簡單的properties/yaml文件配置,達到最終目的。配置文件需要通過解析生成對應的實體類
@ConfigurationProperties(prefix = "weak.password") public class CheckWeakPasswordProperties { private Boolean enabled = true; /** * 需要檢查的URI數組 */ private String[] checkUri; /** * 攔截檢查的方式 1-interceptor 2-filter 3-aop */ private Integer checkType = 1; private String ip = "127.0.0.1"; private String port = "8501"; /** * 客戶端名稱 */ private String clientName = "cloud-user"; /** * 校驗失敗信息提示 */ private String failureMessage = "密碼等級不夠";
...// 省略getter/setter方法
其中prefix = "weak.password",標明配置文件以“weak.password”開頭的字段(對應實體類中的字段)都是需要解析的。在配置文件中輸入前綴后,會進行提示說明
3、定義配置類
這一步非常關鍵,是自動裝配的核心,通過配置文件配置靈活的參數產生相關的Bean,完成一系列初始化操作,關鍵的幾個注解在這里就不解釋了,具體可以看Spring Boot自動配置原理與實踐(一)。
@Configuration @EnableConfigurationProperties(CheckWeakPasswordProperties.class) @ConditionalOnProperty(prefix = "weak.password", name = "enabled", havingValue = "true") public class CheckWeakPasswordAutoConfiguration { public CheckWeakPasswordAutoConfiguration() { } @Bean @ConditionalOnProperty(prefix = "weak.password", name = "checkType", havingValue = "2") public CheckPasswordInterceptor checkPasswordInterceptor(){ return new CheckPasswordInterceptor(); } @Bean @ConditionalOnProperty(prefix = "weak.password", name = "checkType", havingValue = "2") public CheckPasswordFilter checkPasswordFilter(){ return new CheckPasswordFilter(); } @Bean @ConditionalOnProperty(name = "weak.password.check-type", havingValue = "2") public CheckPasswordFilterConfig checkPasswordFilterConfig() { return new CheckPasswordFilterConfig(); } @Bean @ConditionalOnProperty(prefix = "weak.password", name = "checkType", havingValue = "1") public CheckPasswordInterceptorConfig checkPasswordInterceptorConfig(){ return new CheckPasswordInterceptorConfig(); } }
4、創建spring.factories文件
之前三步所有的操作都已經完成,那么將Starter當引入工程中是如何發現並自動裝配的,這就需要spring.factory文件中標明,在resource/META-INF在新建spring.factory文件
在該文件中指明AutoConfiguration的全Class路徑
這樣打包的時候就能將spring.factory文件打包,項目啟動的時候就會掃描並裝配
同時生成spring-configuration-metadata.json文件,其內容就是提供配置文件智能化提示的
{ "groups": [ { "name": "weak.password", "type": "com.yunchuang.password.properties.CheckWeakPasswordProperties", "sourceType": "com.yunchuang.password.properties.CheckWeakPasswordProperties" } ], "properties": [ { "name": "weak.password.check-type", "type": "java.lang.Integer", "description": "攔截檢查的方式 1-interceptor 2-filter 3-aop", "sourceType": "com.yunchuang.password.properties.CheckWeakPasswordProperties", "defaultValue": 1 }, { "name": "weak.password.check-uri", "type": "java.lang.String[]", "description": "需要檢查的URI數組", "sourceType": "com.yunchuang.password.properties.CheckWeakPasswordProperties" }, { "name": "weak.password.client-name", "type": "java.lang.String", "description": "客戶端名稱", "sourceType": "com.yunchuang.password.properties.CheckWeakPasswordProperties", "defaultValue": "cloud-user" }, { "name": "weak.password.enabled", "type": "java.lang.Boolean", "sourceType": "com.yunchuang.password.properties.CheckWeakPasswordProperties", "defaultValue": true }, { "name": "weak.password.failure-message", "type": "java.lang.String", "description": "校驗失敗信息提示", "sourceType": "com.yunchuang.password.properties.CheckWeakPasswordProperties", "defaultValue": "密碼等級不夠" }, { "name": "weak.password.ip", "type": "java.lang.String", "sourceType": "com.yunchuang.password.properties.CheckWeakPasswordProperties", "defaultValue": "127.0.0.1" }, { "name": "weak.password.port", "type": "java.lang.String", "sourceType": "com.yunchuang.password.properties.CheckWeakPasswordProperties", "defaultValue": "8501" } ], "hints": [] }
二、自定義Starter使用
首先引入自定義的Starter包依賴到相關應用中
然后在配置文件中打開開關,或者某些條件才能開啟自動配置,以我的代碼示例舉例的話,就是需要指定enabled為true
其次可以觀察啟動的時候相關的Bean是否被自動裝配,可以打開debug模式查看日志,或者在idea中查看Endpoints-->Beans-->application,可以看到相關的自動配置啟動時加載了,並且相應的Bean也注入了。
最后就是驗證是否符合業務邏輯