SpringBoot自動(dòng)配置的實(shí)現(xiàn)原理
一、運(yùn)行原理
Spring Boot的運(yùn)行是由注解@EnableAutoConfiguration提供的。
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({EnableAutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() default {};
String[] excludeName() default {};
}
這里的關(guān)鍵功能是@Import注解。EnableAutoConfigurationImportSelector使用SpringFactoriesLoader.loadFactoryNames方法來掃描具有MEAT-INF/spring.factories文件的jar包(1.5版本以前使用EnableAutoConfigurationImportSelector類,1.5以后這個(gè)類廢棄了使用的是AutoConfigurationImportSelector類),下面是spring-boot-autoconfigure-1.5.4.RELEASE.jar下的MEAT-INF中的spring.factories文件的部分內(nèi)容。
# Initializers org.springframework.context.ApplicationContextInitializer=\ org.springframework.boot.autoconfigure.SharedMetadataReaderFactoryContextInitializer,\ org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer # Application Listeners org.springframework.context.ApplicationListener=\ org.springframework.boot.autoconfigure.BackgroundPreinitializer # Auto Configuration Import Listeners org.springframework.boot.autoconfigure.AutoConfigurationImportListener=\ org.springframework.boot.autoconfigure.condition.ConditionEvaluationReportAutoConfigurationImportListener # Auto Configuration Import Filters org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\ org.springframework.boot.autoconfigure.condition.OnClassCondition # 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,\ org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\ org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,\ org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
里面的類都是自動(dòng)配置類,SpringBoot會(huì)根據(jù)這些自動(dòng)配置類去自動(dòng)配置環(huán)境。
下面我們就自動(dòng)動(dòng)手寫一個(gè)starter。
二、自定義Starter
首先先介紹幾個(gè)條件注解。
- @ConditionalOnBean:當(dāng)容器里有指定的Bean為true
- @ConditionalOnClass:當(dāng)類路徑下有指定的類為true
- @ConditionalOnMissingBean:當(dāng)容器里沒有指定的Bean為true
- @ConditionalOnProperty:指定的數(shù)據(jù)是否有指定的值
- ...
了解了條件注解后,我們開始自定義Starter。
1、在自定義Starter之前先要在Maven中填寫依賴。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.miaolovezhen</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-starter-test</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.5.4.RELEASE</version>
</dependency>
</dependencies>
</project>
2、完成TestProperties類,這個(gè)類定義了默認(rèn)的屬性值,如該類中,只有一個(gè)屬性值msg,默認(rèn)為world。@ConfigurationProperties注解會(huì)定義一個(gè)匹配,如果想修改屬性值,可以在application.properties中使用“匹配.屬性=修改的值”進(jìn)行修改。如test.msg = tan
@ConfigurationProperties(prefix = "test")
public class TestProperties {
private static final String MSG = "springboot";
private String msg = MSG;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
3、完成服務(wù)類。服務(wù)類是指主要的功能類,如果沒有SpringBoot,這些服務(wù)類在Spring中都是需要自己去配置生成的。如SpringMVC中的DispatcherServlet、Mybatis的DataSource等。
public class TestService {
private String msg;
public String sayHello(){
return "Hello " + msg;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
4、完成自動(dòng)配置類。自動(dòng)配置類主要作用是SpringBoot的配置核心,它會(huì)寫在MEAT-INF/spring.factories中,告知SpringBoot在啟動(dòng)時(shí)去讀取該類并根據(jù)該類的規(guī)則進(jìn)行配置。
- @EnableConfigurationProperties注解根據(jù)TestProperties類開啟屬性注入,允許在application.properties修改里面的屬性值。
- @ConditionOnClass會(huì)檢測(cè)是否存在TestService類
- @ConditionOnProperty類會(huì)查看是否開啟該自動(dòng)配置。默認(rèn)開啟(true)。
- @ConditionOnMissingBean會(huì)檢測(cè)容器中是否有TestService類的對(duì)象,如果沒有則生成一個(gè)。
@Configuration
@EnableConfigurationProperties(TestProperties.class)
@ConditionalOnClass(TestService.class)
@ConditionalOnProperty(prefix = "test" , value = "enabled" , matchIfMissing = true)
public class TestServiceAutoConfiguration {
@Autowired
TestProperties testProperties;
@Bean
@ConditionalOnMissingBean(TestService.class)
public TestService testService(){
TestService testService = new TestService();
testService.setMsg(testProperties.getMsg());
return testService;
}
}
5、最后一步,不要忘記在在MEAT-INF文件夾中創(chuàng)建spring.factories文件。內(nèi)容很簡(jiǎn)單,告訴SpringBoot去讀取TestServiceAutoConfiguration類。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ cn.miaolovezhen.TestServiceAutoConfiguration
好啦,搞定!下面可以使用maven install命令把starter存到本地,其他SpringBoot項(xiàng)目需要使用這個(gè)starter,直接導(dǎo)入就可以啦。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java解析dbf之通過javadbf包生成和讀取dbf文件
這篇文章主要介紹了java通過javadbf讀取和生成DBF文件的方法,大家參考使用吧2014-01-01
解決websocket 報(bào) Could not decode a text frame as UTF-8錯(cuò)誤
這篇文章主要介紹了解決websocket 報(bào) Could not decode a text frame as UTF-8錯(cuò)誤,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10
Java構(gòu)建樹形菜單的實(shí)例代碼(支持多級(jí)菜單)
這篇文章主要介紹了Java構(gòu)建樹形菜單的實(shí)例代碼(支持多級(jí)菜單),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-09-09
Java實(shí)現(xiàn)JWT登錄認(rèn)證的示例代碼
Java中我們可以使用諸如JJWT這樣的庫來生成和驗(yàn)證JWT,本文主要介紹了Java實(shí)現(xiàn)JWT登錄認(rèn)證的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04
Mybatis執(zhí)行流程、緩存原理及相關(guān)面試題匯總
最近剛學(xué)完MyBatis,趁著大好機(jī)會(huì),總結(jié)一下它的執(zhí)行流程,面試也愛問這個(gè),下面這篇文章主要給大家介紹了關(guān)于Mybatis執(zhí)行流程、緩存原理及相關(guān)面試題的相關(guān)資料,需要的朋友可以參考下2022-02-02
mybatis-plus批處理IService的實(shí)現(xiàn)示例
這篇文章主要介紹了mybatis-plus批處理IService的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
elasticsearch數(shù)據(jù)信息索引操作action?support示例分析
這篇文章主要為大家介紹了elasticsearch數(shù)據(jù)信息索引操作action?support示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04
Maven?Pom?文件中的隱式依賴導(dǎo)致Jar沖突問題
這篇文章主要介紹了Maven?Pom?文件中的隱式依賴導(dǎo)致Jar沖突問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12

