Spring drip: The back processor in spring Beanpostprocessor explained

Source: Internet
Author: User

Beanpostprocessor interface Function:

If we want to complete the bean instantiation, configuration, and other initialization methods before and after the spring container, add some of our own logic processing. We need to define one or more Beanpostprocessor interface implementation classes, and then register them in the Spring IOC container.

 Packagecom.test.spring;Importorg.springframework.beans.BeansException;ImportOrg.springframework.beans.factory.config.BeanPostProcessor;/*** Bean Post processor *@authorZSS **/ Public classPostprocessorImplementsBeanpostprocessor {@Override PublicObject Postprocessbeforeinitialization (Object bean, String beanname)throwsbeansexception {if("Narcodeservice". Equals (Beanname)) {//filter out Bean instance ID to narcodeservice            returnBean; } System.out.println ("Post processor processing bean=" "+beanname+" "Start"); Try{Thread.Sleep (1000); } Catch(interruptedexception e) {e.printstacktrace (); }        returnBean; } @Override PublicObject Postprocessafterinitialization (Object bean, String beanname)throwsbeansexception {if("Narcodeservice". Equals (Beanname)) {            returnBean; } System.out.println ("Post processor processing bean=" "+beanname+" "Complete!"); Try{Thread.Sleep (1000); } Catch(interruptedexception e) {e.printstacktrace (); }        returnBean; }}

Note: The two methods in the interface cannot return NULL if NULL is returned then the null pointer exception is reported in subsequent initialization methods or the Bena instance object is not obtained through the Getbean () method
Because the post processor takes the bean instance object out of the spring IOC container and does not put it back in the IOC container again

Configure spring's post processor postprocessor in the spring configuration file

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "><!--define a bean--<bean id= "Narcodeservice"class= "Com.test.service.impl.NarCodeServiceImpl" > </bean> <bean id= "Beanlifecycle"class= "Com.test.spring.BeanLifecycle" init-method= "Init" destroy-method= "close" > <property name= "name" value= "Zhang San" &  gt;</property> <property name= "sex" value= "male" ></property> </bean> <!--Spring Rear processor --<bean id= "Postprocessor"class= "Com.test.spring.PostProcessor"/></beans>

Beanpostprocessor Api:

 Public InterfaceBeanpostprocessor {/*** Apply This beanpostprocessor to the given new beans instance <i>before</i> any bean * Initiali Zation callbacks (like Initializingbean ' s {@codeAfterpropertiesset} * or a custom init-method).         The bean would already is populated with property values. */      //instantiation, Dependency injection complete, completion of some custom initialization tasks before invoking the initialization of the displayObject Postprocessbeforeinitialization (Object bean, String beanname)throwsbeansexception; /*** Apply This beanpostprocessor to the given new beans instance <i>after</i> any bean * Initializ ation callbacks (like Initializingbean ' s {@codeAfterpropertiesset} * or a custom init-method).            The bean would already is populated with property values. */      //instantiation, Dependency injection, execution at completion of initializationObject Postprocessafterinitialization (Object bean, String beanname)throwsbeansexception; }

As you can see from the API:
1: The Postprocessorbeforeinitailization method of the post processor is the bean instantiation, Dependency injection, and the custom initialization method ( For example: The bean tag in the configuration file adds the Init-method attribute to specify the initialization method in the Java class,
@PostConstruct annotations Specify initialization methods, Java classes implement Initailztingbean interfaces) before calling
2: The Postprocessorafterinitailization method of the post processor is called after the bean instantiation, Dependency injection, and custom initialization method

Attention:
The 1.BeanFactory and applicationcontext two containers are slightly different from the post processor of the bean. The ApplicationContext container automatically detects that the Java classes corresponding to those beans in the spring configuration file implement the Beanpostprocessor
interfaces, and automatically registers them as a post processor. They are called during the creation of the bean, so deploying a post processor is not much different from a normal bean.

The 2.BeanFactory container registers the bean after the processor must be registered through code display, the registration method is defined in the Configurablebeanfactory interface in the IOC container inheritance system

 /**   * Add a new beanpostprocessor that wil L get applied to beans created * by this factory.       To is invoked during factory configuration. * <p>note:post-processors submitted here is applied in the order of * registration; Any ordering semantics expressed through implementing the * {  @link   org.springframework.core.Ordered} interface'll be ignored. Note * that autodetected post-processors (e.g. as beans in an applicationcontext) * would always be applied aft       ER programmatically registered ones. *   @param   Beanpostprocessor the Post-processor to register  */ void addbeanpostprocessor (Beanpostprocessor beanpostprocessor); 

Spring drip: The back processor in spring Beanpostprocessor explained

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.