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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Sprig boot自动配置

發布時間:2023/12/4 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Sprig boot自动配置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、概述

Spring Boot是Spring旗下眾多的子項目之一,其理念是約定優于配置,它通過實現了自動配置(大多數用戶平時習慣設置的配置作為默認配置)的功能來為用戶快速構建出標準化的應用。Spring Boot的特點可以概述為如下幾點:

  • 內置了嵌入式的Tomcat、Jetty等Servlet容器,應用可以不用打包成War格式,而是可以直接以Jar格式運行。

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

  • 盡可能地進行自動配置,減少了用戶需要動手寫的各種冗余配置項,Spring Boot提倡無XML配置文件的理念,使用Spring Boot生成的應用完全不會生成任何配置代碼與XML配置文件。

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

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

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

Spring Boot的自動配置看起來神奇,其實原理非常簡單,背后全依賴于@Conditional注解來實現的。

2、什么是@Conditional?

@Conditional是由Spring 4提供的一個新特性,用于根據特定條件來控制Bean的創建行為。而在我們開發基于Spring的應用的時候,難免會需要根據條件來注冊Bean。

例如,你想要根據不同的運行環境,來讓Spring注冊對應環境的數據源Bean,對于這種簡單的情況,完全可以使用@Profile注解實現,就像下面代碼所示:

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

剩下只需要設置對應的Profile屬性即可,設置方法有如下三種:

  • 通過context.getEnvironment().setActiveProfiles(“PROD”)來設置Profile屬性。
  • 通過設定jvm的spring.profiles.active參數來設置環境(Spring Boot中可以直接在application.properties配置文件中設置該屬性)。
  • 通過在DispatcherServlet的初始參數中設置。
<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>

但這種方法只局限于簡單的情況,而且通過源碼我們可以發現@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() {}// 通過提取出@Profile注解中的value值來與profiles配置信息進行匹配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;} }

在業務復雜的情況下,顯然需要使用到@Conditional注解來提供更加靈活的條件判斷,例如以下幾個判斷條件:

  • 在類路徑中是否存在這樣的一個類。
  • 在Spring容器中是否已經注冊了某種類型的Bean(如未注冊,我們可以讓其自動注冊到容器中,上一條同理)。
  • 一個文件是否在特定的位置上。
  • 一個特定的系統屬性是否存在。
  • 在Spring的配置文件中是否設置了某個特定的值。

舉個栗子,假設我們有兩個基于不同數據庫實現的DAO,它們全都實現了UserDao,其中JdbcUserDAO與MySql進行連接,MongoUserDAO與MongoDB進行連接?,F在,我們有了一個需求,需要根據命令行傳入的系統參數來注冊對應的UserDao,就像java -jar app.jar -DdbType=MySQL會注冊JdbcUserDao,而java -jar app.jar -DdbType=MongoDB則會注冊MongoUserDao。使用@Conditional可以很輕松地實現這個功能,僅僅需要在你自定義的條件類中去實現Condition接口,讓我們來看下面的代碼。(以下案例來自: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"); // 獲得系統參數 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"));} } // 根據條件來注冊不同的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();} }

現在,我們又有了一個新需求,我們想要根據當前工程的類路徑中是否存在MongoDB的驅動類來確認是否注冊MongoUserDAO。為了實現這個需求,可以創建檢查MongoDB驅動是否存在的兩個條件類。

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沒有被注冊的情況下去注冊一個UserDAOBean,那么我們可以定義一個條件類來檢查某個類是否在容器中已被注冊。

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

如果你想根據配置文件中的某項屬性來決定是否注冊MongoDAO,例如app.dbType是否等于MongoDB,我們可以實現以下的條件類。

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

我們已經嘗試并實現了各種類型的條件判斷,接下來,我們可以選擇一種更為優雅的方式,就像@Profile一樣,以注解的方式來完成條件判斷。首先,我們需要定義一個注解類。

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

具體的條件判斷邏輯在DatabaseTypeCondition類中,它會根據系統參數dbType來判斷注冊哪一個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");// 默認值為MySqlString enabledDBType = System.getProperty("dbType", "MySql");return (enabledDBType != null && type != null && enabledDBType.equalsIgnoreCase(type));} }

最后,在配置類應用該注解即可。

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

下面列舉Spring Boot對@Conditional的擴展注解

3、AutoConfigure源碼分析

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

SpringBoot 自動配置主要通過 @EnableAutoConfiguration, @Conditional, @EnableConfigurationProperties 或者 @ConfigurationProperties 等幾個注解來進行自動配置完成的。

@EnableAutoConfiguration 開啟自動配置,主要作用就是調用 Spring-Core 包里的 loadFactoryNames(),將 autoconfig 包里的已經寫好的自動配置加載進來。

@Conditional 條件注解,通過判斷類路徑下有沒有相應配置的 jar 包來確定是否加載和自動配置這個類。

@EnableConfigurationProperties 的作用就是,給自動配置提供具體的配置參數,只需要寫在 application.properties 中,就可以通過映射寫入配置類的 POJO 屬性中。

@EnableAutoConfiguration

@Enable*注釋并不是SpringBoot新發明的注釋,Spring 3框架就引入了這些注釋,用這些注釋替代XML配置文件。比如:
@EnableTransactionManagement注釋,它能夠聲明事務管理
@EnableWebMvc注釋,它能啟用Spring MVC
@EnableScheduling注釋,它可以初始化一個調度器。

這些注釋事實上都是簡單的配置,通過@Import注釋導入。
從啟動類的@SpringBootApplication進入,在里面找到了@EnableAutoConfiguration,

@EnableAutoConfiguration里通過@Import導入了 EnableAutoConfigurationImportSelector

進入他的父類AutoConfigurationImportSelector

找到selectImports()方法,他調用了getCandidateConfigurations()方法,在這里,這個方法又調用了Spring Core包中的loadFactoryNames()方法。這個方法的作用是,會查詢META-INF/spring.factories文件中包含的JAR文件。

找到spring.factories文件后,SpringFactoriesLoader將查詢配置文件命名的屬性。

Jar文件在org.springframework.boot.autoconfigure的spring.factories

spring.factories內容如下(截取部分),在這個文件中,可以看到一系列Spring Boot自動配置的列表

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,\ org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\ org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,\ org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\ org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.cassandra.CassandraRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.couchbase.CouchbaseDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.couchbase.CouchbaseRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration,\ org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.ldap.LdapDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.ldap.LdapRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.mongo.MongoRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.neo4j.Neo4jDataAutoConfiguration,\ org.springframework.boot.autoconfigure.data.neo4j.Neo4jRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.solr.SolrRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration,\ org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration,\ org.springframework.boot.autoconfigure.data.rest.RepositoryRestMvcAutoConfiguration,\ org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration,\

自動配置類中的條件注解

接下來,我們在spring.factories文件中隨便找一個自動配置類,來看看是怎樣實現的。我查看了MongoDataAutoConfiguration的源碼,發現它聲明了@ConditionalOnClass注解,通過看該注解的源碼后可以發現,這是一個組合了@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 {}; }

然后,我們開始看OnClassCondition的源碼,發現它并沒有直接實現Condition接口,只好往上找,發現它的父類SpringBootCondition實現了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實現的matches方法依賴于一個抽象方法this.getMatchOutcome(context, metadata),我們在它的子類OnClassCondition中可以找到這個方法的具體實現。

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));}} } // 根據matchType.matches方法來進行匹配 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; }

關于match的具體實現在MatchType中,它是一個枚舉類,提供了PRESENT和MISSING兩種實現,前者返回類路徑中是否存在該類,后者相反。

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() {}// 跟我們之前看過的案例一樣,都利用了類加載功能來進行判斷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); }

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

在Spring Boot中到處都有類似的注解,像@ConditionalOnBean(容器中是否有指定的Bean),@ConditionalOnWebApplication(當前工程是否為一個Web工程)等等,它們都只是@Conditional注解的擴展。當你揭開神秘的面紗,去探索本質時,發現其實Spring Boot自動配置的原理就是如此簡單,在了解這些知識后,你完全可以自己去實現自定義的自動配置類,然后編寫出自定義的starter。

4、總結

SpringBoot 的 自動配置得益于 SpringFramework 強大的支撐,框架早已有很多工具和注解可以自動裝配 Bean 。SpringBoot 通過 一個封裝,將市面上通用的組件直接寫好了配置類。當我們程序去依賴了這些組件的 jar 包后,啟動 SpringBoot應用,于是自動加載開始了。

我們也可以定義自己的自動裝配組件,依賴之后,Spring直接可以加載我們定義的 starter 。

加載步驟:

1)SpringBoot啟動會加載大量的自動配置類
2)我們看我們需要的功能有沒有SpringBoot默認寫好的自動配置類;
3)我們再來看這個自動配置類中到底配置了哪些組件;(只要我們要用的組件有,我們就不需要再來配置了)
4)給容器中自動配置類添加組件的時候,會從properties類中獲取某些屬性。我們就可以在配置文件中指定這
些屬性的值;

總結

以上是生活随笔為你收集整理的Sprig boot自动配置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。