PropertySource to Spring's
Environment. To be used in
conjunction with @Configuration classes.
Given a file app.properties containing the key/value pair
testbean.name=myTestBean, the following @Configuration class
uses @PropertySource to contribute app.properties to the
Environment's set of PropertySources.
@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 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".
${...} placeholders in <bean> and @Value annotationsIn order to resolve ${...} placeholders in <bean> definitions or @Value
annotations using properties from a PropertySource, you must ensure that an
appropriate embedded value resolver is registered in the BeanFactory
used by the ApplicationContext. This happens automatically when using
<context:property-placeholder> in XML. When using @Configuration classes
this can be achieved by explicitly registering a PropertySourcesPlaceholderConfigurer
via a static @Bean method. Note, however, that explicit registration
of a PropertySourcesPlaceholderConfigurer via a static @Bean
method 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 @Bean methods" of @Bean's
javadocs for details and examples.
@PropertySource resource locationsAny ${...} placeholders present in a @PropertySource resource 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 — for example, 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 IllegalArgumentException will be thrown.
@PropertySourceIn cases where a given property key exists in more than one property resource
file, the last @PropertySource annotation processed will 'win' and override
any previous key with the same name.
For example, given two properties files a.properties and
b.properties, consider the following two configuration classes
that reference them with @PropertySource annotations:
@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.properties will override any
duplicates that exist in a.properties, because ConfigB was registered
last.
In certain situations, it may not be possible or practical to tightly control
property source ordering when using @PropertySource 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 javadocs for details.
@PropertySource can be used as a repeatable
annotation. @PropertySource may also be used as a meta-annotation
to create custom composed annotations with attribute overrides.
PropertySources, Configuration, org.springframework.core.env.PropertySource , org.springframework.core.env.ConfigurableEnvironment#getPropertySources() , org.springframework.core.env.MutablePropertySources
| Modifier and Type | Method and Description |
|---|---|
| public String | |
| public Class | |
| public boolean | |
| public String | |
| public String[] |
| encoding | back to summary |
|---|---|
| public String encoding() A specific character encoding for the given resources, e.g. "UTF-8".
| |
| factory | back to summary |
|---|---|
| public Class Specify a custom By default, a default factory for standard resource files will be used
which supports | |
| ignoreResourceNotFound | back to summary |
|---|---|
| public boolean ignoreResourceNotFound() Indicate if a failure to find a
Default is
| |
| name | back to summary |
|---|---|
| public String name() Indicate the unique name of this property source. If omitted, the The name of a
| |
| value | back to summary |
|---|---|
| public String[] value() Indicate the resource locations of the properties files to be loaded. The default As of Spring Framework 6.1, resource location wildcards are also
supported — for example,
Each location will be added to the enclosing | |