Skip to content

Spring @PropertySource annotation

  • by
Spring @PropertySource annotation

1. Overview

In this article, we will learn about the Spring @PropertySource annotation. You can use the @PropertySource annotation to externalize your configuration to a properties file.

This provides a convenient and declarative mechanism for adding a PropertySource to Spring’s Environment.
A PropertySource<T> is nothing but an abstract base class representing a source of name/value property pairs. The underlying property source object can be of any type T that encapsulates properties such as Properties objects, Map objects, and so on.

2. Default base property

By default, SpringApplication loads properties from application.properties or application.yml files automatically by scanning the following locations and adds them to the Spring Environment:

  1. /config subdirectory of the current directory
  2. The current directory
  3. A classpath /config package
  4. The classpath root

Sometimes you want to load properties from a different property file or multiple property files to the Spring Environment's set of PropertySources.

3. @PropertySource annotation

To load a different property source into your Spring application other than application.properties or application.yml, you can use @PropertySource annotations in your @Configuration classes.

Just having the cms.properties file in the class path does not make the Spring to load it in the application. You should have the @PropertySource to add the cms.property source to the application.

Assume you want to load a property file cms.properties from your class path.

samplebean.value=1000

You can add the @PropertySource annotation to your @Configuration class and use the properties defined in the cms.properties file.

@Configuration
@PropertySource("classpath:/cms.properties")
public class AppConfig {

    @Autowired
    Environment env;

    @Bean
    public SampleBean sampleBean() {
        SampleBean sampleBean = new SampleBean();
        sampleBean.setValue(env.getProperty("samplebean.value"));
        return sampleBean;
    }
}
@SpringBootApplication
public class PropertyPlaceholderApplication implements CommandLineRunner {
	private Logger logger = LoggerFactory.getLogger(PropertyPlaceholderApplication.class);

	@Autowired
	SampleBean sampleBean;

	public static void main(String[] args) {
		SpringApplication.run(PropertyPlaceholderApplication.class, args);
	}

	@Override
	public void run(String... args) throws Exception {
		logger.info(sampleBean.getValue());
	}
}

If you execute the above application, you can see that the value property is assigned to the SampleBean as expected.

2022-04-08 13:04:09.900  INFO 5816 --- [           main] c.t.p.PropertyPlaceholderApplication     : Started PropertyPlaceholderApplication in 2.91 seconds (JVM running for 4.551)
2022-04-08 13:04:09.936  INFO 5816 --- [           main] c.t.p.PropertyPlaceholderApplication     : 1000

Note that Spring does not add these property sources to the Environment until the application context is being refreshed.

This is too late to configure certain properties such as logging.* and spring.main.* which are read before refresh begins.

4. Multiple property sources

You can define the @PropertySource annotation repeatedly according to Java 8 conventions. Hence, if we’re using Java 8 or above, you can use this annotation to define multiple property locations:

@Configuration
@PropertySource("classpath:/cms.properties")
@PropertySource("classpath:/root.properties")
public class AppConfig 

Alternatively, you can use @PropertySources to define multiple property sources as an array:

@Configuration

@PropertySources({
        @PropertySource("classpath:/cms.properties"),
        @PropertySource("classpath:/root.properties")
})
public class AppConfig {

There are scenarios where a property key exists in more than one .properties file, the last @PropertySource annotation processed will ‘win’ and override any previous key with the same name.

5. Conclusion

To sum up, we have learned the Spring @PropertySource annotation with example.