Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace environment variables in Spring properties file other than application.properties

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?

like image 254
ᴘᴀɴᴀʏɪᴏᴛɪs Avatar asked Oct 04 '18 15:10

ᴘᴀɴᴀʏɪᴏᴛɪs


People also ask

What is the alternative to application properties file?

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.

What can I use instead of environment variables?

Instead of environment variables, we recommend that you either use a YAML configuration file or a shared data source.

Does environment variable override application properties?

Now, when your Spring Boot application starts, it will be given those environment variables, which will override your application. properties .

Can we use environment variables in properties file?

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.


1 Answers

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.

like image 94
Adina Rolea Avatar answered Sep 30 '22 06:09

Adina Rolea