最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot 自動(dòng)配置的實(shí)現(xiàn)

 更新時(shí)間:2018年08月10日 10:24:34   作者:David_jim  
這篇文章主要介紹了Spring Boot 自動(dòng)配置的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

Spring Boot 自動(dòng)配置

來(lái)看下 spring boot中自動(dòng)配置的注解

@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() 可以排除一些自動(dòng)配置的內(nèi)容
  2. excludeName 通過(guò)名稱(chēng)排除自動(dòng)配置內(nèi)容

再來(lái)看下, @EnableAutoConfiguration 是怎么處理自動(dòng)配置的呢?

注意到@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;
  }
}

}

再來(lái)看下 AutoConfigurationImportSelector ,主要是 接口的 ImportSelector 的實(shí)現(xiàn)

@Override
  public String[] selectImports(AnnotationMetadata annotationMetadata) {
    if (!isEnabled(annotationMetadata)) {
      return NO_IMPORTS;
    }
    try {
      //1、 自動(dòng)配置的元數(shù)據(jù) spring-autocomfigure-metadata.properties
      // 自動(dòng)配置的開(kāi)啟條件
      AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
          .loadMetadata(this.beanClassLoader);
      AnnotationAttributes attributes = getAttributes(annotationMetadata);
      // 獲取設(shè)置的自動(dòng)配置列表 spring.factories
      List<String> configurations = getCandidateConfigurations(annotationMetadata,
          attributes);
      configurations = removeDuplicates(configurations);
      configurations = sort(configurations, autoConfigurationMetadata);
      // 獲取要排除的自動(dòng)配置列表,可以通過(guò) 注解@EnableAutoConfiguration 的exclude和
       // 配置文件設(shè)置 spring.autoconfigure.exclude key的值
      Set<String> exclusions = getExclusions(annotationMetadata, attributes);
      checkExcludedClasses(configurations, exclusions);
      configurations.removeAll(exclusions);
       // 通過(guò) spring-autocomfigure-metadata.properties ConditionOnClass 條件進(jìn)行過(guò)濾
      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 的處理方式,會(huì)解析 @Import 里的接口 ImportSelector 返回的所有配置類(lèi),那是怎么配置的呢,如 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**的注解,我們來(lái)看下 ConditionEvaluator這個(gè) 條件計(jì)算器,會(huì)去計(jì)算出當(dāng)前這個(gè)配置類(lèi) 是否要開(kāi)啟,而這些 @ConditionalOn** 是依賴(lài)于 @Conditional 這個(gè)注解,如  @ConditionalOnBean 最終是通過(guò) Condition 接口來(lái)作條件選擇

@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 整合的項(xiàng)目,但大部分情況下,并不是所以的項(xiàng)目都會(huì)啟用,通過(guò) Condition和@Conditional 來(lái)判斷條件

  1. 判斷classPath 是否存在指定的類(lèi)  @ConditionalOnClass
  2. 判斷 ApplicationContext 中是否存在指定的 Bean  @ConditionalOnBean
  3. 配置環(huán)境中是否存在特定的配置項(xiàng)  @ConditionalOnProperty
  4. 配置環(huán)境中指定的配置項(xiàng)是否存在指定的值

禁用配置

當(dāng)前 也是可以禁用某些我們不想要的默認(rèn)配置,如上面加載時(shí)說(shuō)到,會(huì)排除一些配置(exclude)

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 淺談Java常見(jiàn)的排序算法

    淺談Java常見(jiàn)的排序算法

    今天給大家?guī)?lái)的是關(guān)于Java的相關(guān)知識(shí),文章圍繞著Java常見(jiàn)的排序算法展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • 從底層源碼深入分析Spring的IoC容器的實(shí)現(xiàn)原理

    從底層源碼深入分析Spring的IoC容器的實(shí)現(xiàn)原理

    IoC容器負(fù)責(zé)管理對(duì)象的生命周期和依賴(lài)關(guān)系,大大簡(jiǎn)化了應(yīng)用程序的開(kāi)發(fā)和維,我們這篇文章將會(huì)從底層源碼的角度深入分析Spring的IoC容器實(shí)現(xiàn),探索它的工作原理和關(guān)鍵組件,需要的朋友可以參考下
    2023-07-07
  • SpringBoot yaml語(yǔ)法與數(shù)據(jù)讀取操作詳解

    SpringBoot yaml語(yǔ)法與數(shù)據(jù)讀取操作詳解

    YAML 是 “YAML Ain’t Markup Language”(YAML 不是一種標(biāo)記語(yǔ)言)的遞歸縮寫(xiě)。在開(kāi)發(fā)的這種語(yǔ)言時(shí),YAML 的意思其實(shí)是:“Yet Another Markup Language”(仍是一種標(biāo)記語(yǔ)言),本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07
  • spring-boot List轉(zhuǎn)Page的方法步驟

    spring-boot List轉(zhuǎn)Page的方法步驟

    這篇文章主要介紹了spring-boot List轉(zhuǎn)Page的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • springboot如何使用MybatisPlus

    springboot如何使用MybatisPlus

    MyBatisPlus是一個(gè)強(qiáng)大的數(shù)據(jù)庫(kù)操作框架,其代碼生成器可以快速生成實(shí)體類(lèi)、映射文件等,本文介紹了如何導(dǎo)入MyBatisPlus相關(guān)依賴(lài),創(chuàng)建代碼生成器,并配置數(shù)據(jù)庫(kù)信息以逆向生成代碼,感興趣的朋友跟隨小編一起看看吧
    2024-09-09
  • SpringBoot項(xiàng)目使用MDC給日志增加唯一標(biāo)識(shí)的實(shí)現(xiàn)步驟

    SpringBoot項(xiàng)目使用MDC給日志增加唯一標(biāo)識(shí)的實(shí)現(xiàn)步驟

    本文介紹了如何在SpringBoot項(xiàng)目中使用MDC(Mapped?Diagnostic?Context)為日志增加唯一標(biāo)識(shí),以便于日志追蹤,通過(guò)創(chuàng)建日志攔截器、配置攔截器以及修改日志配置文件,可以實(shí)現(xiàn)這一功能,文章還提供了源碼地址,方便讀者學(xué)習(xí)和參考,感興趣的朋友一起看看吧
    2025-03-03
  • java實(shí)現(xiàn)文件上傳和下載

    java實(shí)現(xiàn)文件上傳和下載

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)文件上傳和下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • 從零實(shí)現(xiàn)一個(gè)簡(jiǎn)單的Spring Bean容器的代碼案例

    從零實(shí)現(xiàn)一個(gè)簡(jiǎn)單的Spring Bean容器的代碼案例

    Spring是一個(gè)非常流行的Java?Web開(kāi)發(fā)框架,它提供了強(qiáng)大的依賴(lài)注入、面向切面編程、聲明式事務(wù)管理等功能,為開(kāi)發(fā)者提供了高效、快速地構(gòu)建Web應(yīng)用程序的工具,在這篇文章中,咱們將一步一步地構(gòu)建一個(gè)簡(jiǎn)單的SpringBean容器,需要的朋友可以參考下
    2023-06-06
  • 淺談Spring的兩種配置容器

    淺談Spring的兩種配置容器

    這篇文章主要介紹了淺談Spring的兩種配置容器,介紹了其實(shí)現(xiàn)以及簡(jiǎn)單的實(shí)例,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-10-10
  • Mybatis collection查詢(xún)集合屬性報(bào)錯(cuò)的解決方案

    Mybatis collection查詢(xún)集合屬性報(bào)錯(cuò)的解決方案

    這篇文章主要介紹了Mybatis collection查詢(xún)集合屬性報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09

最新評(píng)論

江门市| 常山县| 大连市| 沈丘县| 报价| 上虞市| 乐平市| 四川省| 河源市| 灵山县| 西平县| 定陶县| 稷山县| 姚安县| 宝应县| 榆林市| 洪江市| 山西省| 浦北县| 漳州市| 盖州市| 芦山县| 扶余县| 台中县| 叙永县| 静乐县| 紫金县| 竹北市| 双鸭山市| 突泉县| 乐都县| 竹山县| 乌鲁木齐市| 故城县| 微山县| 玛曲县| 清徐县| 龙井市| 玉树县| 吉木萨尔县| 邹平县|