Spring编程:springboot属性配置和属性优先级

Spring Boot 允许我们外部化程序的配置,以便我们可以在不同的环境中使用相同的应用代码。我们可以使用 properties、YAML、环境变量、命令行参数进行外部化配置。配置的属性值可以通过 @Value 注解直接注入到 beans 中,通过 Spring 环境抽象访问;也可以通过 @ConfigurationProperties 绑定到结构化对象中。

Spring Boot 使用一个非常特殊的 PropertySource 顺序,该顺序用来合理的覆盖属性值。属性优先级由高到低排序如下:

1 .spring-boot-devtools.properties

Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).

home 目录下的 .spring-boot-devtools.properties 文件优先级最高,但是需要激活 devtools。

2 @TestPropertySource

@TestPropertySource annotations on your tests.

测试时使用。

3 @SpringBootTest

properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.

猜你喜欢

转载自blog.csdn.net/claram/article/details/94383634