Annotation Type PropertySource
@Target(TYPE) @Retention(RUNTIME) @Documented @Repeatable(PropertySources.class) public @interface PropertySource
Annotation providing a convenient and declarative mechanism for adding aPropertySourceto Spring'sEnvironment. To be used in conjunction with @Configurationclasses.Example usage
Given a file
app.propertiescontaining the key/value pairtestbean.name=myTestBean, the following@Configurationclass uses@PropertySourceto contributeapp.propertiesto theEnvironment's set ofPropertySources.@Configuration @PropertySource("classpath:/com/myco/app.properties") public class AppConfig { @Autowired Environment env; @Bean public TestBean testBean() { TestBean testBean = new TestBean(); testBean.setName(env.getProperty("testbean.name")); return testBean; } }Notice that the
Environmentobject is@Autowiredinto the configuration class and then used when populating theTestBeanobject. Given the configuration above, a call totestBean.getName()will return "myTestBean".Resolving
${...}placeholders in<bean>and@ValueannotationsIn order to resolve ${...} placeholders in
<bean>definitions or@Valueannotations using properties from aPropertySource, you must ensure that an appropriate embedded value resolver is registered in theBeanFactoryused by theApplicationContext. This happens automatically when using<context:property-placeholder>in XML. When using@Configurationclasses this can be achieved by explicitly registering aPropertySourcesPlaceholderConfigurervia astatic@Beanmethod. Note, however, that explicit registration of aPropertySourcesPlaceholderConfigurervia astatic@Beanmethod is typically only required if you need to customize configuration such as the placeholder syntax, etc. See the "Working with externalized values" section of@Configuration's javadocs and "a note on BeanFactoryPostProcessor-returning@Beanmethods" of@Bean's javadocs for details and examples.Resolving ${...} placeholders within
@PropertySourceresource locationsAny ${...} placeholders present in a
@PropertySourceresource location will be resolved against the set of property sources already registered against the environment. For example:@Configuration @PropertySource("classpath:/com/${my.placeholder:default/path}/app.properties") public class AppConfig { @Autowired Environment env; @Bean public TestBean testBean() { TestBean testBean = new TestBean(); testBean.setName(env.getProperty("testbean.name")); return testBean; } }Assuming that "my.placeholder" is present in one of the property sources already registered, e.g. system properties or environment variables, the placeholder will be resolved to the corresponding value. If not, then "default/path" will be used as a default. Expressing a default value (delimited by colon ":") is optional. If no default is specified and a property cannot be resolved, an
IllegalArgumentExceptionwill be thrown.A note on property overriding with @PropertySource
In cases where a given property key exists in more than one
.propertiesfile, the last@PropertySourceannotation processed will 'win' and override.For example, given two properties files
a.propertiesandb.properties, consider the following two configuration classes that reference them with@PropertySourceannotations:@Configuration @PropertySource("classpath:/com/myco/a.properties") public class ConfigA { } @Configuration @PropertySource("classpath:/com/myco/b.properties") public class ConfigB { }The override ordering depends on the order in which these classes are registered with the application context.
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ConfigA.class); ctx.register(ConfigB.class); ctx.refresh();
In the scenario above, the properties in
b.propertieswill override any duplicates that exist ina.properties, becauseConfigBwas registered last.In certain situations, it may not be possible or practical to tightly control property source ordering when using
@PropertySourceannotations. For example, if the@Configurationclasses 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. SeeConfigurableEnvironmentandMutablePropertySourcesjavadocs for details.NOTE: This annotation is repeatable according to Java 8 conventions. However, all such
@PropertySourceannotations need to be declared at the same level: either directly on the configuration class or as meta-annotations within the same custom annotation. Mixing of direct annotations and meta-annotations is not recommended since direct annotations will effectively override meta-annotations.- Since:
- 3.1
- Author:
- Chris Beams, Juergen Hoeller, Phillip Webb, Sam Brannen
- See Also:
PropertySources,Configuration,PropertySource,ConfigurableEnvironment.getPropertySources(),MutablePropertySources
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description StringencodingA specific character encoding for the given resources, e.g.Class<? extends PropertySourceFactory>factorySpecify a customPropertySourceFactory, if any.booleanignoreResourceNotFoundIndicate if failure to find the aproperty resourceshould be ignored.StringnameIndicate the name of this property source.
Element Detail
value
String[] value
Indicate the resource location(s) of the properties file to be loaded.Both traditional and XML-based properties file formats are supported — for example,
"classpath:/com/myco/app.properties"or"file:/path/to/file.xml".Resource location wildcards (e.g. **/*.properties) are not permitted; each location must evaluate to exactly one
.propertiesresource.${...} placeholders will be resolved against any/all property sources already registered with the
Environment. See above for examples.Each location will be added to the enclosing
Environmentas its own property source, and in the order declared.
name
String name
Indicate the name of this property source. If omitted, thefactory()will generate a name based on the underlying resource (in the case ofDefaultPropertySourceFactory: derived from the resource description through a corresponding name-lessResourcePropertySourceconstructor).- Default:
- ""
ignoreResourceNotFound
boolean ignoreResourceNotFound
Indicate if failure to find the aproperty resourceshould be ignored.trueis appropriate if the properties file is completely optional. Default isfalse.- Since:
- 4.0
- Default:
- false
factory
Class<? extends PropertySourceFactory> factory
Specify a customPropertySourceFactory, if any.By default, a default factory for standard resource files will be used.
- Since:
- 4.3
- See Also:
DefaultPropertySourceFactory,ResourcePropertySource
- Default:
- org.springframework.core.io.support.PropertySourceFactory.class