Spring Boot will automatically resolve any ${ENV}
placeholders in application.properties
files, with the respective environment variable.
However such resolution will not happen when I provide a quartz.properties
through a PropertiesFactoryBean
file for Quartz configuration.
@Bean
public Properties getQuartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
}
Is there any Spring way of replacing these environment variables in the property file without utilising an external library?
YAML is a convenient format for specifying hierarchical configuration data. This can be more readable than its property file alternative since it does not contain repeated prefixes.
Instead of environment variables, we recommend that you either use a YAML configuration file or a shared data source.
Now, when your Spring Boot application starts, it will be given those environment variables, which will override your application. properties .
You can put environment variables in your properties file, but Java will not automatically recognise them as environment variables and therefore will not resolve them. In order to do this you will have to parse the values and resolve any environment variables you find.
You can declare a new class to provide the properties (annotated with @Configuration) and also mention the @PropertySource
@Configuration
@PropertySource("classpath:quartz.properties")
public class QuartzConfig {
//...
}
In this way your spring boot application can read as many properties file as you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With