Springboot的出現極大的簡化了開發人員的配置,而這之中的一大利器即是springboot的starter,starter是springboot的核心組成部分,springboot官方同時也爲開發人員封裝了各類各樣方便好用的starter模塊,例如:java
starter的出現極大的幫助開發者們從繁瑣的框架配置中解放出來,從而更專一於業務代碼,而springboot能作的不只僅停留於此,當面對一些特殊的狀況時,咱們可使用咱們自定義的springboot starter。web
在建立咱們自定義的starter以前呢,咱們先看看官方是怎麼說的:spring
模塊編程
在springboot官方文檔中,特別提到,咱們須要建立兩個module ,其中一個是autoconfigure module 一個是 starter module ,其中 starter module 依賴 autoconfigure modulejson
可是,網上仍然有不少blog在說這塊的時候其實會發現他們其實只用了一個module,這固然並無錯,這點官方也有說明:瀏覽器
You may combine the auto-configuration code and the dependency management in a single module if you do not need to separate those two concerns
//若是不須要將自動配置代碼和依賴項管理分離開來,則能夠將它們組合到一個模塊中。
複製代碼
命名規範緩存
springboot 官方建議springboot官方推出的starter 以spring-boot-starter-xxx的格式來命名,第三方開發者自定義的starter則以xxxx-spring-boot-starter的規則來命名,事實上,不少開發者在自定義starter的時候每每會忽略這個東西(由於不看官方文檔很難知道這件事情。同時也不會形成其餘的後果,主要是顯得不夠專業)。springboot
瞭解了這兩點以後,那麼下面讓咱們一塊去探索spingboot starter的奧祕吧。app
按照springboot官方給的思路,starter的核心module應該是autoconfigure,因此咱們直接去看spring-boot-autoconfigure裏面的內容。框架
當Spring Boot啓動時,它會在類路徑中查找名爲spring.factories的文件。該文件位於META-INF目錄中。打開spring.factories文件,文件內容太多了,爲了不我水篇幅,咱們只看其中的一部分:
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
複製代碼
咱們能夠發現一些比較眼熟的單詞,好比Aop,Rabbit,Cache ,當springboot啓動的時候,將會嘗試加載這些配置類,若是該路徑下存在該類的話,則將運行它,並初始化與該配置類相關的bean。
點開一個看看:
@Configuration
@ConditionalOnClass({RabbitTemplate.class, Channel.class})
@EnableConfigurationProperties({RabbitProperties.class})
@Import({RabbitAnnotationDrivenConfiguration.class})
public class RabbitAutoConfiguration {
//...代碼略..
}
複製代碼
咱們先來了解一下這幾個註解:
@ConditionalOnClass :條件註解,當classpath下發現該類的狀況下進行自動配置。
@EnableConfigurationProperties:外部化配置
@Import :引入其餘的配置類
固然,這並非一種通用的套路,查看其餘的配置類,咱們會發現其標註的註解每每也是有所區別的。
首先咱們新建一個maven項目,引入如下依賴:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<!-- 咱們是基於Springboot的應用 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
複製代碼
而後咱們建立一個person類,用做後期咱們測試的bean
public class Person {
//屬性
private int age;
private String name;
private String gender;
/*此處省略getter and setter and toStering*/
}
複製代碼
而後咱們也建立一個PersonConfigProperties來完成咱們屬性的注入
@ConfigurationProperties(prefix = "mystarter.config.student")
public class PersonConfigProperties {
private String name;
private int age;
private String gender;
/* 其餘的配置信息。。。。 */
/*此處省略getter and setter and toStering*/
}
複製代碼
最後建立咱們的自動配置類MyStarterAutoConfiguration.java
@Configuration
@EnableConfigurationProperties(PersonConfigProperties.class)
@ConditionalOnClass(Person.class)
public class MyStarterAutoConfiguration {
@Bean
@ConditionalOnProperty(prefix = "mystarter.config", name = "enable", havingValue = "true")
public Person defaultStudent(PersonConfigProperties personConfigProperties) {
Person person = new Person();
person.setAge(personConfigProperties.getAge());
person.setName(personConfigProperties.getName());
person.setGender(personConfigProperties.getGender());
return person;
}
}
複製代碼
我感受這是否是作好了?
我不要你以爲,我要我以爲
最後咱們最重要的一步:
在resourecs文件目錄下建立META-INF,並建立咱們本身的spring.factories,並把咱們的 MyStarterAutoConfiguration添加進去
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.jdkcb.mystarter.config.MyStarterAutoConfiguration
複製代碼
最後打包成jar包,在咱們新的項目裏面測試:
引入咱們的starter,固然也能夠在本地直接引入咱們的my-spring-boot-starter項目
<dependency>
<groupId>com.jdkcb</groupId>
<artifactId>my-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/my-spring-boot-starter-0.0.1-SNAPSHOT.jar</systemPath>
</dependency>
複製代碼
在application.properties配置文件中添加咱們相應的配置
mystarter.config.enable=true
mystarter.config.person.name=小明
mystarter.config.person.age=5
mystarter.config.person.gender=男
複製代碼
新建一個測試的Controller:
@RestController
public class TestController {
@Autowired
private Person person;
@RequestMapping("/getPerson")
private Person getStudent() {
return person;
}
}
複製代碼
啓動項目,在瀏覽器地址欄輸入 http://127.0.0.1:8080/getPerson ,結果以下
{"age":5,"name":"小明","gender":"男"}
複製代碼
大功告成~
最後的最後,你們好,我是韓數,哼,關注我,有你好果子吃(叉腰)。
記得點個贊再走哦~