java - Spring 启动: Property file with place holder

标签 java spring spring-boot

我正在使用以下代码加载属性文件:

@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/

相关文章:

java - 如何添加com.google.gson.JsonArray 的特定索引?

java - 错误无法加载资源: the server responded with a status of 403 () en angular

java - Spring Cloud Dataflow应用程序http返回401未经授权

spring-boot - 服务器故障后 Spring 批处理文件恢复

java - 失败时在 ANT 中执行默认任务

java - 添加 POST 正文时无法读取路径参数

java - 如何将外部文件夹添加到类路径?

spring - 在 Spring 应用程序中处理异常的位置

spring - 如何查找 Redis 中可用的最大连接数以及使用了多少连接数和免费连接数?

spring-boot - 无法运行这个简单的流,...需要 serde 配置吗?