简体   繁体   English

Quartz Spring Boot:环境属性

[英]Quartz Spring boot: environment property

I've this code in order to populate my properties: 我有以下代码以填充我的属性:

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

quartz.properties is like: quartz.properties类似于:

org.quartz.jobStore.host = localhost

The problem is that according to current environtment (loc, dev...), I need this property is one or another. 问题是根据当前环境(loc,dev ...),我需要此属性是一个或另一个。

We are looking for a way to parameterize this value. 我们正在寻找一种参数化此值的方法。 Something like: 就像是:

org.quartz.jobStore.host = ${jobHost}

where, jobHost would contain the environment related host. 其中, jobHost将包含与环境相关的主机。

I hope I've explained so well. 我希望我已经解释得很好。

Any ideas? 有任何想法吗?

EDIT 编辑

I've tried setting my jobHost variable on commandline: 我尝试在命令行上设置我的jobHost变量:

mvn clean package spring-boot:run -Dspring-boot.run.arguments=--spring.config.additional-location=scheduler-props.properties,--jobHost=localhost

but it gets me: 但它让我:

java.net.UnknownHostException: ${jobHost} java.net.UnknownHostException:$ {jobHost}

it seems jobHost is not resolved. 似乎jobHost没有解决。

The good way of achieving this is by externalizing configuration. 实现此目的的好方法是外部化配置。 Please refer to https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html for details. 有关详细信息,请参阅https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

Not sure at all I got your problem but if you want to programmatically resolve the hostname then 完全不确定我遇到了您的问题,但是如果您想以编程方式解析主机名,则

InetAddress.getLocalHost().getHostAddress()

is the way to go 是要走的路

Otherwise you can programmatically retrieve a "System property" ( System.getProperty(String name) ) passed to your program using -Dpropertyname=value or an "Environment variable" ( System.getenv(String name) ). 否则,你可以以编程方式检索“系统属性”( System.getProperty(String name) )通过使用-Dpropertyname =值或“环境变量”(您的程序System.getenv(String name) )。

@Bean
public Properties quartzProperties() throws IOException {
  PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
  propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
  propertiesFactoryBean.afterPropertiesSet();
  final Properties prop = propertiesFactoryBean.getObject();
  prop.setProperty("org.quartz.jobStore.host", System.getProperty("jobHost");
  return prop
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM