Spring Boot自動配置原理、實戰
Spring Boot自動配置原理
Spring Boot的自動配置註解是@EnableAutoConfiguration,從上面的@Import的類可以找到下面自動載入自動配置的對映。
org.springframework.core.io.support.SpringFactoriesLoader.loadFactoryNames(Class<?>, ClassLoader)
public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) { String factoryClassName = factoryClass.getName(); try { Enumeration<URL> urls = (classLoader != null ? classLoader.getResources(FACTORIES_RESOURCE_LOCATION) : lassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION)); List<String> result = new ArrayList<String>(); while (urls.hasMoreElements()) { URL url = urls.nextElement(); Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url)); String factoryClassNames = properties.getProperty(factoryClassName); result.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(factoryClassNames))); } return result; } catch (IOException ex) { throw new IllegalArgumentException("Unable to load [" + factoryClass.getName() + "] factories from location [" + FACTORIES_RESOURCE_LOCATION + "]", ex); } }
這個方法會載入類路徑及所有jar包下META-INF/spring.factories配置中對映的自動配置的類。
/** * The location to look for factories. * <p>Can be present in multiple JAR files. */ public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
檢視Spring Boot自帶的自動配置的包:spring-boot-autoconfigure-1.5.6.RELEASE.jar,開啟其中的META-INF/spring.factories檔案會找到自動配置的對映。
# Auto Configure 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,\ ...
再來看看資料來源自動配置的實現註解
@Configuration @ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class }) @EnableConfigurationProperties(DataSourceProperties.class) @Import({ Registrar.class, DataSourcePoolMetadataProvidersConfiguration.class }) public class DataSourceAutoConfiguration { ...
@Configuration,@ConditionalOnClass就是自動配置的核心,首先它得是一個配置檔案,其次根據類路徑下是否有這個類去自動配置。
自動配置實戰
所以,瞭解了自動配置的原理,來自己實現一個自動配置的玩意其實很簡單。
新增配置類:
import org.slf4j.Logger; import org.springframework.context.EnvironmentAware; import org.springframework.core.env.Environment; import com.oceanpayment.common.utils.logger.LoggerUtils; public class EnvConfig implements EnvironmentAware { private final Logger logger = LoggerUtils.getLogger(this); private Environment env; public String getStringValue(String key) { return env.getProperty(key); } public Long getLongValue(String key) { String value = getStringValue(key); try { return Long.parseLong(value); } catch (Exception e) { logger.error("字串轉換Long失敗:{} = {}", key, value); } return 0L; } public int getIntValue(String key) { return getLongValue(key).intValue(); } @Override public void setEnvironment(Environment environment) { this.env = environment; } }
新增自動配置類:
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.PropertyResolver; @Configuration @ConditionalOnClass(PropertyResolver.class) public class EnvAutoConfig { @Bean public EnvConfig envConfig() { return new EnvConfig(); } }
建立META-INF/spring.factories檔案,新增自動配置對映:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\\ com.oceanpayment.common.config.env.EnvAutoConfig
這樣就搞定了。
檢視自動配置報告
怎麼檢視自己加的自動配置類有沒有被載入,或者檢視所有自動配置啟用的和未啟用的可以通過以下幾種試檢視。
spring-boot:run執行的在對話方塊Enviroment中加入debug=true變數
java -jar xx.jar --debug
main方法執行,在VM Argumanets加入-Ddebug
直接在application檔案中加入debug=true
如果整合了spring-boot-starter-actuator監控,通過autoconfig端點也可以檢視。
啟動後會在控制檯看到以下自動配置報告資訊:
========================= AUTO-CONFIGURATION REPORT ========================= Positive matches: ----------------- AopAutoConfiguration matched: - @ConditionalOnClass found required classes 'org.springframework.context.annotation.EnableAspectJAutoProxy', 'org.aspectj.lang.annotation.Aspect', 'org.aspectj.lang.reflect.Advice'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition) - @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition) ... EnvAutoConfig matched: - @ConditionalOnClass found required class 'org.springframework.core.env.PropertyResolver'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition) ErrorMvcAutoConfiguration matched: - @ConditionalOnClass found required classes 'javax.servlet.Servlet', 'org.springframework.web.servlet.DispatcherServlet'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition) - @ConditionalOnWebApplication (required) found StandardServletEnvironment (OnWebApplicationCondition) ErrorMvcAutoConfiguration#basicErrorController matched: - @ConditionalOnMissingBean (types: org.springframework.boot.autoconfigure.web.ErrorController; SearchStrategy: current) did not find any beans (OnBeanCondition) ... Negative matches: ----------------- ActiveMQAutoConfiguration: Did not match: - @ConditionalOnClass did not find required classes 'javax.jms.ConnectionFactory', 'org.apache.activemq.ActiveMQConnectionFactory' (OnClassCondition) AopAutoConfiguration.JdkDynamicAutoProxyConfiguration: Did not match: - @ConditionalOnProperty (spring.aop.proxy-target-class=false) found different value in property 'proxy-target-class' (OnPropertyCondition) ArtemisAutoConfiguration: Did not match: - @ConditionalOnClass did not find required classes 'javax.jms.ConnectionFactory', 'org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory' (OnClassCondition) BatchAutoConfiguration: Did not match: - @ConditionalOnClass did not find required class 'org.springframework.batch.core.launch.JobLauncher' (OnClassCondition) ...
Positive matches:已經啟用的自動配置
Negative matches:未啟用的自動配置
從報告中看到自己新增的EnvAutoConfig已經自動配置了。
相關文章
- Spring Boot 自動配置原理Spring Boot
- Spring Boot核心原理-自動配置Spring Boot
- Spring Boot自動配置原理與實踐(一)Spring Boot
- Spring Boot自動配置原理與實踐(二)Spring Boot
- 程式設計實戰篇——Spring Boot 自動配置實現程式設計Spring Boot
- (第四講)Spring Boot 自動化配置原理解析Spring Boot
- Spring Boot 自動裝配原理Spring Boot
- Spring Boot 自動配置的原理、核心註解以及利用自動配置實現了自定義 Starter 元件Spring Boot元件
- Spring Boot 自動配置之@EnableAutoConfigurationSpring Boot
- Spring Boot 自動配置 原始碼分析Spring Boot原始碼
- Spring Boot 外部化配置實戰解析Spring Boot
- # Spring Boot 外部化配置實戰解析Spring Boot
- Spring Boot外部化配置實戰解析Spring Boot
- [spring-rabbit]自動配置原理Spring
- 深度剖析Spring Boot自動裝配機制實現原理Spring Boot
- Spring Boot中自動配置Autoconfigure詳解Spring Boot
- Spring Boot自動配置原理懂後輕鬆寫一個自己的starterSpring Boot
- 建立屬於自己的 Spring Boot 自動配置Spring Boot
- Spring Boot 自動配置(auto-configurtion) 揭祕Spring Boot
- Spring Boot 自動配置之組合註解Spring Boot
- Spring Boot 自動配置之條件註解Spring Boot
- Spring Boot實戰系列(7)整合Consul配置中心Spring Boot
- 【Spring Boot 使用記錄】kafka自動配置和自定義配置Spring BootKafka
- Spring Boot 自動配置之@Enable* 與@Import註解Spring BootImport
- Spring Boot 2 實戰:常用讀取配置的方式Spring Boot
- Spring Boot功能實戰Spring Boot
- 深入淺出Spring Boot 起步依賴和自動配置Spring Boot
- Spring Boot 基於註解驅動原始碼分析--自動配置Spring Boot原始碼
- Jenkins自動部署spring bootJenkinsSpring Boot
- Spring Boot:Spring Boot配置MybatisSpring BootMyBatis
- Spring Boot:Spring Boot配置SwaggerSpring BootSwagger
- Spring Boot實戰:模板引擎Spring Boot
- Spring Boot 整合 Elasticsearch 實戰Spring BootElasticsearch
- 使用@AutoConfigureBefore、After、Order調整Spring Boot自動配置順序Spring Boot
- spring boot+bootstrap實現動態輪播圖實戰Spring Boot
- SpringBoot | 自動配置原理Spring Boot
- springboot 自動配置原理Spring Boot
- SpringBoot自動配置原理Spring Boot