我正在使用以下代码加载属性文件:
@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
}