0

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:

org.quartz.jobStore.host = localhost

The problem is that according to current environtment (loc, dev...), I need this property is one or another.

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.

I hope I've explained so well.

Any ideas?

EDIT

I've tried setting my jobHost variable on commandline:

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}

it seems jobHost is not resolved.

Jordi
  • 18,082
  • 29
  • 125
  • 263
  • Possible duplicate of [Spring boot: Property file with place holder](https://stackoverflow.com/questions/54113717/spring-boot-property-file-with-place-holder) – Deadpool Jan 09 '19 at 16:17
  • delete one of the post, don't create duplicates – Deadpool Jan 09 '19 at 16:17

2 Answers2

0

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.

Rakesh
  • 3,474
  • 2
  • 16
  • 31
  • Could you take a look on post? – Jordi Jan 09 '19 at 15:25
  • I don't think you can pass/append properties directly like the way you mentioned above. You can create dev.properties where you can add your host and I am sure it should be resolved as usual. – Rakesh Jan 09 '19 at 15:29
  • I've also tried addint it on `application.properties`. It fails again... – Jordi Jan 09 '19 at 15:35
  • I guess you have org.quartz.jobStore.host = ${jobHost} in you application.properties file and It wont get resolved within that from other files. Consider injecting the property directly inside your xml like value="${jobHost}" or java class using @Value("${jobHost:myDevhost}"). I hope that will help – Rakesh Jan 09 '19 at 15:40
0

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)).

@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
}
Gab
  • 7,440
  • 2
  • 35
  • 66