我想在春季启动时设置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 {
}
config / 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
弹簧启动执行器/ env
spring.profiles.active: "production"
您可能应该在application.yml文件前添加前缀 classpath:
无论如何,为什么不只使用spring配置文件直接在Java配置中驱动配置?海事组织,这将更清洁,并使您的财产更加类型安全且易于重构,并且不易出现拼写错误。
更新:
根据文档,您无法加载带有@PropertySource
注释的yml文件:
因此,如果您需要使用文件,则需要使用普通属性文件。您可以使用此处显示的特定于属性的应用程序属性文件。
除了application.properties文件之外,还可以使用命名约定application- {profile} .properties来定义特定于配置文件的属性。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句