微信公衆號「後端進階」,專一後端技術分享:Java、Golang、WEB框架、分佈式中間件、服務治理等等。
老司機傾囊相授,帶你一路進階,來不及解釋了快上車!java
SpringBoot自動化配置源碼分析從源碼的角度講解了 SpringBoot 自動化配置的原理,知道了它最終要乾的事情不過是讀取 META-INF/spring.factories 中的自動化配置類而已。git
SpringBoot 項目就是由一個一個 Starter 組成的,一個 Starter 表明該項目的 SpringBoot 起步依賴,除了官方已有的 Starter,若是你須要將本身的項目支持 SpringBoot,那麼就須要把它製做成一個 Starter。這篇博客依據 SpringBoot 的自動化配置原理,開發一個屬於本身的 Starter。github
自動化配置需知足兩個條件:spring
在這裏建立一個 spring-boot-starter-helloworld 項目做爲例子,實現以上兩點。後端
引入 SpringBoot 自動化配置依賴:springboot
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.5.9.RELEASE</version>
</dependency>
</dependencies>
複製代碼
spring-boot-starter-helloworld 只是做爲例子演示自定義 starter 的過程,實現的功能很簡單就是建立一個 HelloworldService 的,並配置 sayHello() 方法打印的語句。微信
public class HelloworldService {
private String words;
private String getWords() {
return words;
}
public void setWords(String words) {
this.words = words;
}
public String sayHello() {
return "hello, " + words;
}
}
複製代碼
建立屬性類,prefix = "helloworld"表明該項目在屬性文件中配置的前綴,便可以在屬性文件中經過 helloworld.words=springboot,就能夠改變屬性類字段 words 的值了。app
@ConfigurationProperties(prefix = "helloworld")
public class HelloworldProperties {
public static final String DEFAULT_WORDS = "world";
private String words = DEFAULT_WORDS;
public String getWords() {
return words;
}
public void setWords(String words) {
this.words = words;
}
}
複製代碼
建立自動化配置類,這個至關於就是一個普通的 Java 配置類,能夠在這裏建立 Bean,並可得到與 application.properties 屬性文件相對應的屬性類的 Bean。框架
// 至關於一個普通的 java 配置類
@Configuration
// 當 HelloworldService 在類路徑的條件下
@ConditionalOnClass({HelloworldService.class})
// 將 application.properties 的相關的屬性字段與該類一一對應,並生成 Bean
@EnableConfigurationProperties(HelloworldProperties.class)
public class HelloworldAutoConfiguration {
// 注入屬性類
@Autowired
private HelloworldProperties hellowordProperties;
@Bean
// 當容器沒有這個 Bean 的時候才建立這個 Bean
@ConditionalOnMissingBean(HelloworldService.class)
public HelloworldService helloworldService() {
HelloworldService helloworldService = new HelloworldService();
helloworldService.setWords(hellowordProperties.getWords());
return helloworldService;
}
}
複製代碼
在 META-INF 目錄下建立 spring.factories,這個屬性文件可重要啦,由於 SpringBoot 自動化配置最終就是要掃描 META-INF/spring.factories 來加載項目的自動化配置類。分佈式
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.objcoding.starters.helloworld.HelloworldAutoConfiguration
複製代碼
爲了引入 starter,我在這裏再建立一個 spring-boot-starter-helloworld-sample 項目。
添加 spring-boot-starter-helloworld 起步依賴:
<dependency>
<groupId>com.objcoding</groupId>
<artifactId>spring-boot-starter-helloworld</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
複製代碼
在 application.properties 中添加屬性:
helloworld.words=springboot
複製代碼
在 SpringBoot 主程序中 注入 helloworldService
@RestController
@SpringBootApplication
public class HelloworldApplication {
@Autowired
private HelloworldService helloworldService;
@RequestMapping("/")
public String sayHello() {
return helloworldService.sayHello();
}
public static void main(String[] args) {
SpringApplication.run(HelloworldApplication.class, args);
}
}
複製代碼
訪問 http://localhost:8080/,打印如下結果:
要源碼的同窗點擊這裏獲取: github.com/objcoding/s…