Beanpostprocessor of spring Container extension point

Source: Internet
Author: User

Original text: Original link

Beanpostprocessor interface

Public interface Beanpostprocessor {public

    object Postprocessbeforeinitialization (Object bean, String beanname) Throws Beansexception;

    public object Postprocessafterinitialization (object bean, String beanname) throws beansexception;

}

Official explanation:

Factory hook that allows for custom modification of the new bean instances, e.g. checking for marker interfaces or wrapping th EM with proxies.
Applicationcontexts can autodetect beanpostprocessor beans in their beans definitions and apply them to any beans Tly created. Plain bean factories allow for programmatic registration of post-processors, applying to all beans created the This FA Ctory.
Typically, post-processors that populate beans via marker interfaces or the like would implement ation (Java.lang.Object, java.lang.String), while post-processors this wrap beans with proxies to normally implementpost Processafterinitialization (Java.lang.Object, java.lang.String).

If an implementation class for this interface is registered to a container, each managed bean of the container obtains a callback to the interface implementation class before invoking the initialization method. When a container invokes an interface-defined method, the instance and first name of the managed bean are passed through the parameter to the method, which is then returned to the container by the return value of the method.

public class Examplebeanpostprocessor implements initializingbean{public
    void Initmethod () {
        System.out.println ("Examplebeanpostprocessor init-method Initmethod");
    }

    @Override public
    void Afterpropertiesset () throws Exception {
        System.out.println ("Examplebeanpostprocessor Initializingbean afterpropertiesset ");
    }
}
public class Beanpostprocessorimpl implements beanpostprocessor{

    @Override the public
    Object Postprocessafterinitialization (Object Bean, String beanname) throws Beansexception {
        System.out.println ("Bean" + Beanname+ "Init after pretreatment");
        return bean;

    @Override Public
    Object Postprocessbeforeinitialization (Object bean, String beanname) throws Beansexception {
        System.out.println ("Bean" + beanname+ "pre-init preprocessing")
        ; return bean;
    }

}
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns=
    "http://www.springframework.org/schema/" Beans "
    xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "
    xmlns:p=" http://www.springframework.org/ schema/p "
    xsi:schemalocation=" Http://www.springframework.org/schema/beans http://www.springframework.org/ Schema/beans/spring-beans-3.1.xsd "
    >
    <bean class=" Com.xiang.bean.BeanPostProcessorImpl "></ bean>
    <bean id= "Examplebeanpostprocessor class=" Com.xiang.bean.ExampleBeanPostProcessor "init-method=" Initmethod "></bean>
</beans>

Output:

Bean examplebeanpostprocessor Pre-init preprocessing
examplebeanpostprocessor Initializingbean afterpropertiesset
Examplebeanpostprocessor init-method initmethod
Bean examplebeanpostprocessor Init preprocessing

The scope of the Beanpostprocessor is container-level, and it is related only to the container in which it is located. If you define a beanpostprocessor in a container, it simply resets the bean in this container. It does not perform any processing on the bean that is defined in another container. The beanpostprocessor is executed after The bean's definition file is loaded in the spring container and the bean is instantiated.

Beanfactory and ApplicationContext containers are not registered the same way: if you use Beanfactory, you must display a call to its addbeanpostprocessor () method for registration, The parameter is an instance of the Beanpostprocessor implementation class, and if it is using ApplicationContext, the container automatically looks for the bean that implements the Beanpostprocessor interface in the configuration file, and then registers automatically, All we have to do is configure a beanpostprocessor to implement the class bean.

Beanpostprcessorimpl beanpostprocessor = new Beanpostprcessorimpl ();
Resource Resource = new Filesystemresource ("Applicationcontext.xml");
Configurablebeanfactory factory = new Xmlbeanfactory (resource);
Factory.addbeanpostprocessor (Beanpostprocessor);

If we use more than one Beanpostprocessor implementation class, then how to determine the processing order. In fact, as long as the implementation of the ordered interface, set the Order property can easily determine the different implementation of the processing sequence of classes.

public class Beanpostprocessorimplfirst implements beanpostprocessor,ordered{@Override public object Postprocessafterinitialization (object bean, String beanname) throws Beansexception {System.out.prin
        TLN ("Bean" +beanname+ "pre-init preprocessing first");
    return bean;
        @Override public Object Postprocessbeforeinitialization (object bean, String beanname) throws Beansexception {
        System.out.println ("Bean" +beanname+ "pre-preprocessing before Init");
    return bean;
    @Override public int GetOrder () {return 0; }

}
public class Beanpostprocessorimplsecond implements beanpostprocessor,ordered{@Override
    public int GetOrder () {return 1;
        @Override public Object Postprocessbeforeinitialization (object bean, String beanname) throws Beansexception {
        System.out.println ("Bean" +beanname+ "pre-init preprocessing Second");
    return bean;
        @Override public Object Postprocessafterinitialization (object bean, String beanname) throws Beansexception {
        System.out.println ("Bean" +beanname+ "init preprocessing Second");
    return bean; }

}
<?xml version= "1.0" encoding= "UTF-8"?> "<beans xmlns=" http:// Www.springframework.org/schema/beans "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xmlns:p=" http:// www.springframework.org/schema/p "xsi:schemalocation=" Http://www.springframework.org/schema/beans http:// Www.springframework.org/schema/beans/spring-beans-3.1.xsd "> <bean class=" Com.xiang.bean.BeanPostProcessorI MPL "></bean> <bean class=" Com.xiang.bean.BeanPostProcessorImplFirst "></bean> <bean class=" Com.xiang.bean.BeanPostProcessorImplSecond "></bean> <bean id=" Examplebeanpostprocessor "class=" Com.xiang.bean.ExampleBeanPostProcessor "init-method=" Initmethod ></bean> </beans> 
Pre-preprocessing
first bean examplebeanpostprocessor init preprocessing Second beans after examplebeanpostprocessor init
Examplebeanpo Pre-stprocessor init pretreatment
examplebeanpostprocessor initializingbean afterpropertiesset
Examplebeanpostprocessor init-method initmethod
Bean examplebeanpostprocessor init pre-preprocessing first
bean Preprocessing Second
Bean examplebeanpostprocessor init after examplebeanpostprocessor Init
Priority high first execution, priority than no priority

to perform beanpostprocessor first and post processing methods in the return of the bean, cannot be null

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.