、元數據與一個定製配置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。

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