配置文件到底能写什么?怎么写?自动配置原理;
配置文件能配置的属性参照 : https://docs.spring.io/spring-boot/docs/1.5.9.RELEASE/reference/htmlsingle/#common-application-properties
1、自动配置原理:
1)、SpringBoot启动的时候加载主配置类SpringBootApplication.class,开启了自动配置功能 @EnableAutoConfiguration
2)、@EnableAutoConfiguration 作用:
//SpringBoot 1.5
@Import({
EnableAutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
//SpringBoot 2.0
@Import({
AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
-
利用EnableAutoConfigurationImportSelector给容器中导入一些组件?
-
可以查看selectImports()方法的内容;
-
List configurations = getCandidateConfigurations(annotationMetadata, attributes);获取候选的配置
-
SpringFactoriesLoader.loadFactoryNames() 扫描所有jar包类路径下 META-INF/spring.factories 把扫描到的这些文件的内容包装成properties对象 从properties中获取到EnableAutoConfiguration.class类(类名)对应的值,然后把他们添加在容器中
-
org.springframework.boot.autoconfigure.version.jar包中
将 类路径下 META-INF/spring.factories 里面配置的所有EnableAutoConfiguration的值加入到了容器中;
# 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,\
org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration,\
org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration,\
org.springframework.boot.autoconfigure.couchbase.CouchbaseAutoConfiguration,\
org.springframework.boot.autoconfigure.dao.PersistenceExceptionTranslationAutoConfiguration,\
org.springframework.bo