Spring(十八)-Environment Abstraction
参考文献
理解Spring Environment
抽象
- 统一的Spring配置属性管理
- Spring Framework 3.1开始引入
Environment
抽象,它统一Spring配置属性的存储,包括占位符处理和类型转换,不仅完整地替换PropertyPlaceholderConfigurer
,而且还支持更丰富的配置属性源PropertySource
;
- Spring Framework 3.1开始引入
- 条件化Spring Bean装配管理
- 通过
Environment Porfiles
信息,帮助Spring容器提供条件化地装配Bean;
- 通过
Spring Environment
接口使用场景
- 用于属性占位符处理
- 用于转换Spring配置属性类型
- 用于存储Spring配置属性源(
PropertySource
) - 用于
Profiles
状态的维护
Environment
占位符处理
- Spring 3.1前占位符处理
- 组件:
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
- 接口:
org.springframework.util.StringValueResolver
- 组件:
- Spring 3.1+占位符处理
- 组件:
org.springframework.context.support.PropertySourcesPlaceholderConfigurer
- 接口:
org.springframework.beans.factory.config.EmbeddedValueResolver
- 组件:
理解条件配置Spring Profils
-
Spring 3.1条件配置
-
API:
org.springframework.core.env.ConfigurableEnvironment
-
修改:
void setActiveProfiles(String... profiles);
void addActiveProfile(String profile);
void setDefaultProfiles(String... profiles);
-
获取:
String[] getActiveProfiles();
String[] getDefaultProfiles();
-
匹配:
1
2
boolean acceptsProfiles(String... profiles);1
boolean acceptsProfiles(Profiles profiles);
-
-
注解:
@org.springframework.context.annotation.Profile
-
Spring 4 重构@Profile
- 基于Spring 4
org.springframework.context.annotation.Condition
接口org.springframework.context.annotation.ProfileCondition
依赖注入Environment
- 直接依赖注入
- 通过
EnvironmentAware
接口回调 - 通过
@Autowired
注入Environment
- 通过
- 间接依赖注入
- 通过
ApplicationContextAware
接口回调 - 通过
@Autowired
注入ApplicationContext
- 通过
- 直接依赖查找
- 通过
org.springframework.context.ConfigurableApplicationContext#ENVIRONMENT_BEAN_NAME
- 通过
- 间接依赖直接
- 通过
org.springframework.context.ConfigurableApplicationContext#getEnvironment()
- 通过
依赖注入@Value
- 通过注入
@Value
- 实现
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
- 实现
Spring类型转换在Environment
中的运用
Environment
底层实现- 底层实现:
org.springframework.core.env.PropertySourcesPropertyResolver
- 核心方法:
org.springframework.core.env.AbstractPropertyResolver#convertValueIfNecessary
- 核心方法:
- 底层服务:
org.springframework.core.convert.ConversionService
- 默认实现:
org.springframework.core.convert.support.DefaultConversionService
- 默认实现:
- 底层实现:
Spring配置属性源PropertySource
-
API
- 单配置属性源:
org.springframework.core.env.PropertySource
- 多配置属性源:
org.springframework.core.env.PropertySources
- 单配置属性源:
-
注解
- 单配置属性源:
@org.springframework.context.annotation.PropertySource
- 多配置属性源:
@org.springframework.context.annotation.PropertySources
- 单配置属性源:
-
关联
- 存储对象:
org.springframework.core.env.MutablePropertySources
- 关联方法:
org.springframework.core.env.ConfigurableEnvironment#getPropertySources()
- 存储对象:
Spring内建的配置属性源
-
内建
PropertySource
PropertySource类型 说明 org.springframework.core.env.CommandLinePropertySource
命令行配置属性源 org.springframework.jndi.JndiPropertySource
JNDI配置属性源 org.springframework.core.env.PropertiesPropertySource
Properties
配置属性源org.springframework.web.context.support.ServletConfigPropertySource
Servlet
配置属性源org.springframework.web.context.support.ServletContextPropertySource
ServletContext
配置属性源org.springframework.core.env.SystemEnvironmentPropertySource
环境变量配置属性源
基于注解的扩展Spring配置属性源
@org.springframework.context.annotation.PropertySource
实现原理- 入口:
org.springframework.context.annotation.ConfigurationClassParser#doProcessConfigurationClass
org.springframework.context.annotation.ConfigurationClassParser#processPropertySource
- 4.3 新增语义
- 配置属性字符编码-
encoding
org.springframework.core.io.support.PropertySourceFactory
- 配置属性字符编码-
- 适配对象:
org.springframework.core.env.CompositePropertySource
- 入口:
基于API扩展Spring配置属性源
- Spring应用上下文启动前装配:
PropertySource
- Spring应用上下文启动后装配:
PropertySource
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 HoleLin's Blog!