Externalizing Configuration with Spring's @PropertySource
- michalkrysiuk64
- Feb 19
- 3 min read
Updated: Feb 20

As Java developers, we understand the importance of separating configuration from code to keep our applications flexible and maintainable. One powerful tool in the Spring ecosystem that helps achieve this is the @PropertySource annotation. In this post, we’ll dive into how @PropertySource works, how to use it effectively, and the key role it plays in your Spring application's configuration management.
What is @PropertySource?
The @PropertySource annotation is a declarative mechanism that allows you to add external configuration properties to Spring’s environment. By using it, you can load property files—commonly with a .properties extension—and have their key/value pairs available for injection into your beans. This means that instead of hardcoding values, you can define them externally and let Spring manage their availability throughout your application.
Consider this simple example:
@Configuration
@PropertySource("classpath:/com/myco/app.properties")
public class AppConfig {
@Autowired
private Environment env;
@Bean
public TestBean testBean() {
TestBean testBean = new TestBean();
testBean.setName(env.getProperty("testbean.name"));
return testBean;
}
}
In this configuration, Spring loads the app.properties file from the classpath, and all its key/value pairs become part of the Spring environment. When the TestBean is created, it retrieves the property testbean.name from the environment, ensuring that configuration values can be easily managed outside of the code.
How It Works
At its core, @PropertySource adds a set of name/value pairs to the Spring Environment from an external source. This makes it possible for your application to leverage dynamic configurations without needing to recompile code when changes are required.
Key Features:
Declarative Property Injection:
Instead of manually loading properties, you annotate your configuration class, and Spring automatically makes these properties available.
You can inject property values using @Value or retrieve them programmatically through the Environment interface.
Multiple Property Files:
With Java 8 or higher, you can apply @PropertySource multiple times on a single configuration class.
For earlier versions, the @PropertySources annotation allows you to specify an array of property sources.
@Configuration
@PropertySources({
@PropertySource("classpath:db.properties"),
@PropertySource("classpath:root.properties")
})
public class DBConfiguration {
// Configuration details here
}
Placeholder Resolution:
You can include placeholders in the resource locations to make your configuration even more flexible.
For instance, using a placeholder allows you to dynamically set the path of your properties file:
@Configuration
@PropertySource("classpath:/com/${my.placeholder:default/path}/app.properties")
public class AppConfig {
// Bean definitions here
}
External and Optional Files:
@PropertySource can load properties from the file system using the file: prefix.
It also offers the option to ignore missing resource files with ignoreResourceNotFound = true, which can be useful for optional configurations.
@PropertySource(
value = "file:${HOME}/db.properties",
ignoreResourceNotFound = true)
Benefits in a Spring Application
By leveraging @PropertySource, you empower your application to:
Centralize Configuration: Manage all configuration properties externally, which is especially helpful when working with different environments (development, staging, production).
Increase Flexibility: Update configuration values without changing the application code.
Support Testing: Easily override properties in test environments using annotations like @TestPropertySource.
For example, if you’re creating a database connection bean, you can store all the necessary parameters in a properties file. This setup not only simplifies your code but also makes it easier to modify settings like the database URL or credentials without a full redeployment.
Conclusion
The @PropertySource annotation is a straightforward yet powerful tool for externalizing configuration in Spring. Its primary role is to load external property files and add their name/value pairs to the Spring Environment, enabling you to manage configurations cleanly and efficiently. Whether you’re dealing with multiple property files, using placeholders for dynamic paths, or handling optional configurations, @PropertySource provides the flexibility and control needed to keep your Spring applications robust and adaptable.
Happy coding, and may your configurations always be externalized and easily maintainable!
Comments