一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - Spring Boot 自動配置的實現

Spring Boot 自動配置的實現

2021-05-25 12:14David_jim Java教程

這篇文章主要介紹了Spring Boot 自動配置的實現,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

spring boot 自動配置

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@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)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 的實現

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@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 文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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

?
1
2
3
4
5
6
7
8
9
10
11
@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 接口來作條件選擇

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
@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 配置

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.jianshu.com/p/7261521394e7

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产卡一卡二卡三卡四 | 国语自产拍在线播放不卡 | 成人国产在线播放 | 国产综合成色在线视频 | 久久精品国产免费播高清无卡 | 欧美性色黄大片四虎影视 | 美国大片成人性网 | 久久热这里面只有精品 | h玉足嫩脚嗯啊白丝 | 暖暖免费高清完整版观看日本 | a级成人毛片免费图片 | 亚洲AV久久无码精品九九软件 | 精品无人区麻豆乱码1区2 | 男女污网站 | 国产免费一区二区三区免费视频 | 放荡的女老板bd中文字幕 | 国产精品每日在线观看男人的天堂 | 大学生初次破苞免费视频 | 成人影院免费在线观看 | 亚洲视频在线一区二区三区 | 91香蕉视频在线观看 | 国产a高清 | 99午夜 | 国产91无毒不卡在线观看 | 校花在公车上被内射好舒 | 精品久久久久久无码人妻国产馆 | 91短视频版高清在线观看免费 | 欧美亚洲影院 | 久久久久久88色偷偷 | 妹妹骑上来蹭着蹭着就射了 | 国产一卡 | 亚洲第一区二区快射影院 | 国产日韩欧美综合在线 | 国产一区二区免费福利片 | 亚洲欧美综合人成野草 | 久久精品亚洲牛牛影视 | 国产精品1区2区 | 欧美另类69xxx| 95在线观看精品视频 | 国产成人久久精品区一区二区 | 美女的让男人桶爽30分钟的 |