【问题标题】:Separate properties file for multiple environments多个环境的单独属性文件
【发布时间】:2015-12-14 14:40:04
【问题描述】:

我正在尝试为 prod 和 dev 环境设置单独的属性文件。

我有两个属性文件application-prod.propertiesapplication-dev.properties放在classpath:/config

我添加了虚拟机选项-Dspring.profiles.active=dev

根据我从the documentation 和网络上的许多其他参考资料中了解到的,在以environment.getProperty("") 访问Spring Environment 时,应该加载“application-dev.properties”中的属性。但是,我得到了 null 并且似乎 Spring 没有读取属性文件。

我还尝试在@PropertySource 中定义这两个文件。这样做,第二个定义的文件被拾取并返回相应的属性。 Spring 没有根据活动配置文件选择文件。

我错过了什么吗?

我也通过一些 SO 问题遇到了issue raised,但我不确定它是否指的是同一个问题。

【问题讨论】:

  • 你在使用带有自动配置的 Spring Boot 吗?
  • 我是菜鸟。据我了解,我没有使用弹簧靴。我正在使用 spring 框架进行自动装配(使用 context:annotation-config)。我不确定我在这里是否有意义:|

标签: java spring spring-mvc properties spring-boot


【解决方案1】:

是的,所以您指向的文档来自 Spring Boot 项目。这与 Spring Framework 不同。如果您不使用 Spring Boot,-Dspring.profiles.active=dev 将无法正常工作。

你有两个选择:

  1. 在您的项目中引入 Spring Boot 并打开自动配置(@SpringBootApplication@EnableAutoConfiguration)。
  2. 使用简单的 Spring Framework 功能,如 PropertyPlaceholderConfigurer,但它不会为您提供与 Spring Boot 功能相同的灵活性,您需要创建一些样板代码来处理各种环境。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-09
    • 2012-12-25
    • 2017-07-15
    • 1970-01-01
    • 2018-03-22
    • 1970-01-01
    相关资源
    最近更新 更多