亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 開發 > Java > 正文

Spring Boot 自動配置的實現

2024-07-14 08:41:59
字體:
來源:轉載
供稿:網友

Spring Boot 自動配置

來看下 spring boot中自動配置的注解

@SuppressWarnings("deprecation")@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited@AutoConfigurationPackage@Import(EnableAutoConfigurationImportSelector.class)public @interface EnableAutoConfiguration {  String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";  /**   * Exclude specific auto-configuration classes such that they will never be applied.   * @return the classes to exclude   */  Class<?>[] exclude() default {};  /**   * Exclude specific auto-configuration class names such that they will never be   * applied.   * @return the class names to exclude   * @since 1.3.0   */  String[] excludeName() default {};}
  1. exclude() 可以排除一些自動配置的內容
  2. excludeName 通過名稱排除自動配置內容

再來看下, @EnableAutoConfiguration 是怎么處理自動配置的呢?

注意到@Import(EnableAutoConfigurationImportSelector.class)

public class EnableAutoConfigurationImportSelector    extends AutoConfigurationImportSelector {  @Override  protected boolean isEnabled(AnnotationMetadata metadata) {    if (getClass().equals(EnableAutoConfigurationImportSelector.class)) {      return getEnvironment().getProperty(          EnableAutoConfiguration.ENABLED_OVERRIDE_PROPERTY, Boolean.class,          true);    }    return true;  }}}

再來看下 AutoConfigurationImportSelector ,主要是 接口的 ImportSelector 的實現

@Override  public String[] selectImports(AnnotationMetadata annotationMetadata) {    if (!isEnabled(annotationMetadata)) {      return NO_IMPORTS;    }    try {      //1、 自動配置的元數據 spring-autocomfigure-metadata.properties      // 自動配置的開啟條件      AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader          .loadMetadata(this.beanClassLoader);      AnnotationAttributes attributes = getAttributes(annotationMetadata);      // 獲取設置的自動配置列表 spring.factories      List<String> configurations = getCandidateConfigurations(annotationMetadata,          attributes);      configurations = removeDuplicates(configurations);      configurations = sort(configurations, autoConfigurationMetadata);      // 獲取要排除的自動配置列表,可以通過 注解@EnableAutoConfiguration 的exclude和       // 配置文件設置 spring.autoconfigure.exclude key的值      Set<String> exclusions = getExclusions(annotationMetadata, attributes);      checkExcludedClasses(configurations, exclusions);      configurations.removeAll(exclusions);       // 通過 spring-autocomfigure-metadata.properties ConditionOnClass 條件進行過濾      configurations = filter(configurations, autoConfigurationMetadata);      fireAutoConfigurationImportEvents(configurations, exclusions);      return configurations.toArray(new String[configurations.size()]);    }    catch (IOException ex) {      throw new IllegalStateException(ex);    }  }

看下 spring.factories 文件

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,/org.springframework.boot.autoconfigure.elasticsearch.jest.JestAutoConfiguration,/org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration,/org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration,/org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration,/org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,/

再看下 spring framework中 ConfigurationClassParser 的處理方式,會解析 @Import 里的接口 ImportSelector返回的所有配置類,那是怎么配置的呢,如 JpaRepositoriesAutoConfiguration

@Configuration@ConditionalOnBean(DataSource.class)@ConditionalOnClass(JpaRepository.class)@ConditionalOnMissingBean({ JpaRepositoryFactoryBean.class,    JpaRepositoryConfigExtension.class })@ConditionalOnProperty(prefix = "spring.data.jpa.repositories", name = "enabled", havingValue = "true", matchIfMissing = true)@Import(JpaRepositoriesAutoConfigureRegistrar.class)@AutoConfigureAfter(HibernateJpaAutoConfiguration.class)public class JpaRepositoriesAutoConfiguration {}

從上面可以看到,有很多的@ConditionalOn**的注解,我們來看下 ConditionEvaluator這個 條件計算器,會去計算出當前這個配置類 是否要開啟,而這些 @ConditionalOn** 是依賴于 @Conditional 這個注解,如  @ConditionalOnBean 最終是通過 Condition 接口來作條件選擇

@Target({ ElementType.TYPE, ElementType.METHOD })@Retention(RetentionPolicy.RUNTIME)@Documented@Conditional(OnBeanCondition.class)public @interface ConditionalOnBean {  /**   * The class type of bean that should be checked. The condition matches when any of   * the classes specified is contained in the {@link ApplicationContext}.   * @return the class types of beans to check   */  Class<?>[] value() default {};  /**   * The class type names of bean that should be checked. The condition matches when any   * of the classes specified is contained in the {@link ApplicationContext}.   * @return the class type names of beans to check   */  String[] type() default {};  /**   * The annotation type decorating a bean that should be checked. The condition matches   * when any of the annotations specified is defined on a bean in the   * {@link ApplicationContext}.   * @return the class-level annotation types to check   */  Class<? extends Annotation>[] annotation() default {};  /**   * The names of beans to check. The condition matches when any of the bean names   * specified is contained in the {@link ApplicationContext}.   * @return the name of beans to check   */  String[] name() default {};  /**   * Strategy to decide if the application context hierarchy (parent contexts) should be   * considered.   * @return the search strategy   */  SearchStrategy search() default SearchStrategy.ALL;}

Spring boot 的autoconfigure 是囊括了所有可以和spring 整合的項目,但大部分情況下,并不是所以的項目都會啟用,通過 Condition和@Conditional 來判斷條件

  1. 判斷classPath 是否存在指定的類  @ConditionalOnClass
  2. 判斷 ApplicationContext 中是否存在指定的 Bean  @ConditionalOnBean
  3. 配置環境中是否存在特定的配置項  @ConditionalOnProperty
  4. 配置環境中指定的配置項是否存在指定的值

禁用配置

當前 也是可以禁用某些我們不想要的默認配置,如上面加載時說到,會排除一些配置(exclude)

  1. 設置 @EnableAutoConfiguration 的exclude 配置
  2. 在配置文件增加 spring.autoconfigure.exclude 配置

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
91免费福利视频| 国产精选久久久久久| 亚洲自拍高清视频网站| 亚洲国产成人精品女人久久久| 理论片在线不卡免费观看| 国产丝袜精品第一页| 日韩va亚洲va欧洲va国产| 亚洲精品日韩丝袜精品| 青青a在线精品免费观看| 欧美洲成人男女午夜视频| 一本大道久久加勒比香蕉| 亚洲男人天堂古典| 亚洲精品一区久久久久久| 亚洲一区二区中文字幕| 欧美中文字幕在线观看| 亚洲va欧美va国产综合久久| 国产亚洲a∨片在线观看| 中日韩美女免费视频网址在线观看| 亚洲欧洲中文天堂| 国产精品久久久久9999| 91国内揄拍国内精品对白| 日韩中文字幕在线免费观看| 亚洲第一免费播放区| 亚洲xxx视频| 国内精品美女av在线播放| 欧美另类高清videos| 日韩欧美中文第一页| 国产在线精品一区免费香蕉| 亚洲区免费影片| 国产成人一区二区| 亚洲人成网站777色婷婷| 亚洲欧美在线播放| 欧美重口另类videos人妖| 91九色单男在线观看| 日韩在线视频网站| 国产精品扒开腿做爽爽爽视频| 亚洲开心激情网| 精品免费在线观看| 亚洲精品aⅴ中文字幕乱码| 欧美黄色片在线观看| 亚洲第一色中文字幕| 国产日韩中文字幕在线| 国产自产女人91一区在线观看| 亚洲激情在线视频| 国产成人福利网站| 国产精品久久久久久亚洲调教| 国产精品人人做人人爽| 国产午夜精品麻豆| 欧美电影第一页| 国产亚洲欧美视频| 欧美激情视频在线| 国产一区玩具在线观看| 久久精品国产欧美亚洲人人爽| 日韩美女免费观看| 亚洲男人av电影| 在线电影欧美日韩一区二区私密| 午夜精品一区二区三区在线播放| 91亚洲人电影| 91高清视频免费观看| 亚洲天堂开心观看| 欧美国产激情18| 少妇高潮久久久久久潘金莲| 欧美日韩免费在线| 成人av电影天堂| 国产免费一区二区三区香蕉精| 久操成人在线视频| 91精品国产高清久久久久久91| 91亚洲国产成人精品性色| 日本久久亚洲电影| 国产日韩欧美在线视频观看| 8090理伦午夜在线电影| 日韩在线观看网站| 欧美激情第6页| 成人av番号网| 日韩成人免费视频| 国产日产欧美a一级在线| 国产91av在线| 国产精品久久久久影院日本| 国内外成人免费激情在线视频| 久久久精品在线观看| 色悠悠久久88| 欧美日韩亚洲高清| 2019中文在线观看| 国产91精品青草社区| 久久亚洲欧美日韩精品专区| 亚洲国产美女久久久久| 成人黄色短视频在线观看| 精品久久久久久久久久久久久久| 久久久这里只有精品视频| 成人精品一区二区三区电影黑人| 538国产精品一区二区在线| 精品久久久久久久久久久| 狠狠躁夜夜躁人人爽天天天天97| 国产精品偷伦一区二区| 国产日韩在线看| 精品福利免费观看| 久久精品亚洲94久久精品| 日本久久久久久久| 国产精品欧美在线| 亚洲免费伊人电影在线观看av| 久久亚洲精品一区| 亚洲美女精品成人在线视频| 成人午夜在线影院| 68精品久久久久久欧美| 国产69精品99久久久久久宅男| 日韩av在线资源| 亚洲一区二区三区在线免费观看| 久久人人爽亚洲精品天堂| 国产亚洲精品久久久久久777| 一区二区三区 在线观看视| 久久久久久久国产精品| 久久精品国产清自在天天线| 欧美大片在线看| 超薄丝袜一区二区| 国产美女直播视频一区| 日本精品一区二区三区在线| 欧美人与物videos| 精品高清美女精品国产区| 6080yy精品一区二区三区| 亚洲精品动漫久久久久| 91久久在线观看| 日韩免费av在线| 久久久久久久av| 夜夜嗨av色综合久久久综合网| 57pao精品| 久久久精品免费| 亚洲最大中文字幕| 亚洲国产美女精品久久久久∴| 中国人与牲禽动交精品| 在线观看视频99| 国产精品扒开腿做爽爽爽的视频| 亚洲欧美另类国产| 日韩av大片在线| 欧美在线一级va免费观看| 国产精品美乳一区二区免费| 午夜精品久久17c| 久久综合色88| 欧美日韩国产一中文字不卡| 久久久久久午夜| 久久久久久尹人网香蕉| 国产精品久久久久久av福利| 国外日韩电影在线观看| 亚洲久久久久久久久久久| 日韩欧美国产一区二区| 日韩在线观看av| 超碰97人人做人人爱少妇| 国产精品高清在线| 奇米影视亚洲狠狠色| 亚洲性av在线| 日韩av在线免费看| 91av视频在线播放| 亚洲国产精品999| 国产免费一区二区三区在线能观看| 久久久久久久一区二区三区| 久久久精品999| 亚洲精品456在线播放狼人| 亚洲国产精品视频在线观看| 欧美黄色片免费观看| 精品国产福利在线| 黄色一区二区在线| 91成人精品网站| 亚洲精品免费一区二区三区| 日韩精品日韩在线观看| 日韩欧美一区二区三区|