springboot test 配置文件加载顺序及覆盖关系

参照目录结构:1.配置文件加载基础原则:2.application.yml主配置文件加载原则:3.application.yml中指定spring.profiles.active:xxx时,xxx的加载原则:4.使用@ActiveProfiles("yyy")时:5.其他自定义配置文件,如xxx.properties:

参照目录结构:

1.配置文件加载基础原则:

通过任意方式指定的application-xxx.yml中会覆盖application.yml中同名配置,application.yml一般作为兜底或通用配置

2.application.yml主配置文件加载原则:

[ 实际运行的application.yml ] = [ test/resources/application.yml ] ? [ test/resources/application.yml ] : [ main/resources/application.yml ]

3.application.yml中指定spring.profiles.active:xxx时,xxx的加载原则:

[ 实际运行的application-xxx.yml ] = [ test/resources/application-xxx.yml ] ? [ test/resources/application-xxx.yml ] : [ main/resources/application-xxx.yml ]

4.使用@ActiveProfiles(“yyy”)时:

yyy会覆盖 application.yml中指定spring.profiles.active:xxx 指定的xxx, xxx不生效application-yyy.yml加载原则同 原则3yyy可与xxx一致, 原则同理

5.其他自定义配置文件,如xxx.properties:

5.1

一般自定义xxx.properties有对应的xxxBean, 会用@PropertySource指定xxx.properties此时[ 实际运行的alipay.properties ] = [ test/resources/alipay.properties ] ? [ test/resources/alipay.properties ] : [ main/resources/alipay.properties ]test 和 main 不会同时加载

@Data

@Component

@PropertySource(value = {"classpath:/xxx.properties"})

@ConfigurationProperties(prefix = "xxx")

public class XxxBean {...}

5.2@PropertySource(“classpath:/xxx.properties”)同时使用@TestPropertySource(“classpath:yyy.properties”)时:

yyy.properties和xxx.properties同时加载yyy.properties优先覆盖xxx.properties同名内容xxx/yyy.properties 的 main/test位置读取原则同上

@ActiveProfiles("sb")

@TestPropertySource({"classpath:yyy.properties"})

@SpringBootTest(classes = AlipayApplication.class)

@RunWith(SpringRunner.class)

public class SpringTest {...}

相关文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: