日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

Spring Boot是如何实现自动配置的?

發(fā)布時(shí)間:2025/3/21 javascript 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot是如何实现自动配置的? 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原文:sylvanassun.github.io/2018/01/08/2018-01-08-spring_boot_auto_configure/

Spring Boot 是 Spring 旗下眾多的子項(xiàng)目之一,其理念是約定優(yōu)于配置,它通過(guò)實(shí)現(xiàn)了自動(dòng)配置(大多數(shù)用戶平時(shí)習(xí)慣設(shè)置的配置作為默認(rèn)配置)的功能來(lái)為用戶快速構(gòu)建出標(biāo)準(zhǔn)化的應(yīng)用。

Spring Boot 的特點(diǎn)可以概述為如下幾點(diǎn):

  • 內(nèi)置了嵌入式的 Tomcat、Jetty 等 Servlet 容器,應(yīng)用可以不用打包成War 格式,而是可以直接以 Jar 格式運(yùn)行。

  • 提供了多個(gè)可選擇的 ”starter ” 以簡(jiǎn)化Maven的依賴管理(也支持Gradle),讓您可以按需加載需要的功能模塊。

  • 盡可能地進(jìn)行自動(dòng)配置,減少了用戶需要?jiǎng)邮謱懙母鞣N冗余配置項(xiàng),Spring Boot 提倡無(wú)XML配置文件的理念,使用Spring Boot生成的應(yīng)用完全不會(huì)生成任何配置代碼與XML配置文件。

  • 提供了一整套的對(duì)應(yīng)用狀態(tài)的監(jiān)控與管理的功能模塊(通過(guò)引入spring-boot-starter-actuator),包括應(yīng)用的線程信息、內(nèi)存信息、應(yīng)用是否處于健康狀態(tài)等,為了滿足更多的資源監(jiān)控需求,Spring Cloud中的很多模塊還對(duì)其進(jìn)行了擴(kuò)展。

有關(guān)Spring Boot的使用方法就不做多介紹了,如有興趣請(qǐng)自行閱讀官方文檔Spring Boot或其他文章。

如今微服務(wù)的概念愈來(lái)愈熱,轉(zhuǎn)型或嘗試微服務(wù)的團(tuán)隊(duì)也在如日漸增,而對(duì)于技術(shù)選型,Spring Cloud是一個(gè)比較好的選擇,它提供了一站式的分布式系統(tǒng)解決方案,包含了許多構(gòu)建分布式系統(tǒng)與微服務(wù)需要用到的組件,例如服務(wù)治理、API網(wǎng)關(guān)、配置中心、消息總線以及容錯(cuò)管理等模塊??梢哉f(shuō),Spring Cloud”全家桶”極其適合剛剛接觸微服務(wù)的團(tuán)隊(duì)。似乎有點(diǎn)跑題了,不過(guò)說(shuō)了這么多,我想要強(qiáng)調(diào)的是,Spring Cloud中的每個(gè)組件都是基于Spring Boot構(gòu)建的,而理解了Spring Boot的自動(dòng)配置的原理,顯然也是有好處的。

Spring Boot的自動(dòng)配置看起來(lái)神奇,其實(shí)原理非常簡(jiǎn)單,背后全依賴于@Conditional注解來(lái)實(shí)現(xiàn)的。

什么是@Conditional?

@Conditional是由Spring 4提供的一個(gè)新特性,用于根據(jù)特定條件來(lái)控制Bean的創(chuàng)建行為。而在我們開(kāi)發(fā)基于Spring的應(yīng)用的時(shí)候,難免會(huì)需要根據(jù)條件來(lái)注冊(cè)Bean。

例如,你想要根據(jù)不同的運(yùn)行環(huán)境,來(lái)讓Spring注冊(cè)對(duì)應(yīng)環(huán)境的數(shù)據(jù)源Bean,對(duì)于這種簡(jiǎn)單的情況,完全可以使用@Profile注解實(shí)現(xiàn),就像下面代碼所示:

@Configuration public class AppConfig {@Bean@Profile("DEV")public DataSource devDataSource() {...}@Bean@Profile("PROD")public DataSource prodDataSource() {...} }

剩下只需要設(shè)置對(duì)應(yīng)的Profile屬性即可,設(shè)置方法有如下三種:

  • 通過(guò)context.getEnvironment().setActiveProfiles("PROD")來(lái)設(shè)置Profile屬性。

  • 通過(guò)設(shè)定jvm的spring.profiles.active參數(shù)來(lái)設(shè)置環(huán)境(Spring Boot中可以直接在application.properties配置文件中設(shè)置該屬性)。

  • 通過(guò)在DispatcherServlet的初始參數(shù)中設(shè)置。

<servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>spring.profiles.active</param-name><param-value>PROD</param-value></init-param> </servlet>

但這種方法只局限于簡(jiǎn)單的情況,而且通過(guò)源碼我們可以發(fā)現(xiàn)@Profile自身也使用了@Conditional注解。

package?org.springframework.context.annotation; @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented @Conditional({ProfileCondition.class})?// 組合了Conditional注解 public?@interface?Profile {String[] value(); } package?org.springframework.context.annotation; class?ProfileCondition?implements?Condition?{ProfileCondition() {}// 通過(guò)提取出@Profile注解中的value值來(lái)與profiles配置信息進(jìn)行匹配public?boolean?matches(ConditionContext context, AnnotatedTypeMetadata metadata)?{if(context.getEnvironment() !=?null) {MultiValueMap attrs = metadata.getAllAnnotationAttributes(Profile.class.getName());if(attrs !=?null) {Iterator var4 = ((List)attrs.get("value")).iterator();Object value;do?{if(!var4.hasNext()) {return?false;}value = var4.next();}?while(!context.getEnvironment().acceptsProfiles((String[])((String[])value)));return?true;}}return?true;} }

在業(yè)務(wù)復(fù)雜的情況下,顯然需要使用到@Conditional注解來(lái)提供更加靈活的條件判斷,例如以下幾個(gè)判斷條件:

  • 在類路徑中是否存在這樣的一個(gè)類。

  • 在Spring容器中是否已經(jīng)注冊(cè)了某種類型的Bean(如未注冊(cè),我們可以讓其自動(dòng)注冊(cè)到容器中,上一條同理)。

  • 一個(gè)文件是否在特定的位置上。

  • 一個(gè)特定的系統(tǒng)屬性是否存在。

  • 在Spring的配置文件中是否設(shè)置了某個(gè)特定的值。

舉個(gè)栗子,假設(shè)我們有兩個(gè)基于不同數(shù)據(jù)庫(kù)實(shí)現(xiàn)的DAO,它們?nèi)紝?shí)現(xiàn)了UserDao,其中JdbcUserDAO與MySql進(jìn)行連接,MongoUserDAO與MongoDB進(jìn)行連接。

現(xiàn)在,我們有了一個(gè)需求,需要根據(jù)命令行傳入的系統(tǒng)參數(shù)來(lái)注冊(cè)對(duì)應(yīng)的UserDao,就像java -jar app.jar -DdbType=MySQL會(huì)注冊(cè)JdbcUserDao,而java -jar app.jar -DdbType=MongoDB則會(huì)注冊(cè)MongoUserDao。

使用@Conditional可以很輕松地實(shí)現(xiàn)這個(gè)功能,僅僅需要在你自定義的條件類中去實(shí)現(xiàn)Condition接口,讓我們來(lái)看下面的代碼。(以下案例來(lái)自:https://dzone.com/articles/how-springboot-autoconfiguration-magic-works)

public?interface?UserDAO?{.... } public?class?JdbcUserDAO?implements?UserDAO?{.... } public?class?MongoUserDAO?implements?UserDAO?{.... } public?class?MySQLDatabaseTypeCondition?implements?Condition?{@Overridepublic?boolean?matches(ConditionContext conditionContext, AnnotatedTypeMetadata metadata)?{String enabledDBType = System.getProperty("dbType");?// 獲得系統(tǒng)參數(shù) dbType// 如果該值等于MySql,則條件成立return?(enabledDBType !=?null?&& enabledDBType.equalsIgnoreCase("MySql"));} } // 與上述邏輯一致 public?class?MongoDBDatabaseTypeCondition?implements?Condition?{@Overridepublic?boolean?matches(ConditionContext conditionContext, AnnotatedTypeMetadata metadata)?{String enabledDBType = System.getProperty("dbType");return?(enabledDBType !=?null?&& enabledDBType.equalsIgnoreCase("MongoDB"));} } // 根據(jù)條件來(lái)注冊(cè)不同的Bean @Configuration public?class?AppConfig?{@Bean@Conditional(MySQLDatabaseTypeCondition.class)public?UserDAO?jdbcUserDAO()?{return?new?JdbcUserDAO();}@Bean@Conditional(MongoDBDatabaseTypeCondition.class)public?UserDAO?mongoUserDAO()?{return?new?MongoUserDAO();} }

現(xiàn)在,我們又有了一個(gè)新需求,我們想要根據(jù)當(dāng)前工程的類路徑中是否存在MongoDB的驅(qū)動(dòng)類來(lái)確認(rèn)是否注冊(cè)MongoUserDAO。為了實(shí)現(xiàn)這個(gè)需求,可以創(chuàng)建檢查MongoDB驅(qū)動(dòng)是否存在的兩個(gè)條件類。

public?class?MongoDriverPresentsCondition?implements?Condition?{@Overridepublic?boolean?matches(ConditionContext conditionContext, AnnotatedTypeMetadata metadata)?{try?{Class.forName("com.mongodb.Server");return?true;}?catch?(ClassNotFoundException e) {return?false;}} } public?class?MongoDriverNotPresentsCondition?implements?Condition?{@Overridepublic?boolean?matches(ConditionContext conditionContext, AnnotatedTypeMetadata metadata)?{try?{Class.forName("com.mongodb.Server");return?false;}?catch?(ClassNotFoundException e) {return?true;}} }

假如,你想要在UserDAO沒(méi)有被注冊(cè)的情況下去注冊(cè)一個(gè)UserDAOBean,那么我們可以定義一個(gè)條件類來(lái)檢查某個(gè)類是否在容器中已被注冊(cè)。

public?class?UserDAOBeanNotPresentsCondition?implements?Condition?{@Overridepublic?boolean?matches(ConditionContext conditionContext, AnnotatedTypeMetadata metadata)?{UserDAO userDAO = conditionContext.getBeanFactory().getBean(UserDAO.class);return?(userDAO ==?null);} }

如果你想根據(jù)配置文件中的某項(xiàng)屬性來(lái)決定是否注冊(cè)MongoDAO,例如app.dbType是否等于MongoDB,我們可以實(shí)現(xiàn)以下的條件類。

public?class?MongoDbTypePropertyCondition?implements?Condition?{@Overridepublic?boolean?matches(ConditionContext conditionContext, AnnotatedTypeMetadata metadata)?{String dbType = conditionContext.getEnvironment().getProperty("app.dbType");return?"MONGO".equalsIgnoreCase(dbType);} }

我們已經(jīng)嘗試并實(shí)現(xiàn)了各種類型的條件判斷,接下來(lái),我們可以選擇一種更為優(yōu)雅的方式,就像@Profile一樣,以注解的方式來(lái)完成條件判斷。首先,我們需要定義一個(gè)注解類。

@Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Documented @Conditional(DatabaseTypeCondition.class) public?@interface?DatabaseType {String?value(); }

具體的條件判斷邏輯在DatabaseTypeCondition類中,它會(huì)根據(jù)系統(tǒng)參數(shù)dbType來(lái)判斷注冊(cè)哪一個(gè)Bean。

public?class?DatabaseTypeCondition?implements?Condition {@Overridepublic?boolean?matches(ConditionContext conditionContext, AnnotatedTypeMetadata metadata) {Map<String,?Object> attributes = metadata.getAnnotationAttributes(DatabaseType.class.getName());String?type?= (String) attributes.get("value");// 默認(rèn)值為MySqlString?enabledDBType = System.getProperty("dbType",?"MySql");return?(enabledDBType !=?null?&&?type?!=?null?&& enabledDBType.equalsIgnoreCase(type));} }

最后,在配置類應(yīng)用該注解即可。

@Configuration @ComponentScan public class AppConfig {@Bean@DatabaseType("MySql")public UserDAO jdbcUserDAO() {return?new?JdbcUserDAO();}@Bean@DatabaseType("mongoDB")public?UserDAO?mongoUserDAO() {return?new?MongoUserDAO();} }

AutoConfigure源碼分析

通過(guò)了解@Conditional注解的機(jī)制其實(shí)已經(jīng)能夠猜到自動(dòng)配置是如何實(shí)現(xiàn)的了,接下來(lái)我們通過(guò)源碼來(lái)看看它是怎么做的。本文中講解的源碼基于Spring Boot 1.5.9版本(最新的正式版本)。

使用過(guò)Spring Boot的童鞋應(yīng)該都很清楚,它會(huì)替我們生成一個(gè)入口類,其命名規(guī)格為ArtifactNameApplication,通過(guò)這個(gè)入口類,我們可以發(fā)現(xiàn)一些信息。

@SpringBootApplication public?class?DemoApplication?{public?static?void?main(String[] args)?{SpringApplication.run(DemoApplication.class, args);} }

首先該類被@SpringBootApplication注解修飾,我們可以先從它開(kāi)始分析,查看源碼后可以發(fā)現(xiàn)它是一個(gè)包含許多注解的組合注解。

@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = {@Filter(type = FilterType.CUSTOM,classes = {TypeExcludeFilter.class} ),?@Filter(type = FilterType.CUSTOM,classes = {AutoConfigurationExcludeFilter.class} )} ) public?@interface?SpringBootApplication {@AliasFor(annotation = EnableAutoConfiguration.class,attribute =?"exclude")Class<?>[] exclude() default {};@AliasFor(annotation = EnableAutoConfiguration.class,attribute =?"excludeName")String[] excludeName() default {};@AliasFor(annotation = ComponentScan.class,attribute =?"basePackages")String[] scanBasePackages() default {};@AliasFor(annotation = ComponentScan.class,attribute =?"basePackageClasses")Class<?>[] scanBasePackageClasses() default {}; }

該注解相當(dāng)于同時(shí)聲明了@Configuration、@EnableAutoConfiguration與@ComponentScan三個(gè)注解(如果我們想定制自定義的自動(dòng)配置實(shí)現(xiàn),聲明這三個(gè)注解就足夠了),而@EnableAutoConfiguration是我們的關(guān)注點(diǎn),從它的名字可以看出來(lái),它是用來(lái)開(kāi)啟自動(dòng)配置的,源碼如下:

@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?{}; }

我們發(fā)現(xiàn)@Import(Spring 提供的一個(gè)注解,可以導(dǎo)入配置類或者Bean到當(dāng)前類中)導(dǎo)入了EnableAutoConfigurationImportSelector類,根據(jù)名字來(lái)看,它應(yīng)該就是我們要找到的目標(biāo)了。不過(guò)查看它的源碼發(fā)現(xiàn)它已經(jīng)被Deprecated了,而官方API中告知我們?nèi)ゲ榭此母割怉utoConfigurationImportSelector。

/**?@deprecated?*/ @Deprecated public?class?EnableAutoConfigurationImportSelector?extends?AutoConfigurationImportSelector?{public?EnableAutoConfigurationImportSelector()?{}protected?boolean?isEnabled(AnnotationMetadata metadata)?{return?this.getClass().equals(EnableAutoConfigurationImportSelector.class)?((Boolean)this.getEnvironment().getProperty("spring.boot.enableautoconfiguration", Boolean.class, Boolean.valueOf(true))).booleanValue():true;} }

由于AutoConfigurationImportSelector的源碼太長(zhǎng)了,這里我只截出關(guān)鍵的地方,顯然方法selectImports是選擇自動(dòng)配置的主入口,它調(diào)用了其他的幾個(gè)方法來(lái)加載元數(shù)據(jù)等信息,最后返回一個(gè)包含許多自動(dòng)配置類信息的字符串?dāng)?shù)組。

public?String[] selectImports(AnnotationMetadata annotationMetadata) {if(!this.isEnabled(annotationMetadata)) {return?NO_IMPORTS;}?else?{try?{AutoConfigurationMetadata ex = AutoConfigurationMetadataLoader.loadMetadata(this.beanClassLoader);AnnotationAttributes attributes =?this.getAttributes(annotationMetadata);List configurations =?this.getCandidateConfigurations(annotationMetadata, attributes);configurations =?this.removeDuplicates(configurations);configurations =?this.sort(configurations, ex);Set exclusions =?this.getExclusions(annotationMetadata, attributes);this.checkExcludedClasses(configurations, exclusions);configurations.removeAll(exclusions);configurations =?this.filter(configurations, ex);this.fireAutoConfigurationImportEvents(configurations, exclusions);return?(String[])configurations.toArray(new?String[configurations.size()]);}?catch?(IOException var6) {throw?new?IllegalStateException(var6);}} }

重點(diǎn)在于方法getCandidateConfigurations()返回了自動(dòng)配置類的信息列表,而它通過(guò)調(diào)用SpringFactoriesLoader.loadFactoryNames()來(lái)掃描加載含有META-INF/spring.factories文件的jar包,該文件記錄了具有哪些自動(dòng)配置類。(建議還是用IDE去看源碼吧,這些源碼單行實(shí)在太長(zhǎng)了,估計(jì)文章中的觀看效果很差)

protected?List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {List?configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());Assert.notEmpty(configurations,?"No auto configuration classes?found in META-INF spring.factories.?If you are using a custom packaging, make sure that file is correct.");return?configurations; }public?static?List<String> loadFactoryNames(Class<?>?factoryClass,?ClassLoader?classLoader)?{String factoryClassName = factoryClass.getName();try?{Enumeration ex = classLoader !=?null?classLoader.getResources("META-INF/spring.factories"):ClassLoader.getSystemResources("META-INF/spring.factories");ArrayList result =?new?ArrayList();while(ex.hasMoreElements()) {URL url = (URL)ex.nextElement();Properties properties = PropertiesLoaderUtils.loadProperties(new?UrlResource(url));String factoryClassNames = properties.getProperty(factoryClassName);result.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(factoryClassNames)));}return?result;}?catch?(IOException var8) {throw?new?IllegalArgumentException("Unable to load ["?+ factoryClass.getName() +?"] factories from location ["?+?"META-INF/spring.factories"?+?"]", var8);} }

接下來(lái),我們?cè)趕pring.factories文件中隨便找一個(gè)自動(dòng)配置類,來(lái)看看是怎樣實(shí)現(xiàn)的。我查看了MongoDataAutoConfiguration的源碼,發(fā)現(xiàn)它聲明了@ConditionalOnClass注解,通過(guò)看該注解的源碼后可以發(fā)現(xiàn),這是一個(gè)組合了@Conditional的組合注解,它的條件類是OnClassCondition。

@Configuration @ConditionalOnClass({Mongo.class, MongoTemplate.class}) @EnableConfigurationProperties({MongoProperties.class}) @AutoConfigureAfter({MongoAutoConfiguration.class}) public?class?MongoDataAutoConfiguration?{.... } @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented @Conditional({OnClassCondition.class}) public @interface ConditionalOnClass {Class<?>[] value() default {};String[] name() default {}; }

然后,我們開(kāi)始看OnClassCondition的源碼,發(fā)現(xiàn)它并沒(méi)有直接實(shí)現(xiàn)Condition接口,只好往上找,發(fā)現(xiàn)它的父類SpringBootCondition實(shí)現(xiàn)了Condition接口。

class?OnClassCondition?extends?SpringBootCondition?implements?AutoConfigurationImportFilter,?BeanFactoryAware,?BeanClassLoaderAware?{..... } public?abstract?class?SpringBootCondition?implements?Condition?{private?final?Log logger = LogFactory.getLog(this.getClass());public?SpringBootCondition()?{}public?final?boolean?matches(ConditionContext context, AnnotatedTypeMetadata metadata)?{String classOrMethodName = getClassOrMethodName(metadata);try?{ConditionOutcome ex =?this.getMatchOutcome(context, metadata);this.logOutcome(classOrMethodName, ex);this.recordEvaluation(context, classOrMethodName, ex);return?ex.isMatch();}?catch?(NoClassDefFoundError var5) {throw?new?IllegalStateException("Could not evaluate condition on "?+ classOrMethodName +?" due to "?+ var5.getMessage() +?" not found. Make sure your own configuration does not rely on that class. This can also happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)", var5);}?catch?(RuntimeException var6) {throw?new?IllegalStateException("Error processing condition on "?+?this.getName(metadata), var6);}}public?abstract?ConditionOutcome?getMatchOutcome(ConditionContext var1, AnnotatedTypeMetadata var2); }

SpringBootCondition實(shí)現(xiàn)的matches方法依賴于一個(gè)抽象方法this.getMatchOutcome(context, metadata),我們?cè)谒淖宇怬nClassCondition中可以找到這個(gè)方法的具體實(shí)現(xiàn)。

public?ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {ClassLoader classLoader = context.getClassLoader();ConditionMessage matchMessage = ConditionMessage.empty();// 找出所有ConditionalOnClass注解的屬性List onClasses =?this.getCandidates(metadata, ConditionalOnClass.class);List onMissingClasses;if(onClasses !=?null) {// 找出不在類路徑中的類onMissingClasses =?this.getMatches(onClasses, OnClassCondition.MatchType.MISSING, classLoader);// 如果存在不在類路徑中的類,匹配失敗if(!onMissingClasses.isEmpty()) {return?ConditionOutcome.noMatch(ConditionMessage.forCondition(ConditionalOnClass.class,?new?Object[0]).didNotFind("required class",?"required classes").items(Style.QUOTE, onMissingClasses));}matchMessage = matchMessage.andCondition(ConditionalOnClass.class,?new?Object[0]).found("required class",?"required classes").items(Style.QUOTE,?this.getMatches(onClasses, OnClassCondition.MatchType.PRESENT, classLoader));}// 接著找出所有ConditionalOnMissingClass注解的屬性// 它與ConditionalOnClass注解的含義正好相反,所以以下邏輯也與上面相反onMissingClasses =?this.getCandidates(metadata, ConditionalOnMissingClass.class);if(onMissingClasses !=?null) {List present =?this.getMatches(onMissingClasses, OnClassCondition.MatchType.PRESENT, classLoader);if(!present.isEmpty()) {return?ConditionOutcome.noMatch(ConditionMessage.forCondition(ConditionalOnMissingClass.class,?new?Object[0]).found("unwanted class",?"unwanted classes").items(Style.QUOTE, present));}matchMessage = matchMessage.andCondition(ConditionalOnMissingClass.class,?new?Object[0]).didNotFind("unwanted class",?"unwanted classes").items(Style.QUOTE,?this.getMatches(onMissingClasses, OnClassCondition.MatchType.MISSING, classLoader));}return?ConditionOutcome.match(matchMessage); } // 獲得所有annotationType注解的屬性 private?List<String> getCandidates(AnnotatedTypeMetadata metadata, Class<?> annotationType) {MultiValueMap attributes = metadata.getAllAnnotationAttributes(annotationType.getName(),?true);ArrayList candidates =?new?ArrayList();if(attributes ==?null) {return?Collections.emptyList();}?else?{this.addAll(candidates, (List)attributes.get("value"));this.addAll(candidates, (List)attributes.get("name"));return?candidates;} } private?void?addAll(List<String> list, List<Object> itemsToAdd) {if(itemsToAdd !=?null) {Iterator var3 = itemsToAdd.iterator();while(var3.hasNext()) {Object?item = var3.next();Collections.addAll(list, (String[])((String[])item));}} } ? ? // 根據(jù)matchType.matches方法來(lái)進(jìn)行匹配 private?List<String> getMatches(Collection<String> candidates, OnClassCondition.MatchType matchType, ClassLoader classLoader) {ArrayList matches =?new?ArrayList(candidates.size());Iterator var5 = candidates.iterator();while(var5.hasNext()) {String?candidate = (String)var5.next();if(matchType.matches(candidate, classLoader)) {matches.add(candidate);}}return?matches; }

關(guān)于match的具體實(shí)現(xiàn)在MatchType中,它是一個(gè)枚舉類,提供了PRESENT和MISSING兩種實(shí)現(xiàn),前者返回類路徑中是否存在該類,后者相反。

private?static?enum?MatchType {PRESENT {public?boolean?matches(String className, ClassLoader classLoader)?{return?OnClassCondition.MatchType.isPresent(className, classLoader);}},MISSING {public?boolean?matches(String className, ClassLoader classLoader)?{return?!OnClassCondition.MatchType.isPresent(className, classLoader);}};private?MatchType()?{}// 跟我們之前看過(guò)的案例一樣,都利用了類加載功能來(lái)進(jìn)行判斷private?static?boolean?isPresent(String className, ClassLoader classLoader)?{if(classLoader ==?null) {classLoader = ClassUtils.getDefaultClassLoader();}try?{forName(className, classLoader);return?true;}?catch?(Throwable var3) {return?false;}}private?static?Class<?> forName(String className, ClassLoader classLoader)?throws?ClassNotFoundException {return?classLoader !=?null?classLoader.loadClass(className):Class.forName(className);}public?abstract?boolean?matches(String var1, ClassLoader var2); }

現(xiàn)在終于真相大白,@ConditionalOnClass的含義是指定的類必須存在于類路徑下,MongoDataAutoConfiguration類中聲明了類路徑下必須含有Mongo.class, MongoTemplate.class這兩個(gè)類,否則該自動(dòng)配置類不會(huì)被加載。

在Spring Boot中到處都有類似的注解,像@ConditionalOnBean(容器中是否有指定的Bean),@ConditionalOnWebApplication(當(dāng)前工程是否為一個(gè)Web工程)等等,它們都只是@Conditional注解的擴(kuò)展。

當(dāng)你揭開(kāi)神秘的面紗,去探索本質(zhì)時(shí),發(fā)現(xiàn)其實(shí)Spring Boot自動(dòng)配置的原理就是如此簡(jiǎn)單,在了解這些知識(shí)后,你完全可以自己去實(shí)現(xiàn)自定義的自動(dòng)配置類,然后編寫出自定義的starter。

總結(jié)

以上是生活随笔為你收集整理的Spring Boot是如何实现自动配置的?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。