自動配置原理
配置文件到底能寫什么?怎么寫?自動配置原理;
參考:https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/htmlsingle/#common-application-properties
配置文件能配置的屬性參照
1、自動配置原理:
1)、SpringBoot啟動的時候加載主配置類,開啟了自動配置功能 @EnableAutoConfiguration
2)、@EnableAutoConfiguration 作用:
利用EnableAutoConfigurationImportSelector給容器中導入一些組件?
可以查看selectImports()方法的內容;
List configurations = getCandidateConfigurations(annotationMetadata, attributes);獲取候選的配置
?注:? ? ??
SpringFactoriesLoader.loadFactoryNames()
掃描所有jar包類路徑下??META‐INF/spring.factories
把掃描到的這些文件的內容包裝成properties對象
從properties中獲取到EnableAutoConfiguration.class類(類名)對應的值,然后把他們添加在容器
中
?
?將 類路徑下 META-INF/spring.factories 里面配置的所有EnableAutoConfiguration的值加入到了容器中;
?
每一個這樣的 xxxAutoConfiguration類都是容器中的一個組件,都加入到容器中;用他們來做自動配置;
?
每一個自動配置類進行自動配置功能;
?
以HttpEncodingAutoConfiguration(Http編碼自動配置)為例解釋自動配置原理;
?
??
@Configuration //表示這是一個配置類,以前編寫的配置文件一樣,也可以給容器中添加組件@EnableConfigurationProperties(HttpEncodingProperties.class) //啟動指定類的ConfigurationProperties功能;將配置文件中對應的值和HttpEncodingProperties綁定起來;并把HttpEncodingProperties加入到ioc容器中@ConditionalOnWebApplication //Spring底層@Conditional注解(Spring注解版),根據不同的條件,如果滿足指定的條件,整個配置類里面的配置就會生效; 判斷當前應用是否是web應用,如果是,當前配置類生效@ConditionalOnClass(CharacterEncodingFilter.class) //判斷當前項目有沒有這個類CharacterEncodingFilter;SpringMVC中進行亂碼解決的過濾器;@ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", matchIfMissing =true) //判斷配置文件中是否存在某個配置 spring.http.encoding.enabled;如果不存在,判斷也是成立的 //即使我們配置文件中不配置pring.http.encoding.enabled=true,也是默認生效的;public class HttpEncodingAutoConfiguration {//他已經和SpringBoot的配置文件映射了private final HttpEncodingProperties properties;//只有一個有參構造器的情況下,參數的值就會從容器中拿public HttpEncodingAutoConfiguration(HttpEncodingProperties properties) {this.properties = properties;}@Bean //給容器中添加一個組件,這個組件的某些值需要從properties中獲取@ConditionalOnMissingBean(CharacterEncodingFilter.class) //判斷容器沒有這個組件?public CharacterEncodingFilter characterEncodingFilter() {CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();filter.setEncoding(this.properties.getCharset().name());filter.setForceRequestEncoding(this.properties.shouldForce(Type.REQUEST));filter.setForceResponseEncoding(this.properties.shouldForce(Type.RESPONSE));return filter;} }
?
?
根據當前不同的條件判斷,決定這個配置類是否生效?
一但這個配置類生效;這個配置類就會給容器中添加各種組件;這些組件的屬性是從對應的properties類中獲取
的,這些類里面的每一個屬性又是和配置文件綁定的;
?
?所有在配置文件中能配置的屬性都是在xxxxProperties類中封裝者‘;配置文件能配置什么就可以參照某個功
能對應的這個屬性類
?
@ConfigurationProperties(prefix?=?"spring.http.encoding")??//從配置文件中獲取指定的值和bean的屬 性進行綁定 public?class?HttpEncodingProperties?{public?static?final?Charset?DEFAULT_CHARSET?=?Charset.forName("UTF‐8");
?
?
精髓:
1)、SpringBoot啟動會加載大量的自動配置類
2)、我們看我們需要的功能有沒有SpringBoot默認寫好的自動配置類;
3)、我們再來看這個自動配置類中到底配置了哪些組件;(只要我們要用的組件有,我們就不需要再來配置了)
4)、給容器中自動配置類添加組件的時候,會從properties類中獲取某些屬性。我們就可以在配置文件中指定這
些屬性的值;
?
?
xxxxAutoConfigurartion:自動配置類;
給容器中添加組件
xxxxProperties:封裝配置文件中相關屬性;
?
?
細節
1、@Conditional派生注解(Spring注解版原生的@Conditional作用)
? ? ? 作用:必須是@Conditional指定的條件成立,才給容器中添加組件,配置配里面的所有內容才生效;
?
@Conditional擴展注解 作用(判斷是否滿足當前指定條件)
@ConditionalOnJava 系統的java版本是否符合要求
@ConditionalOnBean 容器中存在指定Bean;
@ConditionalOnMissingBean 容器中不存在指定Bean;
@ConditionalOnExpression 滿足SpEL表達式指定
@ConditionalOnClass 系統中有指定的類
@ConditionalOnMissingClass 系統中沒有指定的類
@ConditionalOnSingleCandidate 容器中只有一個指定的Bean,或者這個Bean是首選Bean
@ConditionalOnProperty 系統中指定的屬性是否有指定的值
@ConditionalOnResource 類路徑下是否存在指定資源文件
@ConditionalOnWebApplication 當前是web環境
@ConditionalOnNotWebApplication 當前不是web環境
@ConditionalOnJndi JNDI存在指定項
?底層都是?@Conditional
自動配置類必須在一定的條件下才能生效;
我們怎么知道哪些自動配置類生效?
我們可以通過在 yml中啟用 debug=true屬性;
這樣就可以讓控制臺打印自動配置報告,這樣我們就可以很方便的知道哪些自動配置類生效;
?會打印日志:
=========================
AUTO‐CONFIGURATION?REPORT
=========================
Positive?matches:(自動配置類啟用的)
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
???DispatcherServletAutoConfiguration?matched:
?‐?@ConditionalOnClass?found?required?class
'org.springframework.web.servlet.DispatcherServlet';?@ConditionalOnMissingClass?did?not?find
unwanted?class?(OnClassCondition)
??????‐?@ConditionalOnWebApplication?(required)?found?StandardServletEnvironment
(OnWebApplicationCondition)
???????
???
Negative?matches:(沒有啟動,沒有匹配成功的自動配置類)
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
???ActiveMQAutoConfiguration:
??????Did?not?match:
?????????‐?@ConditionalOnClass?did?not?find?required?classes?'javax.jms.ConnectionFactory',
'org.apache.activemq.ActiveMQConnectionFactory'?(OnClassCondition)
???AopAutoConfiguration:
??????Did?not?match:
?????????‐?@ConditionalOnClass?did?not?find?required?classes
'org.aspectj.lang.annotation.Aspect',?'org.aspectj.lang.reflect.Advice'?(OnClassCondition)
?
?
近期項目做的案例
?
yml:
?
?
?
yml:# 自定義配置參數
default:job-thread-pool:corePoolSize: 10maximumPoolSize: 200keepAliveTime: 0nameFormat: Job-Thread-%dqueueCapacity: 1024
?
對應的類去讀取成Bean:
@Getter @Setter @ConfigurationProperties(prefix = "default.job-thread-pool") public class JobThreadPoolConfig {private int corePoolSize;private int maximumPoolSize;private long keepAliveTime;private String nameFormat;private int queueCapacity; }
config中去獲取配置信息:
@Configuration @Slf4j @EnableConfigurationProperties({JobThreadPoolConfig.class}) public class CommonConfiguration {@Autowiredprivate JobThreadPoolConfig jobThreadPoolConfig;
?