色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

SpringBoot自動裝配原理小結

瀏覽:3日期:2023-03-08 18:54:22
目錄springboot autoconfig的一些實驗SpringBoot autoconfig部分注解說明SpringBoot autoconfig原理starter

約定優于配置(Convention Over Configuration)是一種軟件設計范式,目的在于減少配置的數量或者降低理解難度,從而提升開發效率。

先總結一下結論:

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

springboot autoconfig的一些實驗

一個springboot工程,springbootautoconfig.test.config這個包和啟動類的包不再同一個路徑下,那么這個包下的注解等應該不會生效,bean也無法托管給spring管理

SpringBoot自動裝配原理小結

@Configuration//開啟配置@EnableConfigurationProperties(HelloProperties.class)//開啟使用映射實體對象@ConditionalOnClass(TestHello.class)//存在TestHello時初始化該配置類@ConditionalOnProperty//存在對應配置信息時初始化該配置類(prefix = 'zxp.hello',//存在配置前綴zxp.hellovalue = 'flag')public class HelloAutoConfiguration { @Autowired private HelloProperties helloProperties; @Bean//創建HelloService實體bean @ConditionalOnMissingBean(TestHello.class)//缺失HelloService實體bean時,初始化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')@Datapublic 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下創建META-INF路徑,并創建spring.factories文件

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

再試啟動又報錯了

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

原因是,如果沒有配置zxp.hello.flag,怎會報錯

@ConditionalOnProperty//存在對應配置信息時初始化該配置類(prefix = 'zxp.hello',//存在配置前綴hello value = 'flag'//開啟)

在application.properties中添加

zxp.hello.flag=2

成功了,訪問controller

name is null flag is 1SpringBoot autoconfig部分注解說明

@ConditionalOnXxx 可以根據條件來決定是否執行自動配置

@ConditionalOnBean:當SpringIoc容器內存在指定Bean的條件@ConditionalOnSingleCandidate:當指定Bean在SpringIoc容器內只有一個,或者雖然有多個但是指定首選的Bean@ConditionalOnMissingBean:當SpringIoc容器內不存在指定Bean的條件@ConditionalOnClass:當SpringIoc容器內存在指定Class的條件@ConditionalOnMissingClass:當SpringIoc容器內不存在指定Class的條件@ConditionalOnExpression:基于SpEL表達式作為判斷條件@ConditionalOnJava:基于JVM版本作為判斷條件@ConditionalOnJndi:在JNDI存在時查找指定的位置@ConditionalOnResource:類路徑是否有指定的值@ConditionalOnProperty:指定的屬性是否有指定的值@ConditionalOnNotWebApplication:當前項目不是Web項目的條件@ConditionalOnWebApplication:當前項目是Web項目的條件@AutoConfigureBefore@AutoConfigureAfter@AutoConfigureOrder

public @interface ConditionalOnProperty { String[] value() default {}; //數組,獲取對應property名稱的值,與name不可同時使用 String prefix() default '';//property名稱的前綴,可有可無 String[] name() default {};//數組,property完整名稱或部分名稱(可與prefix組合使用,組成完整的property名稱),與value不可同時使用 String havingValue() default '';//可與name組合使用,比較獲取到的屬性值與havingValue給定的值是否相同,相同才加載配置 boolean matchIfMissing() default false;//缺少該property時是否可以加載。如果為true,沒有該property也會正常加載;反之報錯 boolean relaxedNames() default true;//是否可以松散匹配,至今不知道怎么使用的 } SpringBoot autoconfig原理

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

AutoConfigurationImportSelector類中調用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包內的spring.factories文件內容

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

包含了所有spring為其增加的自動配置的bean配置,這些bean在滿足條件后會被加載到spring上下文中,從而實現了自動配置

eg:

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

會發現RabbitTemplate會報錯,為什么這里不存在的類卻不報錯呢? 1、這個jar編譯時這個類是有的,保證編譯能過 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

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

starter

starter就是整理了依賴的maven配置,主要指maven相關依賴配置到單獨的一個工程以避免引入過多的maven配置

以上就是SpringBoot自動裝配原理詳解的詳細內容,更多關于SpringBoot自動裝配原理的資料請關注好吧啦網其它相關文章!

標簽: Spring
相關文章:
主站蜘蛛池模板: 久久亚洲精品成人 | 欧美另类精品 | 久久一本色系列综合色 | 一级毛片免费观看视频 | 久久综合婷婷 | 成年人一级片 | 一区高清| 亚洲国产综合人成综合网站00 | 日韩毛片在线免费观看 | 久久久久久亚洲精品不卡 | 亚洲视频在线观看视频 | 国产精品亚洲片夜色在线 | 真实国产乱子伦高清 | 国产一区二区日韩欧美在线 | 波多野结衣免费视频观看 | 亚洲自拍高清 | 日本欧美一区二区三区片 | 美国毛片亚洲社区在线观看 | 国产三区二区 | 国产成年女一区二区三区 | 中文字幕天堂最新版在线网 | 国产亚洲精品一区999 | 在线观看亚洲欧美 | 黄毛片一级毛片 | 波多野结衣免费观看视频 | 成年女人免费视频播放成年m | 国产成人a毛片在线 | 欧美激情一区二区亚洲专区 | 欧美在线不卡 | 九九精品免费观看在线 | 免费在线视频成人 | 黄色作爱视频 | 亚洲天堂久久精品 | 欧美三级成版人版在线观看 | 国产a级一级久久毛片 | 精品国产一区二区三区在线观看 | 日韩欧美在线精品 | 亚洲理论欧美理论在线观看 | 全部在线播放免费毛片 | 手机在线观看精品国产片 | 国产 日韩 欧美 在线 |