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

Spring Boot 自動配置的實現(xiàn)

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

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

再來看下, @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 的實現(xiàn)

@Override
  public String[] selectImports(AnnotationMetadata annotationMetadata) {
    if (!isEnabled(annotationMetadata)) {
      return NO_IMPORTS;
    }
    try {
      //1、 自動配置的元數(shù)據(jù) spring-autocomfigure-metadata.properties
      // 自動配置的開啟條件
      AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
          .loadMetadata(this.beanClassLoader);
      AnnotationAttributes attributes = getAttributes(annotationMetadata);
      // 獲取設(shè)置的自動配置列表 spring.factories
      List<String> configurations = getCandidateConfigurations(annotationMetadata,
          attributes);
      configurations = removeDuplicates(configurations);
      configurations = sort(configurations, autoConfigurationMetadata);
      // 獲取要排除的自動配置列表,可以通過 注解@EnableAutoConfiguration 的exclude和
       // 配置文件設(shè)置 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. 配置環(huán)境中是否存在特定的配置項  @ConditionalOnProperty
  4. 配置環(huán)境中指定的配置項是否存在指定的值

禁用配置

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

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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 淺談Java常見的排序算法

    淺談Java常見的排序算法

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

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

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

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

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

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

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

    springboot如何使用MybatisPlus

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

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

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

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

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

    從零實現(xiàn)一個簡單的Spring Bean容器的代碼案例

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

    淺談Spring的兩種配置容器

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

    Mybatis collection查詢集合屬性報錯的解決方案

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

最新評論

义乌市| 达日县| 玛纳斯县| 镇沅| 海林市| 兰西县| 玉门市| 磐石市| 小金县| 万安县| 恭城| 原平市| 台东县| 道真| 新津县| 天水市| 孟村| 滦南县| 靖西县| 临泉县| 克什克腾旗| 阳高县| 东阳市| 阿鲁科尔沁旗| 卫辉市| 根河市| 石景山区| 黔南| 西乌珠穆沁旗| 邹平县| 和林格尔县| 邵武市| 南木林县| 从江县| 商都县| 自贡市| 闽侯县| 开江县| 盘锦市| 江西省| 芦山县|