我想在spring boot中设置3个配置文件:使用外部配置文件进行 生产环境 ,开发和测试 .
申请类:
@SpringBootApplication
public class Application {
public static void main(String[] args){
SpringApplication.run( Application.class, args );
}
}
AppConfig类:
@Configuration
@PropertySources({
@PropertySource("config/application.yml"),
@PropertySource(value = "file:${external.config}")
})
@ConfigurationProperties
public class AppConfig {
}
配置/ application.yml:
---
spring.profiles: production
endpoints.enabled: false
---
spring.profiles: development,test
endpoints.enabled: true
info.version: @project.version@
info.test: Test dev or test
info.profile: ${spring.profiles.active}
---
external.config: ${user.home}/.myapp/application.properties
.myapp / application.properties:
spring.profiles.active=production
info.version=5
spring-boot-actuator / info的输出
{
version: "5",
test: "Test dev or test",
profile: "production"
}
预期产量:
404 because of the endpoints.enabled: false
spring 启动器 - Actuator /环境
spring.profiles.active: "production"
1 回答
您可能应该在application.yml文件前加上
classpath:
的前缀在任何情况下,为什么不直接使用 spring 配置文件在java配置中驱动配置?国际海事组织,这将更清洁,将使您的 property 更加类型安全和重新考虑因素友好,不容易出现拼写错误 .
更新:
根据文档,您无法使用
@PropertySource
注释加载yml文件:http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-yaml-shortcomings
因此,如果您需要使用文件,则需要使用普通属性文件 . 您可以使用here显示的特定于属性的应用程序属性文件 .