1、application-profile功能
- 快速的环境切换,定义多个配置文件------如:生产时的配置文件、测试的配置文件
- 如何不同情况下,使用不同的配置文件??
- 默认配置文件 application.yaml;任何时候都会加载
- 指定环境配置文件 application-{env}.yaml env为环境标识
- 激活指定环境
- 默认配置文件激活 spring.profiles.active=环境标识
- 命令行激活:java -jar xxx.jar --spring.profiles.active=环境标识
- 修改配置文件的任意值,命令行配置的优先
- 默认配置与环境配置同时生效
- 同名配置项,profile配置优先
2、@Profile的条件装配功能
- 在配置类 或者 组件上面添加 @profile("环境标识")
- 作用:
- 当对应的环境生效的时候,相应的组件或者配置才会生效
- 也可以跟一个环境标识的数组
proxyBeanMethods = false)
("production") //当加载application-production.yaml配置文件的时候,才会生效
public class ProductionConfiguration {
// ...
}
(
3、profile的分组功能
- 在主配置文件中 把多个配置文件分组
- spring.profiles.group.组名[下标]=环境标识
- 在使用 spring.profiles.active=组名的时候,可以把一个组配置的都加载
spring.profiles.group.production[0]=proddb
spring.profiles.group.production[1]=prodmq
使用:--spring.profiles.active=production 激活
2、页面的国际化
3、外部化配置
https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config
- Default properties (specified by setting
SpringApplication.setDefaultProperties
- ).
@PropertySource
- 获取外部的配置文件
@Configuration
- classes. Please note that such property sources are not added to the
Environment
- until the application context is being refreshed. This is too late to configure certain properties such as
logging.*
- and
spring.main.*
- which are read before refresh begins.
application.properties
- 配置文件
- A
RandomValuePropertySource
- that has properties only in
random.*
- .
- 操作系统的环境变量
- java的属性 (
System.getProperties()
- ).
- JNDI 属性 来自
java:comp/env
- .
ServletContext
- 初始化参数
ServletConfig
- 配置信息
- Properties from
SPRING_APPLICATION_JSON
- (inline JSON embedded in an environment variable or system property).
- 命令行参数
properties
- attribute on your tests. Available on
@SpringBootTest
- and thetest annotations for testing a particular slice of your application.
@TestPropertySource
- annotations on your tests.
- Devtools global settings properties in the
$HOME/.config/spring-boot
- directory when devtools is active.
1、外部配置源
常用:Java属性文件、YAML文件、环境变量、命令行参数;
- 使用application配置
- 加载系统的环境变量
- 使用@Value("${环境变量名称}")
- 使用通过主配置类找到当前的环境run.getEnvironment();
- 命令行 启动方式
- java -jar xxx.jar 启动相应的java文件,在运行的时候可以添加运行属性。
- 如:java -jar xxx.jar --spring.profiles.active=环境标识
- 两条杠 后面添加相应的属性
- 会优先使用命令行的配置加载
2、配置文件查找位置(优先级依次变高)
(1) classpath 根路径(application就放在类路径的根路径中)
(2) classpath 根路径下config目录
(3) jar包当前目录
- 使用java -jar xxx.jar的时候,使用的是这个配置文件
(4) jar包当前目录的config目录
(5) /config子目录的直接子目录
3、配置文件加载顺序:
- 当前jar包内部的application.properties和application.yml
- 当前jar包内部的application-{profile}.properties 和 application-{profile}.yml
- 引用的外部jar包的application.properties和application.yml
- 引用的外部jar包的application-{profile}.properties 和 application-{profile}.yml
4、指定环境优先,外部优先,后面的可以覆盖前面的同名配置项
- 对于相同的配置
- 使用命令行执行的时候,在命令行指定的配置最高
- 在java中: /config/application.yaml > application.yaml
- 在jar文件目录中: /config/xx/application.yaml > /config/application > application
4、自定义starter
1、starter启动原理
- starter-pom引入 autoconfigurer 包
- ❤❤❤springBoot自动配置的原理:
- 1、从入口注解@SpringBootApplication---分为三个子注解
- @SpringBootConfiguration 声明配置类
- @ComponentScan 扫描包的注解
- @EnableAutoConfiguration 自动配置最为中点的一个注解
- 2、@EnableAutoConfiguration 由两个注解合成 -----这个注解是自动配置的核心
- @AutoConfigurationPackage 默认的扫描包的规则
- @Import({AutoConfigurationImportSelector.class}) 加载指定的bean
- 3、@Import({AutoConfigurationImportSelector.class}) 的AutoConfigurationImportSelector中
- 返回值为一个string[] 数组,数组中的每一个值为Bean的全限定类名
- string[]的值在 全文中所有的 META-INF/spring.factories 文件中加载
- META-INF/spring.factories 文件 在各个starter中已经写好要注入的类(主要包含每个starter主要的xxxAutoConfiguration)
- 4、根据xxxAutoConfiguration类上的条件,进行注入 和我的配置文件 ,进行把对象配置到容器
引入starter --- xxxAutoConfiguration --- 容器中放入组件 ---- 绑定xxxProperties ---- 配置项
2、自定义starter
- 步骤:
- 步骤一:创建一个springboot应用,创建下面两个springboot模块
- xxx-spring-boot-starter 模块(启动器)
- xxx-spring-boot-starter-autoconfigure 模块(自动配置包)
- 步骤二:编写 xxx-spring-boot-starter 模块
- 该模块只需要 引入springboot 和 xxx-spring-boot-starter-autoconfigure模块
- 步骤三:编写xxx-spring-boot-starter-autoconfigure 模块
- 1、编写 xxxAutoConfiguration配置类 进行组件注入需要的bean
- 2、在想要从配置文件中注入的配置,条件封装为一个bean ,添加@ConfigurationProperties(prefix = "前缀")
- 3、使用@EnableConfigurationProperties 与 配置类绑定,并注入
- 4、配置META-INF/spring.factories使其自动生效
- 在resources下面创建一个spring.factories文件
- 可以查考其他starter的spring.factories的编写
- @EnableAutoConfiguration 注解的全限定类名 = xxxAutoConfiguration全限定类名 (如果想换行 使用 '\' )
- 步骤四:
- 做好的项目--clean+install---打包放到本地仓库中,别的项目就可以引入自定义的starter