@PropertySource tells Spring to load a properties file and add it to the Environment, so its keys can be injected with @Value or read through Environment.getProperty(). You put it on a @Configuration class, give it one or more resource locations such as classpath:custom-config.properties, and Spring registers the file as an extra property source.
In Spring Boot you often do not need it, because application.properties and application.yml are loaded automatically. It is useful for additional files, such as a shared or third-party properties file.
During startup, Spring processes @Configuration classes, reads each @PropertySource and appends a ResourcePropertySource to the Environment. Beans can then resolve ${key} placeholders from it.
The location can use these prefixes:
| Prefix | Meaning |
|---|---|
classpath: | Look on the classpath, usually src/main/resources |
file: | A path on the file system |
${...} placeholders | Resolved from existing environment properties, for example file:${config.dir}/app.properties |
You can list several files in one annotation, and later files override earlier ones when they define the same key.
src/main/resources/custom-config.properties:Calling describe() returns:
The :unknown part of ${app.version:unknown} is a default value that Spring uses when the key is missing. Constructor injection, as shown, keeps the class easy to test.
| Attribute | Purpose |
|---|---|
value | One or more resource locations |
ignoreResourceNotFound | When true, a missing file is ignored instead of failing startup |
encoding | Character set of the file, for example UTF-8 |
name | Name of the property source in the Environment |
factory | A custom PropertySourceFactory to support other formats |
Since Spring 4 the annotation is repeatable, so you can declare it several times on one class:
This loads bundled defaults and then lets an optional file on the server override them.
For groups of related settings, prefer type-safe binding over many @Value fields. Combine @PropertySource with @ConfigurationProperties:
With mail.host=smtp.example.com and mail.port=587 in the file, the object is filled automatically.
@PropertySource reads .properties files only, unless you supply a custom factory. Put YAML in application.yml or use spring.config.import.@PropertySource rank below command-line arguments, environment variables and application.properties, so they cannot override those.logging.* and spring.main.*) before @PropertySource runs, so those keys have no effect from such a file.application-dev.properties style files, use Spring Boot's own profile handling, or build the path with ${spring.profiles.active} carefully.ignoreResourceNotFound = true.In current Spring Boot, spring.config.import=optional:file:./extra.properties is often a better choice than @PropertySource because it takes part in Boot's normal precedence rules and supports YAML.
Not out of the box. Use application.yml, spring.config.import, or write a PropertySourceFactory that parses YAML.
Spring Boot loads application.properties automatically and supports profiles and precedence rules. @PropertySource adds an extra, explicitly named file and ranks lower.
Yes. Use file: with an absolute path, for example @PropertySource("file:/etc/myapp/app.properties"), and consider ignoreResourceNotFound = true.
Common causes are a wrong file path, a missing key without a default, or a @Configuration class that Spring never scanned, so the annotation was never processed.
@PropertySource adds a .properties file to the Spring Environment so you can read it with @Value, Environment or @ConfigurationProperties. Use ignoreResourceNotFound for optional files, repeat the annotation for several files, and remember it ignores YAML and ranks low in Boot. For most Boot applications, application.yml or spring.config.import is the simpler choice.