The @PropertySource annotation tells Spring to load a properties file into the Environment so its values can be injected with @Value or read through Environment.getProperty(). It is placed on a @Configuration class and is how plain Spring applications pull in extra configuration files. In Spring Boot, application.properties and application.yml are loaded automatically, so @PropertySource is only needed for additional files.
One limitation matters in practice: @PropertySource does not support YAML out of the box, only .properties and XML property files, unless you supply a custom factory.
Spring represents configuration as an Environment that holds a list of property sources, such as system properties, environment variables and files. @PropertySource appends a file to that list when the annotated configuration class is processed.
With src/main/resources/app.properties:
The annotation is repeatable since Spring 4, so several files can be declared by repeating it or with @PropertySources. When two files define the same key, the one declared later wins.
In Spring Boot, prefer @ConfigurationProperties with a record. It gives type safety, validation and relaxed binding, and works with the properties you loaded.
Register it with @EnableConfigurationProperties(AppProperties.class) or @ConfigurationPropertiesScan. Relaxed binding maps app.max-retries to maxRetries.
| Location | Meaning |
|---|---|
classpath:app.properties | File on the classpath |
file:/etc/myapp/app.properties | File on the file system |
classpath:config/${env}.properties | Placeholder resolved from the environment |
Placeholders in the path are resolved against existing property sources, so ${env} must come from system properties or environment variables, not from the file being loaded.
By default a missing file fails startup. Set ignoreResourceNotFound = true for optional overrides, and encoding when the file is not read in the platform default as expected:
@PropertySource uses DefaultPropertySourceFactory, which reads .properties and XML only. To load YAML, write a factory that uses YamlPropertiesFactoryBean:
Property sources registered by @PropertySource have low precedence in Spring Boot. Command-line arguments, system properties, environment variables and application.properties all override them. Also, the annotation is processed late: values from a @PropertySource file are not available when Boot decides auto-configuration conditions or when it resolves logging.* and spring.profiles.* settings.
For Boot applications, these alternatives are usually better:
spring.config.import=optional:file:./extra.properties in application.properties loads extra files with Boot's full precedence rules and supports YAML.application-prod.yml for environment differences.@ConfigurationProperties for typed access.Not by default. Provide a custom PropertySourceFactory as shown above, or use spring.config.import in Spring Boot.
Startup fails with a FileNotFoundException unless you set ignoreResourceNotFound = true.
Usually not. Boot loads application.properties and application.yml automatically. Use spring.config.import for extra files, and @PropertySource only when you need classic Spring behaviour.
Set the same key as an environment variable, system property or command-line argument. Those sources have higher precedence in Boot.
@PropertySource adds a properties file to the Spring Environment so its keys can be injected with @Value, read with Environment, or bound with @ConfigurationProperties. It handles .properties and XML natively, needs a custom factory for YAML, and has low precedence in Spring Boot, where spring.config.import is often the better tool.