Spring 上下文变量的配置、加载 相关内容

一、 BeanFactoryPostProcessor (上下文占位符相关)

这是一个函数接口(functional interface),所以它能被用作lambda表达式(lambda expression)的表达目标或者是方法的实现目标(method reference)。

它可以让应用程序上下文bean定义的参数,实现自定义修改。通过调整bean工厂底层上下文中bean的配置项属性。

应用程序能在所有的bean定义中自动识别 BeanFactoryPostProcessor的bean,并且在创建其它bean之前先执行它们。

当系统管理员想要自定义应用程序上下文bean的某些参数时,非常合适使用自定义配置文件,然后通过此接口进行加载。

解决此类配置需求的“开箱即用(out-of-the-box)”解决方案,请参见PropertyResourceConfigurer以及其具体实现类。

BeanFactoryPostProcessor 可以与Bean的定义进行交互和修改,但是无法与Bean实例互动。如果非要这么做,可能会导致bean过早地实例化、破坏容器或者得到意想不到的副作用。如果需要与bean实例进行交互,请以实现BeanPostProcessor作为顶替方案。

1.1、PropertyResourceConfigurer

可以从属性配置资源中配置独立bean的配置项,比如一个properties文件。适用于系统管理员自定义配置文件,用于重写应用程序上下文中bean的配置项。

发行版中,提供如下两个具体的实现类

● PropertyOverrideConfigurer 类似于“beanName.property=value"这种形式(将配置文件中的值,写入指定bean的定义中)

● PropertyPlaceholderConfigurer 针对于"${...}"这样的占位符(placeholders)(将配置文件中的值,写入所有bean定义的同名占位符中)

通过重写方法:convertPropertyValue(java.lang.String) 可以将读入的属性值进行转化。比如,在执行替换之前,将加密的值进行解密或者做适当的加密处理。

1.1.1 PropertyPlaceholderConfigurer extends PlaceholderConfigurerSupport

PropertyPlaceholderConfigurer 此类用于匹配针对本地配置文件(local properties)、系统配置(system properties)和环境变量(environment variables)的 ${...} 占位符。

从Spring 3.1开始,应该使用 PropertySourcesPlaceholderConfigurer 代替上述实现类。因为它通过使用更高级的Environment 和同样是3.1开始支持的 PropertySource 组件,变得更为灵活。

在下述情况下,依然适用于使用 PropertyPlaceholderConfigurer

● thespring-contextmodule is not available (i.e., one is using Spring'sBeanFactoryAPI as opposed toApplicationContext).

● existing configuration makes use of the "systemPropertiesMode" and/or "systemPropertiesModeName" properties. Users are encouraged to move away from using these settings, and rather configure property source search order through the container's Environment; however, exact preservation of functionality may be maintained by continuing to use PropertyPlaceholderConfigurer.

在Spring 3.1之前,命名空间  注册为PropertyPlaceholderConfigurer 的一个实例。如果使用spring-context-3.0.xsd 作为命名空间的解释器,那么依然保持现状。也就是说,就算使用Spring 3.1,你只要不更新你的 xsi:schemaLocation 并且继续使用这个3.0的XSD,那么就能保持PropertyPlaceholderConfigurer 作为如上命名空间的注册实例。

1.1.1.1 PropertySourcesPlaceholderConfigurer extends PlaceholderConfigurerSupport implements EnvironmentAware

该子类是PlaceholderConfigurerSupport 下专门针对当前 Spring Environment 和 其下面的 PropertySources 中的变量资源进行 ${...}占位符处理,包括:bean定义中的参数值,@Value标签等。

它在Spring3.1中设计,并用于顶替PropertyPlaceholderConfigurer的类。在spring-context-3.1 XSD中,默认作为property-placeholder的支持者,也就是在3.0之前默认为PropertyPlaceholderConfigurer 后台执行者的元素。请查看spring-context 的XSD文档了解详情。

1.1.1.2 注解类型 PropertySource


二、BeanPostProcessor (跟面向切面编程有关)

工厂钩子(Factory hook)可以对新的bean实例进行自定义修改。比如:为检查标记接口(marker interfaces)或者是代理包装它们。

ApplicationContexts能够在所有的bean定义中自动识别BeanPostProcessor类的bean,并且在所有其他bean创建完成之后执行它们。

普通Bean工厂(plain bean factories)允许注册(programmatic registration) post-processors,适用于所有通过此类工厂创建的bean。

想要通过标记接口( marker interfaces )和类似的方式将post-processors加入到bean中,通常需要实现接口postProcessBeforeInitialization(java.lang.Object, java.lang.String)  。与此同时,如果是要通过代理包装bean,一般就实现接口postProcessAfterInitialization(java.lang.Object, java.lang.String) 。

Typically, post-processors that populate beans via marker interfaces or the like will implement postProcessBeforeInitialization(java.lang.Object, java.lang.String),while post-processors that wrap beans with proxies will normally implement postProcessAfterInitialization(java.lang.Object, java.lang.String).

2.1 


PreferencesPlaceholderConfigurer

子类PropertyPlaceholderConfigurer 是从JDK 1.4 开始支持的API(java.util.prefs)

支持系统路径和用户的参数树。同样支持传统的在placeholders中指定的路径("myPath/myPlaceholderKey")。如果没有指定则使用它的root节点。

你可能感兴趣的:(Spring 上下文变量的配置、加载 相关内容)