1

I'm loading a property file like using this code:

@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 = ${jobHost}

I've tried setting my jobHost variable application.properties file:

jobHost = localhost

but it gets me:

java.net.UnknownHostException: ${jobHost}

it seems jobHost is not resolved.

Any ideas?

Jordi
  • 20,868
  • 39
  • 149
  • 333

2 Answers2

0

Since you directly handle the Properties, the ${jobHost} doesn't get resolved.

You can use ConfigurationProperties:

@Configuration
@PropertySource("classpath:quartz.properties")
@ConfigurationProperties(prefix = "xxx")
public class QuartzConfigProperties {
    // Fields go here
}

or

@Component
@PropertySource("classpath:quartz.properties")
public class QuartzConfigProperties {

    @Value("${org.quartz.jobStore.host}")
    private String host;

    //getters and setters

}
  • I guess I need to fill up this class with some fields. Do I need to add `jobHost`? – Jordi Jan 09 '19 at 16:00
  • @Jordi Yes. For simplicity I suggest you to use the second alternative –  Jan 09 '19 at 16:02
  • As far I've figured out, you are setting on `host` field the value of `org.quartz.jobStore.host`. What I need is to set the `org.quartz.jobStore.host` the value of `jobHost`... – Jordi Jan 09 '19 at 16:04
  • @Jordi No, that's how `@Value` works. Must be something like `${the key from .properties file}`. But you can set also `${jobHost}` –  Jan 09 '19 at 16:08
0

Storing property in more than one place can be confusing for other programmers. And the keys should be unique to preserve consistency I recommend to decide from which property file you want to read the value.

1. Reading from application.properties See example here:

How to access a value defined in the application.properties file in Spring Boot

Access it from any @Component as @Eugen Covaci suggested with @Value:

@Value("${org.quartz.jobStore.host}")
private String name;

2. Reading from quartz.properties This way you are open for new properties (I assume you will have more if you created a separate property file). See my example below:

QuartzProperties POJO (each field is a property)

    public class QuartzProperties {

    private String jobHost;

    public String getJobHost() { return jobHost; }

    public void setJobHost(String jobHost) { this.jobHost = jobHost; }
}

Configuration class

@Configuration
@PropertySource("classpath:quartz.properties")
public class QuartzConfig {
    @Value("${org.quartz.jobStore.host}")
    String jobHost;
    //other properties for Quartz here
    @Bean
    public QuartzProperties quartzProperties(){
        QuartzProperties quartzProperties = new QuartzProperties();
        quartzProperties.setJobHost(jobHost);
        //setters for other properties here
        return quartzProperties;
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer properties(){
        return new PropertySourcesPlaceholderConfigurer();
    }
}

Access it from any @Component by quartzProperties.getJobHost().

I just named it as quartzProperties and QuartzConfig but it can be jobStoreProperties or JobStoreConfig. Depends on the purpose of these properties.

Or if you want to create 2 profiles (dev & test for example) you could create them following the Spring YAML Configuration

Or you can externalize the configuration as an environment variable/pass it as a command line argument. Externalized Configuration

fhery021
  • 317
  • 2
  • 7