替换application.properties以外的Spring属性文件中的环境变量

ᴘᴀɴ*_*ᴛɪs 5 java spring properties-file spring-boot

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

但是,当我quartz.properties通过PropertiesFactoryBeanQuartz配置文件提供时,这种解决方案不会发生.

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

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

Adi*_*lea 3

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

@Configuration
@PropertySource("classpath:quartz.properties")
public class QuartzConfig {
      //...
}
Run Code Online (Sandbox Code Playgroud)

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