springboot学习日记03---配置继续
上一篇读取配置文件笔记当中,是读取全局的那个application.yml但是如果很多配置都写在那里,整个文件就很臃肿。于是还可以使用以**解,读取指定配置文件:
@PropertySource(value={"classoath:xxxx})
表示读取类路径下的某一文件
@importResource(location={"classoath:xxxx})
用于读取自定义配置文件,并且使其生效,如果不用这个注解,则配置文件不会生效
配置类
使用配置类的方式,替代之前的配置文件
在类定义上方添加@Configuration
在方法上添加@Bean注解
方法的名称就是spring容器的bean名称
Profile
多环境配置文件:
默认启用 application.properties
假设想启用application-uat.properties
则在 application.properties 中加入 spring.profiles.active=uat
yml多环境
yml是可以采用文档块的形式
定格三个横杠 ---之后文档会被拆分,如图
加入属性spring:
spring: profiles: xxx
然后在第一个文档块标记**哪个文档模块
3
使用命令行的方式
maven 打包后启动指定配置文件
java -jar xxxxx.jar --spring.profiles.active=dev
那么问题来了:配置文件能配置的属性,有什么呢?参考spring文档 Common application properties
自动配置原理,在使用注解
@SpringBootApplication 的时候,里面有一个注解
@EnableAutoConfiguration
中父类的selectImport方法
List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
扫描类路径下的META-INF/spring.factories
扫描的文件包装成properties对象从该对象中获取EnableAutoConfiguration中的值加入到容器