SpringBoot高级
SpringBoot配置
配置文件优先级
按照yaml>yml>properties的顺序加载
存在相同配置项,后加载的会覆盖先加载的
加载顺序越靠后,优先级越高
SpringBoot存在其他的多种方式进行配置,如下所示,越靠下优先级越高
1. Default properties (specified by setting SpringApplication.setDefaultProperties).
2. @PropertySource annotations on your @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.
3. Config data (such as application.properties files). (上述配置文件的方式)
4. A RandomValuePropertySource that has properties only in random.*.
5. OS environment variables. (系统环境变量方式)
6. Java System properties (System.getProperties()). (java系统参数方式)
7. JNDI attributes from java:comp/env.
8. ServletContext init parameters.
9. ServletConfig init parameters.
10. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
11. Command line arguments. (命令行参数方式)
12. properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.
13. @DynamicPropertySource annotations in your tests.
14. @TestPropertySource annotations on your tests.
15. Devtools global settings properties in the $HOME/.config/spring-boot directory when devtools is active.
系统环境变量方式配置
系统环境变量:将一些配置在系统中设置成全局的环境变量
在run configurations中进行配置
配置规则
配置项目的
.
使用
_
替换
删除所有的
-
字母全部转为大写
java系统参数方式
(System.getProperties())
在run configurations中进行配置
指在运行时,通过
-Dkey=value
进行参数指定
eg:java -Dserver.port=8085 -jar xxxx.jar(命令行)
或
-DServer.port=8085(idea中)