Spring @PropertySource Annotation Example
November 22, 2021
This page will walk through Spring @PropertySource
annotation example.
1. The
@PropertySource
annotation is the declarative mechanism for adding PropertySource
to the Spring Environment
.
2. The
@PropertySource
annotation is used in conjunction with @Configuration
classes.
3. The key/value data of property files loaded by
@PropertySource
can be read by using @Value
and Environment
.
4. If a property key exists in more than one property files, the last
@PropertySource
annotation processed will override any previous key with the same name.
5. The property file path passed in
@PropertySource
can have ${...} placeholders.
Contents
Using @PropertySource
1. Find the code to use@PropertySource
annotation.
@PropertySource("classpath:/com/cp/myConfig.properties") @Configuration public class AppConfig { ------ }
@PropertySource
annotation as following.
@PropertySources({ @PropertySource("classpath:/com/cp/myConfig1.properties"), @PropertySource("classpath:/com/cp/myConfig2.properties") }) @Configuration public class AppConfig { ------ }
@PropertySource
annotation is a repeatable annotation.
@PropertySource("classpath:/com/cp/myConfig1.properties") @PropertySource("classpath:/com/cp/myConfig2.properties") @Configuration public class AppConfig { ------ }
AppConfig.java
package com.concretepage; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySources; import org.springframework.core.env.Environment; @PropertySources({ @PropertySource("classpath:/com/cp/myConfig1.properties"), @PropertySource("classpath:/com/cp/myConfig2.properties") }) @Configuration public class AppConfig { @Autowired Environment env; @Bean public Country getCountry() { Country country = new Country(); country.setName(env.getProperty("country.name")); country.setCapital(env.getProperty("country.capital")); return country; } @Bean public City getCity() { City city = new City(); city.setName(env.getProperty("city.name")); city.setDm(env.getProperty("city.dm")); city.setCountryName(env.getProperty("country.name")); return city; } }
country.name=India country.capital=New Delhi
city.name=Varanasi city.dm=Mahesh
package com.concretepage; public class Country { private String name; private String capital; //Setters and Getters }
package com.concretepage; public class City { private String name; private String dm; private String countryName; //Setters and Getters }
package com.concretepage; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class SpringDemo { public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AppConfig.class); ctx.refresh(); Country c = ctx.getBean(Country.class); System.out.println(c.getName()); System.out.println(c.getCapital()); City city = ctx.getBean(City.class); System.out.println(city.getName()); System.out.println(city.getDm()); System.out.println(city.getCountryName()); } }
India New Delhi Varanasi Mahesh India
Fetch Value from Property File
Once the property files are loaded by using@PropertySource
, the values against keys can be accessed by using @Value
and Environment
in @Configuration
class.
1. Using
Environment
.
Autowire
Environment
class.
@Autowired Environment env;
Country country = new Country(); country.setName(env.getProperty("country.name")); country.setCapital(env.getProperty("country.capital"));
@Value
annotation.
Find the code to use
@Value
annotation to access values.
AppConfig.java
package com.concretepage; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySources; @PropertySources({ @PropertySource("classpath:/com/cp/myConfig1.properties"), @PropertySource("classpath:/com/cp/myConfig2.properties") }) @Configuration public class AppConfig { @Value("${country.name}") String countryName; @Value("${country.capital}") String countryCapital; @Value("${city.name}") String cityName; @Value("${city.dm}") String cityDm; @Bean public Country getCountry() { Country country = new Country(); country.setName(countryName); country.setCapital(countryCapital); return country; } @Bean public City getCity() { City city = new City(); city.setName(cityName); city.setDm(cityDm); city.setCountryName(countryName); return city; } }
Property Overriding
If a property key exists in multiple property files, then the last@PropertySource
annotation processed will override any previous key with the same name.
myConfig1.properties
country.name=India country.capital=New Delhi
country.name=USA city.name=Varanasi city.dm=Mahesh
country.name
exists in both the above property files.
@PropertySources({ @PropertySource("classpath:/com/cp/myConfig1.properties"), @PropertySource("classpath:/com/cp/myConfig2.properties") }) @Configuration public class AppConfig { @Value("${country.name}") String countryName; ------ }
countryName
will be USA.
Resolving ${...} Placeholders
There can be ${...} placeholders in property file path passed in@PropertySource
annotation and these ${...} placeholders can be resolved against the set of property sources already registered against the environment.
@PropertySource("classpath:/com/${prop.placeholder}/myConfig.properties")
@PropertySource("classpath:/com/${prop.placeholder:default/path}/myConfig.properties")
@TestPropertySource
annotation, we will register the placeholder key and its value.
SpringAppTest.java
@ExtendWith(SpringExtension.class) @ContextConfiguration(classes = AppConfig.class) @TestPropertySource(properties = { "prop.placeholder=cp" }) public class SpringAppTest { @Autowired private Country country; @Test public void test() { assertEquals("India", country.getName()); } }
@PropertySource("classpath:/com/${prop.placeholder}/myConfig.properties") @Configuration public class AppConfig { @Autowired Environment env; @Bean public Country getCountry() { Country country = new Country(); country.setName(env.getProperty("country.name")); country.setCapital(env.getProperty("country.capital")); return country; } }