Springboot 激活指定的配置文件

Springboot提供了很多方式,帮助我们实现不同环境应用配置的定制化。我们可以通过properties文件、yaml文件、环境变量和命令行参数定制化配置。这些配置的属性值可通过@Value注解注入到bean的属性中,或通过Spring的Environment对象进行获取,也可以通过@ConfigurationProperties注入到指定的结构体中,我们就可以从Spring的Context中获取结构体中,从而获取指定的属性值。

Springboot具体配置优先级

我们通过各种不同方式传入spring容器的配置有一个指定的优先级,具体可参考Springboot官方文档说明,它们的顺序如下:

1. Devtools global settings properties on your home directory (~/.spring-bootdevtools.
properties when devtools is active).
2. @TestPropertySource annotations on your tests.
3. properties attribute on your tests. Available on @SpringBootTest and the test annotations for
testing a particular slice of your application.
4. Command line arguments.
5. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable
or system property).
6. ServletConfig init parameters.
7. ServletContext init parameters.
8. JNDI attributes from java:comp/env.
9. Java System properties (System.getProperties()).
10.OS environment variables.
11.A RandomValuePropertySource that has properties only in random.*.
12.Profile-specific application properties outside of your packaged jar (application-
{profile}.properties and YAML variants).
13.Profile-specific application properties packaged inside your jar (application-
{profile}.properties and YAML variants).
14.Application properties outside of your packaged jar (application.properties and YAML
variants).
15.Application properties packaged inside your jar (application.properties and YAML variants).
16.@PropertySource annotations on your @Configuration classes.

优先级最高的是热启动devtools配置的,其次是测试相关,然后是通过命令行参数等等,其中以命令行和application-profile.proeprties和YAML使用较多。

命令行方式参数可以通过SprigApplication.setCommandLineProperties(false)进行禁用。

application.properties文件优先级

springboot启动会从以下4个位置加载application-{profile}.properties,application-{profile}.yml文件,默认的加载优先级为

(1) 工程根目录:./config/
(2) 工程根目录:./
(3) classpath:/config/
(4) classpath:/

第一、第二优先级的properties、yml配置文件在打包时不会打入jar中,只能通过命令行参数指定,第三,第四优先级我们可以通过IDEA进行测试

启动项目后,发现监听的端口为/config/application.yml文件中的配置的端口,它的优先级是高于classpath:/的。

自定义application.proeprties文件的路径,可通过参数spring.config.location进行指定,多个路径用','分割,通过spring.config.name指定自定义的propertes文件名。

如:

spring.config.location:classpath:custom/,file:./custom/

spring.config.name=custom.properties

此外还可以通过参数spring.config.additionallocation指定额外的配置文件路径,它的优先级高于默认的配置文件。

application.properties多环境配置

通常我们的应用会在多个环境运行,此时我们需要给每个环境配置一份不同的配置文件,我们可以按照application-{profile}.properites的方式给每个环境配置一份配置文件,如开发环境:application-dev.properties,然后通过命令行参数指定启用的配置文件后缀名,-Dspring.profiles.active=dev指定激活application-dev.properties配置文件,实现不同环境不同配置。如果未进行参数配置,则默认启用application-default.properties配置文件。

application-{profile}.properties配置覆盖

当我们在默认配置文件中指定了spring.profiles.active时,此时会启用指定的配置文件

如果我们通过命令行参数指定spring.profiles.active时,此时配置文件中spring.profiles.active指定的配置会被覆盖

当我们指定的配置文件中有的配置值不存在,但配置文件中的spring.profiles.active指定的配置文件中存在该值,则会使用该配置文件中的值

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值