繁体   English   中英

具有外部属性的Spring Boot配置文件

[英]Spring boot profiles with external properties

我想在春季启动时设置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

弹簧启动执行器/ env

spring.profiles.active: "production"

您可能应该在application.yml文件前加上classpath:

无论如何,为什么不只使用spring配置文件直接在Java配置中驱动配置? 海事组织,这将更清洁,并使您的财产更安全,更便于重构,并且不容易出现拼写错误。

更新:

根据文档,您无法使用@PropertySource注释加载yml文件:

http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-yaml-shortcomings

因此,如果需要使用文件,则需要使用普通属性文件。 您可以使用此处显示的特定于属性的应用程序属性文件。

除了application.properties文件,还可以使用命名约定application- {profile} .properties来定义特定于配置文件的属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM