SpringBoot使用自動配置xxxAutoConfiguration
常用的類:
- @ConditionalOnProperty(name = "use.redis.session.store", havingValue = "true")
- @ConditionalOnClass(Session.class)
- @AutoConfigureAfter(RedisAutoConfiguration.class)
- @ConditionalOnWebApplication
- @ConditionalOnMissingBean(RedisHttpSessionConfiguration.class)
- @SpringBootApplication(exclude ={SessionAutoConfiguration.class})
舉例:以MybatisAutoConfiguration為例
- 1. 在jar包中/META-INF/spring.factories中配置
org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
spring在啟動時會去自動加載這個bean - 2. MybatisAutoConfiguration.java上加上@Configuration
- 3. 如果需要通過自定義構(gòu)造函數(shù)來構(gòu)造bean,則加上構(gòu)造函數(shù)
public MybatisAutoConfiguration(MybatisProperties properties,
ObjectProvider<Interceptor[]> interceptorsProvider,
ResourceLoader resourceLoader,
ObjectProvider<DatabaseIdProvider> databaseIdProvider,
ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider) {
this.properties = properties;
this.interceptors = interceptorsProvider.getIfAvailable();
this.resourceLoader = resourceLoader;
this.databaseIdProvider = databaseIdProvider.getIfAvailable();
this.configurationCustomizers = configurationCustomizersProvider.getIfAvailable();
}
構(gòu)造函數(shù)中依賴的Bean,Spring會自動從Spring beanFactory容器中去找到適配的bean來傳入
參數(shù):
- MybatisProperties properties --> 依賴MybatisProperties 這個bean,而MybatisProperties 是一個@ConfigurationProperties。則加上@EnableConfigurationProperties(MybatisProperties.class),讓bean MybatisProperties 先加載
- ObjectProvider<Interceptor[]> interceptorsProvider --> 依賴 org.apache.ibatis.plugin.Interceptor[],但又不確定 Interceptor 的bean是否存在,則通過ObjectProvider<Interceptor[]> 去獲取,interceptorsProvider.getIfAvailable()
- ResourceLoader resourceLoader --> 依賴 bean ResourceLoader,直接在 Spring 容器中找到相應(yīng)的 bean
- ObjectProvider<DatabaseIdProvider> databaseIdProvider --> 同上
- ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider --> 同上
附:斷點打在MybatisAutoConfiguration 的構(gòu)造函數(shù)上,查看調(diào)用棧,便可以看出端倪
以上所述是小編給大家介紹的SpringBoot使用自動配置xxxAutoConfiguration,希望對大家有所幫助。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
java使用淘寶API讀寫json實現(xiàn)手機歸屬地查詢功能代碼
本文介紹java使用淘寶API讀寫json實現(xiàn)手機歸屬地查詢功能,代碼簡單,大家可以參考使用2013-11-11
springboot?項目啟動后無日志輸出直接結(jié)束的解決
這篇文章主要介紹了springboot?項目啟動后無日志輸出直接結(jié)束的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
springboot webflux 過濾器(使用RouterFunction實現(xiàn))
這篇文章主要介紹了springboot webflux 過濾器(使用RouterFunction實現(xiàn)),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Java源碼解析之SortedMap和NavigableMap
今天帶大家來學(xué)習(xí)Java SortedMap和NavigableMap,文中有非常詳細的代碼示例,對正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下2021-05-05
Mybatis配置之<environments>配置元素詳解
這篇文章主要介紹了Mybatis配置之<environments>配置元素,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
Java?ArrayList遍歷foreach與iterator時remove的區(qū)別
這篇文章主要介紹了Java?ArrayList遍歷foreach與iterator時remove的區(qū)別,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-07-07
MyBatis的collection和association的使用解讀
這篇文章主要介紹了MyBatis的collection和association的使用解讀2023-12-12

