Annotation Type PropertySource

public @interface PropertySource

Annotation providing a convenient and declarative mechanism for adding a PropertySource to Spring's Environment. To be used in conjunction with @Configuration classes.

Example usage

Given a file containing the key/value pair, the following @Configuration class uses @PropertySource to contribute to the Environment's set of PropertySources.

 public class AppConfig {
     Environment env;

     public TestBean testBean() {
         TestBean testBean = new TestBean();
         return testBean;
Notice that the Environment object is @Autowired into the configuration class and then used when populating the TestBean object. Given the configuration above, a call to testBean.getName() will return "myTestBean".

A note on property overriding with @PropertySource

In cases where a given property key exists in more than one .properties file, the last @PropertySource annotation processed will 'win' and override. For example, given two properties files and, consider the following two configuration classes that reference them with @PropertySource annotations:
 public class ConfigA { }

 public class ConfigB { }
The override ordering depends on the order in which these classes are registered with the application context.
 AnnotationConfigApplicationContext ctx =
     new AnnotationConfigApplicationContext();
In the scenario above, the properties in will override any duplicates that exist in, because ConfigB was registered last.

In certain situations, it may not be possible or practical to tightly control property source ordering when using @ProperySource annotations. For example, if the @Configuration classes above were registered via component-scanning, the ordering is difficult to predict. In such cases - and if overriding is important - it is recommended that the user fall back to using the programmatic PropertySource API. See ConfigurableEnvironment and * MutablePropertySources Javadoc for details.

Chris Beams
See Also:
Configuration, PropertySource, ConfigurableEnvironment.getPropertySources(), MutablePropertySources

Required Element Summary
 String[] value
          Indicate the resource location(s) of the properties file to be loaded.
Optional Element Summary
 String name
          Indicate the name of this property source.

Element Detail


public abstract String[] value
Indicate the resource location(s) of the properties file to be loaded. For example, "classpath:/com/myco/" or "file:/path/to/file". Note that resource location wildcards are not permitted, and that each location must evaluate to exactly one .properties resource. Each location will be added to the enclosing Environment as its own property source, and in the order declared.


public abstract String name
Indicate the name of this property source. If omitted, a name will be generated based on the description of the underlying resource.

See Also:
PropertySource.getName(), Resource.getDescription()