【问题标题】:Replace environment variables in Spring properties file other than application.properties替换除 application.properties 之外的 Spring 属性文件中的环境变量
【发布时间】:2019-03-10 02:22:18
【问题描述】:

Spring Boot 将使用相应的环境变量自动解析 application.properties 文件中的任何 ${ENV} 占位符。

但是,当我通过 PropertiesFactoryBean 文件为 Quartz 配置提供 quartz.properties 时,不会发生这种分辨率。

@Bean
public Properties getQuartzProperties() throws IOException {
    PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
    propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
    propertiesFactoryBean.afterPropertiesSet();
    return propertiesFactoryBean.getObject();
}

是否有任何 Spring 方法可以在不使用外部库的情况下替换属性文件中的这些环境变量?

【问题讨论】:

  • 您可以使用注解@PropertySource 来提及差异。基于环境的属性文件,称为spring profiling。请通过弹簧分析。

标签: java spring spring-boot properties-file


【解决方案1】:

您可以声明一个新类来提供属性(使用@Configuration 注释)并提及@PropertySource

@Configuration
@PropertySource("classpath:quartz.properties")
public class QuartzConfig {
      //...
}

通过这种方式,您的 Spring Boot 应用程序可以读取任意数量的属性文件。

【讨论】:

  • 有没有办法以Properties 对象的形式访问该属性文件?我可以将该配置传递给 Quartz 的唯一方法是通过 initialize 方法,并且只接受 Properties 对象
猜你喜欢
  • 1970-01-01
  • 2011-07-13
  • 2018-03-22
  • 1970-01-01
  • 2016-06-08
  • 2017-12-26
  • 2020-09-18
  • 2011-01-16
  • 1970-01-01
相关资源
最近更新 更多