前言
在之前的博文(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也注入了。
最後就是驗證是否符合業務邏輯