SpringBoot核心原理---自動配置 之建立自己的starter pom maven依賴包
上一篇:SpringBoot 的執行原理之自動配置,瞭解SpringBoot自動配置的原理,在此基礎上動手做一個自己的自動配置依賴包。
一、準備工作
先建立Maven工程:
目錄結構:
二、編碼
MistraService.java
這個類就是要自動配置的Bean,條件就設為是否存在這個類的Bean,沒有的話就建立。
package com.mistra.mistrastarter;
/**
* Author: RoronoaZoro丶WangRui
* Time: 2018/6/25/025
* Describe: 是否進行自動配置的判斷依據類,根據此類是否存在來決定是否建立這個類的Bean
*/
public class MistraService {
private String name;
public String sayYourName(){
return "I'm " + name + "! ";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
MistraServiceProperties .java
為MistraService提供配置引數值得類。name設有預設值,配置檔案配置字首是”mistra”—>”mistra.name”,當沒有在配置檔案配置時就取預設值。
package com.mistra.mistrastarter;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Author: RoronoaZoro丶WangRui
* Time: 2018/6/25/025
* Describe: 自動配置的型別安全的注入引數類
* 通過application.properties配置mistra.name的值來設定引數,若不設定,預設為"RoronoaZoro丶小王瑞"
*/
@ConfigurationProperties(prefix = "mistra" )
public class MistraServiceProperties {
private static final String NAME = "RoronoaZoro丶小王瑞";
private String name = NAME;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
MistraServiceAutoConfiguration.java
自動配置類。
package com.mistra.mistrastarter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Author: RoronoaZoro丶WangRui
* Time: 2018/6/25/025
* Describe: 自動配置類
* 根據條件判斷是否要自動配置,建立Bean
*/
@Configuration
@EnableConfigurationProperties(MistraServiceProperties.class)
@ConditionalOnClass(MistraService.class)//判斷MistraService這個類在類路徑中是否存在
@ConditionalOnProperty(prefix = "mistra",value = "enabled",matchIfMissing = true)
public class MistraServiceAutoConfiguration {
@Autowired
private MistraServiceProperties mistraServiceProperties;
@Bean(name = "mistraService")
@ConditionalOnMissingBean(MistraService.class)//當容器中沒有這個Bean時(MistraService)就自動配置這個Bean,Bean的引數來自於MistraServiceProperties
public MistraService mistraService(){
MistraService mistraService = new MistraService();
mistraService.setName(mistraServiceProperties.getName());
return mistraService;
}
}
根據MistraServiceProperties提供的引數,判斷MistraService這個類在類路徑中是否存在,並且當容器中沒有這個Bean(MistraService )時就自動配置建立這個Bean。
spring.factories
註冊自動配置類。注意位置:src.main.resources.META-INF.spring.factories
三、maven打包
然後打包,並註冊到本地maven倉庫:
CMD 切換到專案根目錄下:mvn install
然後在本地倉庫就可以看見依賴包了:
四、引用
在別的專案引入依賴:
<dependency>
<groupId>com.mistra</groupId>
<artifactId>mistrastarter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
重新整理Maven後可以在依賴裡面找到自定義的包:
SpringBoot啟動類TestApplication.java
@SpringBootApplication(scanBasePackages = "com.spring.boot.test")
public class TestApplication {
public static void main(String[] args) {
System.out.println("☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆ RoronoaZoro丶小王瑞 ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆");
SpringApplication.run(TestApplication.class, args);
}
在配置檔案配置debug:true,可以看到自動配置的情況,可以找到自己寫的配置類:
單元測試類StarterPomTest.java
package com.spring.boot.test.starter;
import com.mistra.mistrastarter.MistraService;
import com.spring.boot.test.TestApplication;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Author: RoronoaZoro丶WangRui
* Time: 2018/6/26/026
* Describe: 自定義starter測試
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = TestApplication.class)
public class StarterPomTest {
@Autowired
private MistraService mistraService;
@Test
public void say() {
System.out.println(mistraService.sayYourName());
}
}
在配置檔案中加入引數:
mistra:
name: 羅羅諾亞索隆
如果不配置,就是取類中定義的預設值。
執行say():
至此,自定義的starter測試就完成了,也是對SpringBoot的核心自動配置的一次理解。
Demo原始碼GitHub