spring学习7-PropertySource Abstraction
2014-11-09 12:59
232 查看
PropertySource Abstraction
Spring’s Environment abstraction provides search operations over a configurable hierarchy of property sources. To explain fully, consider the following:ApplicationContext ctx = new GenericApplicationContext(); Environment env = ctx.getEnvironment(); boolean containsFoo = env.containsProperty("foo"); System.out.println("Does my environment contain the 'foo' property? " + containsFoo);
In the snippet above, we see a high-level way of asking Spring whether the
fooproperty
is defined for the current environment. To answer this question, the
Environmentobject
performs a search over a set of
PropertySourceobjects.
A
PropertySourceis
a simple abstraction over any source of key-value pairs, and Spring’s
StandardEnvironmentis
configured with two PropertySource objects — one representing the set of JVM system properties (a la
System.getProperties())
and one representing the set of system environment variables (a la
System.getenv()).
These default property sources are present for StandardEnvironment, for use in standalone applications. StandardServletEnvironmentis populated with additional default property sources including servlet config and servlet context parameters. StandardPortletEnvironmentsimilarly has access to portlet config and portlet context parameters as property sources. Both can optionally enable a JndiPropertySource. See Javadoc for details. |
StandardEnvironment,
the call to
env.containsProperty("foo")will
return true if a
foosystem
property or
fooenvironment
variable is present at runtime.
The search performed is hierarchical. By default, system properties have precedence over environment variables, so if the fooproperty happens to be set in both places during a call to env.getProperty("foo"), the system property value will win and be returned preferentially over the environment variable. |
PropertySourceand
add it to the set of
PropertySourcesfor
the current
Environment:
ConfigurableApplicationContext ctx = new GenericApplicationContext(); MutablePropertySources sources = ctx.getEnvironment().getPropertySources(); sources.addFirst(new MyPropertySource());
In the code above,
MyPropertySourcehas
been added with highest precedence in the search. If it contains a
fooproperty,
it will be detected and returned ahead of any
fooproperty
in any other
PropertySource.
The
MutablePropertySourcesAPI
exposes a number of methods that allow for precise manipulation of the set of property sources.
5.13.4 @PropertySource
The @PropertySourceannotation
provides a convenient and declarative mechanism for adding a
PropertySourceto
Spring’s
Environment.
Given a file "app.properties" containing the key/value pair
testbean.name=myTestBean,
the following
@Configurationclass
uses
@PropertySourcein
such a way that a call to
testBean.getName()will
return "myTestBean".
@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; } }
Any
${...}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. If no default is specified and a property cannot be resolved, an
IllegalArgumentExceptionwill
be thrown.
相关文章推荐
- 学习Spring必学的Java基础知识(3)----PropertyEditor
- Spring3.1新属性管理API:PropertySource、Environment、Profile
- Spring源码学习-PropertyPlaceholderHelper
- Spring学习之获取其他Bean的属性值(PropertyPathFactoryBean)--踩坑
- Spring学习之获取其他Bean的属性值(PropertyPathFactoryBean)
- 基于spring source tools suite注解的spring3.0.x MVC学习笔记(二)
- 使用Spring提供的BeanUtils.copyProperties()方法报错:Could not copy property 'xxx' from source to target
- spring学习的点点滴滴——PropertyPlaceholderConfigurer,PropertiesFactoryBean
- Spring @PropertySource example
- Spring Boot 自定义PropertySourceLoader
- Spring3.1新属性管理API:PropertySource、Environment、Profi
- spring学习5.2-PropertyPlaceholderConfigurer 和PropertyOverrideConfigurer
- spring3.2.3常用配置学习一【PropertyPlaceholderConfigurer】
- spring 3.1 特性之 @propertysource
- spring 源码研究---core包-- PropertySource Environment
- spring@PropertySource
- Spring3.1新属性管理API:PropertySource、Environment、Profile
- Spring 从文件系统读取配置文件, 使用@PropertySource
- spring 学习之 bean 的注入方式 property和constructor-arg的使用方式
- spring @PropertySource classpath