、元数据与一个定制配置BeanFactoryPo

原文:BeanFactoryPostProcessor
The next extension point that we will look at is the
org.springframework.beans.factory.config.BeanFactoryPostProcessor. The
semantics of this interface are similar to those of the BeanPostProcessor, with one major
difference: BeanFactoryPostProcessors operate on the bean configuration metadata; that
is, the Spring IoC container allows BeanFactoryPostProcessors to read the configuration
metadata and potentially change it before the container instantiates any beans other than
BeanFactoryPostProcessors.
You can configure multiple BeanFactoryPostProcessors, and you can control the order in
which these BeanFactoryPostProcessors execute by setting the order property. However,
you can only set this property if the BeanFactoryPostProcessor implements the Ordered
interface. If you write your own BeanFactoryPostProcessor, you should consider implementing
the Ordered interface too. Consult the Javadoc for the BeanFactoryPostProcessor and

Ordered interfaces for more details.

Note
If you want to change the actual bean instances (i.e., the objects that are created from the
configuration metadata), then you instead need to use a BeanPostProcessor (described
above in the section called “Customizing beans using a BeanPostProcessor”). While it is
technically possible to work with bean instances within a BeanFactoryPostProcessor
(e.g., using BeanFactory.getBean()), doing so causes premature bean instantiation,
violating the standard container lifecycle. This may cause negative side effects such as
bypassing bean post processing.
Also, BeanFactoryPostProcessors are scoped per-container. This is only relevant if
you are using container hierarchies. If you define a BeanFactoryPostProcessor in one
container, it will only be applied to the bean definitions in that container. Bean definitions in
one container will not be post-processed by BeanFactoryPostProcessors in another
container, even if both containers are part of the same hierarchy.
A bean factory post-processor is executed automatically when it is declared inside an
ApplicationContext, in order to apply changes to the configuration metadata that define
the container. Spring includes a number of predefined bean factory post-processors, such as
PropertyOverrideConfigurer and PropertyPlaceholderConfigurer. A custom
BeanFactoryPostProcessor can also be used, for example, to register custom property editors.

An ApplicationContext automatically detects any beans that are deployed into it that implement
the BeanFactoryPostProcessor interface. It uses these beans as bean factory post-processors,
at the appropriate time. You can deploy these post-processor beans as you would any other bean.
Note
As with BeanPostProcessors, you typically do not want to configure
BeanFactoryPostProcessors for lazy initialization. If no other bean references
a Bean(Factory)PostProcessor, that post-processor will not get instantiated
at all. Thus, marking it for lazy initialization will be ignored, and the
Bean(Factory)PostProcessor will be instantiated eagerly even if you set the
default-lazy-init attribute to true on the declaration of your <beans /> element.
Example: the PropertyPlaceholderConfigurer
You use the PropertyPlaceholderConfigurer to externalize property values from a bean
definition in a separate file using the standard Java Properties format. Doing so enables the person
deploying an application to customize environment-specific properties such as database URLs and
passwords, without the complexity or risk of modifying the main XML definition file or files for the
container.
Consider the following XML-based configuration metadata fragment, where a DataSource with
placeholder values is defined. The example shows properties configured from an external Properties
file. At runtime, a PropertyPlaceholderConfigurer is applied to the metadata that will replace
some properties of the DataSource. The values to replace are specified as placeholders of the form
${property-name} which follows the Ant / log4j / JSP EL style.
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:com/foo/jdbc.properties"/>
</bean>
<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
The actual values come from another file in the standard Java Properties format:
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=sa
jdbc.password=root
Therefore, the string ${jdbc.username} is replaced at runtime with the value 'sa', and
the same applies for other placeholder values that match keys in the properties file. The
PropertyPlaceholderConfigurer checks for placeholders in most properties and attributes of
a bean definition. Furthermore, the placeholder prefix and suffix can be customized.

With the context namespace introduced in Spring 2.5, it is possible to configure property placeholders
with a dedicated configuration element. One or more locations can be provided as a comma-separated
list in the location attribute.
<context:property-placeholder location="classpath:com/foo/jdbc.properties"/>
The PropertyPlaceholderConfigurer not only looks for properties in the Properties
file you specify. By default it also checks against the Java System properties if it cannot
find a property in the specified properties files. You can customize this behavior by setting the
systemPropertiesMode property of the configurer with one of the following three supported
integer values:
• never (0): Never check system properties
• fallback (1): Check system properties if not resolvable in the specified properties files. This is the
default.
• override (2): Check system properties first, before trying the specified properties files. This allows
system properties to override any other property source.
Consult the Javadoc for the PropertyPlaceholderConfigurer for more information.

译文:BeanFactoryPostProcessor下一个扩展点,我们将看看的org.springframework.beans.factory.config.BeanFactoryPostProcessor。的这个接口的语义BeanPostProcessor类似,与一个专业区别:beanfactorypostprocessor bean配置元数据操作;是,Spring IoC容器允许beanfactorypostprocessor读取配置元数据和可能改变它在容器实例化以外的任何beanbeanfactorypostprocessor。您可以配置多个beanfactorypostprocessor,你可以控制的顺序这些beanfactorypostprocessor执行设定的顺序属性。然而,你只能设置这个属性如果BeanFactoryPostProcessor实现了命令接口。如果您编写自己的BeanFactoryPostProcessor,您应该考虑实现命令接口。Javadoc BeanFactoryPostProcessor和咨询命令接口的详细信息。

请注意如果你想改变(即实际的bean实例。,从创建的对象配置元数据),那么您需要使用一个BeanPostProcessor(描述以上章节“定制bean使用BeanPostProcessor”)。虽然它是技术上可能与在BeanFactoryPostProcessor bean实例(如。,使用BeanFactory.getBean()),这样做会导致过早的bean实例化,违反标准容器的生命周期。这可能导致负面影响等绕过bean后处理。另外,beanfactorypostprocessor作用域每个集装箱。这是只适用您正在使用容器层次结构。如果你定义一个BeanFactoryPostProcessor容器,它将只适用于集装箱的bean定义。Bean定义一个容器不会beanfactorypostprocessor后期处理的另一个容器,即使两个容器相同层次结构的一部分。一个bean工厂后处理器执行时自动宣布在一个ApplicationContext,以应用更改配置元数据定义容器。春天包含许多预定义的bean工厂后处理器,如PropertyOverrideConfigurer和来完成。一个自定义BeanFactoryPostProcessor也可以被使用,例如,注册自定义属性编辑器。

ApplicationContext自动检测任何bean的部署到它实现BeanFactoryPostProcessor接口。它使用这些bean作为bean工厂后处理器,在适当的时候。您可以部署这些后处理器bean将其他bean。请注意与BeanPostProcessors一样,你通常不希望配置beanfactorypostprocessor延迟初始化。如果没有其他bean引用Bean(工厂)后处理程序,后处理器将不会得到实例化在所有。因此,标记它为延迟初始化将被忽略,实例化Bean(工厂)后处理程序将热切即使你设置default-lazy-init属性为true的声明< bean / >元素。来完成的您使用来完成外部化bean的属性值使用标准的Java属性定义在一个单独的文件格式。这样做使人部署应用程序定制数据库url和等与环境相关的属性密码,没有修改的复杂性或风险主要的XML定义文件或文件容器。考虑下面的基于xml的配置元数据片段,其中数据源定义占位符的值。从外部配置的示例显示了属性的属性文件。在运行时,公关

在Spring 2.5中引入的上下文名称空间,可以配置属性占位符使用一个专用的配置元素。一个或多个位置可以提供为逗号分隔位置属性列表。<上下文:property-placeholder位置= " classpath:com/foo/jdbc.properties " / >来完成,不仅查找属性的属性您指定的文件。默认情况下它还检查对Java系统属性,如果它不能找到一个属性在属性文件中指定。通过设置您可以定制这一行为systemPropertiesMode属性的配置支持以下三种之一整型值:•从不(0):检查系统属性•后退(1):检查系统属性如果无法在指定的属性文件。这是一个违约。•覆盖(2):首先检查系统属性,然后在指定的属性文件。这允许系统属性来覆盖任何其他财产来源。咨询更多信息来完成的Javadoc。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章