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

SpringBoot自動(dòng)裝配原理小結(jié)

 更新時(shí)間:2021年05月28日 08:29:31   作者:XPacific  
Spring Boot主要作用就是簡(jiǎn)化Spring應(yīng)用的開(kāi)發(fā),開(kāi)發(fā)者只需要通過(guò)少量代碼就可以創(chuàng)建一個(gè)Spring應(yīng)用,而達(dá)到這一目的最核心的思想就是約定優(yōu)于配置。

約定優(yōu)于配置(Convention Over Configuration)是一種軟件設(shè)計(jì)范式,目的在于減少配置的數(shù)量或者降低理解難度,從而提升開(kāi)發(fā)效率。

先總結(jié)一下結(jié)論:

springboot通過(guò)spring.factories能把main方法所在類路徑以外的bean自動(dòng)加載,其目的就是為了幫助自動(dòng)配置bean,減輕配置量

springboot autoconfig的一些實(shí)驗(yàn)

一個(gè)springboot工程,springbootautoconfig.test.config這個(gè)包和啟動(dòng)類的包不再同一個(gè)路徑下,那么這個(gè)包下的注解等應(yīng)該不會(huì)生效,bean也無(wú)法托管給spring管理

@Configuration//開(kāi)啟配置
@EnableConfigurationProperties(HelloProperties.class)//開(kāi)啟使用映射實(shí)體對(duì)象
@ConditionalOnClass(TestHello.class)//存在TestHello時(shí)初始化該配置類
@ConditionalOnProperty//存在對(duì)應(yīng)配置信息時(shí)初始化該配置類
(
        prefix = "zxp.hello",//存在配置前綴zxp.hello
        value = "flag"
)
public class HelloAutoConfiguration {
    @Autowired
    private HelloProperties helloProperties;

    @Bean//創(chuàng)建HelloService實(shí)體bean
    @ConditionalOnMissingBean(TestHello.class)//缺失HelloService實(shí)體bean時(shí),初始化HelloService并添加到SpringIoc
    public TestHello helloService()
    {
        System.out.println(">>>The TestHello Not Found,Execute Create New Bean.");
        TestHello testHello = new TestHello(helloProperties.getName(),helloProperties.getFlag());
        return testHello;
    }
}
@ConfigurationProperties(prefix = "zxp.hello")
@Data
public class HelloProperties {
    private String name;
    private String flag;
}
public class TestHello {
    String name;
    String flag;

    public TestHello(String name, String flag) {
        this.name = name;
        this.flag = flag;
    }

    public String print(){
        String msg = "name is "+name + "  " + "flag is "+flag;
        System.out.println(msg);
        return msg;
    }
}

在resources下創(chuàng)建META-INF路徑,并創(chuàng)建spring.factories文件

#配置自定義Starter的自動(dòng)化配置
org.springframework.boot.autoconfigure.EnableAutoConfiguration=springbootautoconfig.test.config.HelloAutoConfiguration

再試啟動(dòng)又報(bào)錯(cuò)了

- Bean method 'helloService' not loaded because @ConditionalOnProperty (zxp.hello) did not find property 'flag'

原因是,如果沒(méi)有配置zxp.hello.flag,怎會(huì)報(bào)錯(cuò)

@ConditionalOnProperty//存在對(duì)應(yīng)配置信息時(shí)初始化該配置類
(
        prefix = "zxp.hello",//存在配置前綴hello  
        value = "flag"http://開(kāi)啟
)

在application.properties中添加

zxp.hello.flag=2

成功了,訪問(wèn)controller

name is null flag is 1

SpringBoot autoconfig部分注解說(shuō)明

@ConditionalOnXxx 可以根據(jù)條件來(lái)決定是否執(zhí)行自動(dòng)配置

@ConditionalOnBean:當(dāng)SpringIoc容器內(nèi)存在指定Bean的條件
@ConditionalOnSingleCandidate:當(dāng)指定Bean在SpringIoc容器內(nèi)只有一個(gè),或者雖然有多個(gè)但是指定首選的Bean
@ConditionalOnMissingBean:當(dāng)SpringIoc容器內(nèi)不存在指定Bean的條件

@ConditionalOnClass:當(dāng)SpringIoc容器內(nèi)存在指定Class的條件
@ConditionalOnMissingClass:當(dāng)SpringIoc容器內(nèi)不存在指定Class的條件
@ConditionalOnExpression:基于SpEL表達(dá)式作為判斷條件
@ConditionalOnJava:基于JVM版本作為判斷條件
@ConditionalOnJndi:在JNDI存在時(shí)查找指定的位置
@ConditionalOnResource:類路徑是否有指定的值
@ConditionalOnProperty:指定的屬性是否有指定的值

@ConditionalOnNotWebApplication:當(dāng)前項(xiàng)目不是Web項(xiàng)目的條件
@ConditionalOnWebApplication:當(dāng)前項(xiàng)目是Web項(xiàng)目的條件

@AutoConfigureBefore
@AutoConfigureAfter
@AutoConfigureOrder
public @interface ConditionalOnProperty {
    String[] value() default {}; //數(shù)組,獲取對(duì)應(yīng)property名稱的值,與name不可同時(shí)使用  
    String prefix() default "";//property名稱的前綴,可有可無(wú)  
    String[] name() default {};//數(shù)組,property完整名稱或部分名稱(可與prefix組合使用,組成完整的property名稱),與value不可同時(shí)使用  
    String havingValue() default "";//可與name組合使用,比較獲取到的屬性值與havingValue給定的值是否相同,相同才加載配置  
    boolean matchIfMissing() default false;//缺少該property時(shí)是否可以加載。如果為true,沒(méi)有該property也會(huì)正常加載;反之報(bào)錯(cuò)  
    boolean relaxedNames() default true;//是否可以松散匹配,至今不知道怎么使用的  
} 

SpringBoot autoconfig原理

springboot啟動(dòng)類注解@SpringBootApplication引入@EnableAutoConfiguration又引入@Import(AutoConfigurationImportSelector.class)

AutoConfigurationImportSelector類中調(diào)用SpringFactoriesLoader.loadFactoryNames 方法掃描了所有JAR包的META-INF/spring.factories,如下代碼:

protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
   List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
         getBeanClassLoader());
   Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "
         + "are using a custom packaging, make sure that file is correct.");
   return configurations;
}
public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
    ……
    Enumeration<URL> urls = (classLoader != null ?
      classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
      ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
    result = new LinkedMultiValueMap<>();
    while (urls.hasMoreElements()) {

spring-boot-autoconfigure包內(nèi)的spring.factories文件內(nèi)容

……
work.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
……

包含了所有spring為其增加的自動(dòng)配置的bean配置,這些bean在滿足條件后會(huì)被加載到spring上下文中,從而實(shí)現(xiàn)了自動(dòng)配置

eg:

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ RabbitTemplate.class, Channel.class })
@EnableConfigurationProperties(RabbitProperties.class)
@Import(RabbitAnnotationDrivenConfiguration.class)
public class RabbitAutoConfiguration {

會(huì)發(fā)現(xiàn)RabbitTemplate會(huì)報(bào)錯(cuò),為什么這里不存在的類卻不報(bào)錯(cuò)呢? 1、這個(gè)jar編譯時(shí)這個(gè)類是有的,保證編譯能過(guò) 2、看下ConditionalOnClass注解的注釋

The classes that must be present. Since this annotation is parsed by loading class bytecode, > it is safe to specify classes here that may ultimately not be on the classpath, only if this annotation is directly on the affected component and not if this annotation is used as a composed, meta-annotation. In order to use this annotation as a meta-annotation, only use the name attribute. Returns: the classes that must be present

必須出現(xiàn)的類。由于此注釋是通過(guò)加載類字節(jié)碼來(lái)解析的,因此在此處指定最終可能不在類路徑上的類是安全的,前提是此注釋直接位于受影響的組件上,而不是將此注釋用作組合的元注釋。要將此注釋用作元注釋,請(qǐng)僅使用name屬性。

starter

starter就是整理了依賴的maven配置,主要指maven相關(guān)依賴配置到單獨(dú)的一個(gè)工程以避免引入過(guò)多的maven配置

以上就是SpringBoot自動(dòng)裝配原理詳解的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot自動(dòng)裝配原理的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java用itextpdf導(dǎo)出PDF方法(通俗易懂)

    Java用itextpdf導(dǎo)出PDF方法(通俗易懂)

    因?yàn)轫?xiàng)目需要導(dǎo)出PDF文件,所以去找了一下能夠生成PDF的java工具,這篇文章主要給大家介紹了關(guān)于Java用itextpdf導(dǎo)出PDF的相關(guān)資料,文中介紹的方法通俗易懂,需要的朋友可以參考下
    2023-07-07
  • Spring如何正確注入集合類型

    Spring如何正確注入集合類型

    這篇文章主要介紹了Spring如何正確注入集合類型,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • java 中枚舉類enum的values()方法的詳解

    java 中枚舉類enum的values()方法的詳解

    這篇文章主要介紹了java 中枚舉類enum的values()方法的詳解的相關(guān)資料,希望通過(guò)本文大家能夠掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-09-09
  • java線程池不同場(chǎng)景下使用示例經(jīng)驗(yàn)總結(jié)

    java線程池不同場(chǎng)景下使用示例經(jīng)驗(yàn)總結(jié)

    這篇文章主要為大家介紹了java線程池不同場(chǎng)景如何使用的示例源碼及經(jīng)驗(yàn)總結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-03-03
  • SpringBoot集成Dubbo啟用gRPC協(xié)議

    SpringBoot集成Dubbo啟用gRPC協(xié)議

    這篇文章主要介紹了SpringBoot集成Dubbo啟用gRPC協(xié)議,以及與原生 gRPC 在代碼編寫(xiě)過(guò)程中的區(qū)別。感興趣的同學(xué)可以參考閱讀
    2023-04-04
  • 淺談java中定義泛型類和定義泛型方法的寫(xiě)法

    淺談java中定義泛型類和定義泛型方法的寫(xiě)法

    下面小編就為大家?guī)?lái)一篇淺談java中定義泛型類和定義泛型方法的寫(xiě)法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-02-02
  • springboot中設(shè)置定時(shí)任務(wù)的三種方法小結(jié)

    springboot中設(shè)置定時(shí)任務(wù)的三種方法小結(jié)

    在我們開(kāi)發(fā)項(xiàng)目過(guò)程中,經(jīng)常需要定時(shí)任務(wù)來(lái)幫助我們來(lái)做一些內(nèi)容,本文介紹了springboot中設(shè)置定時(shí)任務(wù)的三種方法,主要包括@Scheduled注解,Quartz框架和xxl-job框架的實(shí)現(xiàn),感興趣的可以了解一下
    2023-12-12
  • 詳解SpringBoot目錄結(jié)構(gòu)劃分

    詳解SpringBoot目錄結(jié)構(gòu)劃分

    代碼目錄結(jié)構(gòu)是一個(gè)在項(xiàng)目開(kāi)發(fā)中非常重要的部分,本文主要介紹了詳解SpringBoot目錄結(jié)構(gòu)劃分,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-08-08
  • 用Java將字符串的首字母轉(zhuǎn)換大小寫(xiě)

    用Java將字符串的首字母轉(zhuǎn)換大小寫(xiě)

    在項(xiàng)目開(kāi)發(fā)的時(shí)候會(huì)需要統(tǒng)一字符串的格式,比如首字母要求統(tǒng)一大寫(xiě)或小寫(xiě),那用Java如何實(shí)現(xiàn)這一功能?下面一起來(lái)學(xué)習(xí)學(xué)習(xí)。
    2016-08-08
  • Spring?cloud?Hystrix注解初始化源碼過(guò)程解讀

    Spring?cloud?Hystrix注解初始化源碼過(guò)程解讀

    這篇文章主要為大家介紹了Hystrix初始化部分,我們從源碼的角度分析一下@EnableCircuitBreaker以及@HystrixCommand注解的初始化過(guò)程,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2023-12-12

最新評(píng)論

图木舒克市| 紫金县| 抚顺县| 习水县| 宜兰县| 红桥区| 镇雄县| 祁连县| 孟村| 华池县| 新邵县| 达州市| 弋阳县| 文成县| 田阳县| 大丰市| 华蓥市| 玉门市| 高陵县| 黄浦区| 贡嘎县| 延安市| 广西| 垣曲县| 沂源县| 岳池县| 丹江口市| 中卫市| 柯坪县| 华宁县| 安龙县| 滦平县| 全南县| 商丘市| 河东区| 裕民县| 四会市| 长海县| 陆丰市| 容城县| 泾阳县|