我正在使用以下代码加载属性文件:
@Bean
public Properties quartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
}
quartz.properties
就像:
org.quartz.jobStore.host = ${jobHost}
我尝试设置我的 jobHost
变量 application.properties
文件:
jobHost = localhost
但这让我很感动:
java.net.UnknownHostException: ${jobHost}
似乎 jobHost
未解析。
有什么想法吗?
最佳答案
由于您直接处理Properties
,因此${jobHost}
不会得到解析。
您可以使用ConfigurationProperties :
@Configuration
@PropertySource("classpath:quartz.properties")
@ConfigurationProperties(prefix = "xxx")
public class QuartzConfigProperties {
// Fields go here
}
或
@Component
@PropertySource("classpath:quartz.properties")
public class QuartzConfigProperties {
@Value("${org.quartz.jobStore.host}")
private String host;
//getters and setters
}
关于java - Spring 启动: Property file with place holder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54113717/