Spring container extension point: Bean back processor

Source: Internet
Author: User
Tags class definition
Beanpostprocessor (Bean post processor) is commonly used to modify the value inside the bean, implement the bean dynamic proxy, and so on.

Both Beanfactorypostprocessor and Beanpostprocessor are the extension points exposed when spring initializes the bean. But what difference do they have?
The graph of the Understanding bean life cycle shows that Beanfactorypostprocessor is the earliest called in the life cycle, much earlier than Beanpostprocessor. It executes before the bean is instantiated, after the spring container loads the bean's definition file. That is, Spring allows beanfactorypostprocessor to read bean configuration metadata and modify it before the container creates a bean. For example, to increase the bean's properties and values, to reset the bean as a candidate for automatic assembly, resetting the bean's dependencies, and so on.

You can configure multiple beanfactorypostprocessor at the same time in the Srping configuration file and control the order of execution for each beanfactorypostprocessor by setting the ' order ' property when registering in XML.

The Beanfactorypostprocessor interface is defined as follows:

Public interface Beanfactorypostprocessor {    void Postprocessbeanfactory (configurablelistablebeanfactory Beanfactory) throws beansexception;}

Interface has only one method postprocessbeanfactory. The parameter of this method is configurablelistablebeanfactory type, in actual development, we often use its getbeandefinition () method to get the metadata definition of a bean: beandefinition. It has these methods:

Look at an example:
A bean is defined in the configuration file:

<bean id= "Messi" class= "Twm.spring.LifecycleTest.footballPlayer" >    <property name= "name" value= "Messi" ></property>    <property name= "Team" value= "Barcelona" ></property></bean>

Create class Beanfactorypostprocessorimpl to implement Interface Beanfactorypostprocessor:

public class Beanfactorypostprocessorimpl implements beanfactorypostprocessor{public    void Postprocessbeanfactory (Configurablelistablebeanfactory beanfactory) throws beansexception {        System.out.println ("Beanfactorypostprocessorimpl");        Beandefinition bdefine=beanfactory.getbeandefinition ("Messi");        System.out.println (Bdefine.getpropertyvalues (). toString ());        Mutablepropertyvalues PV =  bdefine.getpropertyvalues ();              if (Pv.contains ("team")) {                propertyvalue ppv= pv.getpropertyvalue ("name");                Typedstringvalue obj= (Typedstringvalue) ppv.getvalue ();                if (Obj.getvalue (). Equals ("Messi")) {                    pv.addpropertyvalue ("team", "Argentina");                  }        }              Bdefine.setscope (Beandefinition.scope_prototype);    }}

Call class:

public static void Main (string[] args) throws Exception {    ApplicationContext ctx = new Classpathxmlapplicationcontext ("Beans.xml");    Footballplayer obj = Ctx.getbean ("Messi", footballplayer.class);    System.out.println (Obj.getteam ());}

Output:

propertyvalues:length=2; Bean property ' name '; Bean Property ' Team '
Argentina

The Propertyplaceholderconfigurer class referred to in the Propertyplaceholderconfigurer application is an implementation of the Beanfactorypostprocessor interface. It replaces the placeholder (such as ${jdbc.url}) in the class definition with the contents of the properties file before the container creates the bean.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.