The life cycle of spring beans and the back processor of the bean

Source: Internet
Author: User

Bean's life cycle:

The Spring IOC container can manage the Bean's life cycle,
Spring allows custom tasks to be performed at specific points in the Bean life cycle.
The process by which the Spring IOC container manages the Bean's life cycle:
1 Creating a Bean instance from a constructor or factory method
2 setting values and references to other beans for the Bean's properties
3 Call the Bean initialization method (can be specified, using the bean tag's Inti-method property to specify the initialization method, do not specify ignore this step by default) beans can use the
4 when the container is closed, call the Bean's destruction method (can be specified, using the Bean label's Destroy-method property to specify the destruction method, not specified by default ignore this step)

code example:
Bean Instance Code:

 Packagecom.jeremy.spring.cycle; Public classPerson {PrivateString Name;  Public voidsetName (String name) {Name=name; System.out.println ("The Setter ....."); }         PublicPerson () {System.out.println ("Person constructor ..."); }                 Public voidinit () {System.out.println ("Init ..."); }         Public voiddestory () {System.out.println ("Desdtory ..."); } @Override PublicString toString () {return"Person [name=" + Name + "]"; }

After Init-method and Destroy-method are configured

Test code:

    @Test    publicvoid  testnoprocessorcycle () {        Configurableapplicationcontext Cacontext=new classpathxmlapplicationcontext ("Bean-cycle.xml");        person person = (Com.jeremy.spring.cycle.person) cacontext.getbean ("person");        Cacontext.close ();    }

Test results:

Person constructor ....
Setter............
Init......
2014-10-14 19:49:36 Org.springframework.context.support.AbstractApplicationContext Doclose
Info: Closing org[email protected]8965fb:startup Date [Tue Oct 19:49:35 CST 2014]; Root of context Hierarchy
Desdtory ...

Prove that the bean declaration process is as described above

1 Why the Post processor of the bean is required (which is used before and after the container invokes the Bean's Init () method)---Once the IOC has configured the Bean's post processor it is valid for all beans in the IOC container where the bean resides.
If you do not configure the Post processor, each time the IOC container directly samples the bean, but if the actual development, we need to add some of their own logic processing after the bean initial configuration and other initialization?? What to do, then we need to use the bean's back processor.

2 To configure the Bean post processor:

1 You need to create a new class to implement the Beanpostprocessor interface, and implement Postprocessafterinitialization (Object arg0, String arg1), Postprocessbeforeinitialization (Object arg0, String arg1) two methods

ARG0: Is the incoming bean object,
Arg1: is the bean ID value
Return value: is to return a bean object, we can also think of modifying the returned Bean object, not necessarily to return the container initialization of the Bean object
Code below:
  

 Packagecom.jeremy.spring.cycle;Importorg.springframework.beans.BeansException;ImportOrg.springframework.beans.factory.config.BeanPostProcessor; Public classMybeanpostprocessorImplementsBeanpostprocessor { Publicmybeanpostprocessor () {//TODO auto-generated Constructor stub} @Override PublicObject Postprocessafterinitialization (Object arg0, String arg1)throwsbeansexception {System.out.println ("Before Init ....." +Arg0.getclass ()); returnarg0; } @Override PublicObject Postprocessbeforeinitialization (Object arg0, String arg1)throwsbeansexception {//TODO auto-generated Method StubSystem.out.println ("After init ......" +Arg0.getclass ()); returnarg0; }}

2 after configuring the processor in the XML configuration file,

<!--Configuring the Bean Post Processor: No configuration id attribute is required, the IOC container recognizes that he is a bean post processor and calls its method--
<bean class= "Com.jeremy.spring.cycle.MyBeanPostProcessor" ></bean>

3 Test Code

@Test      Public void testnoprocessorcycle () {        configurableapplicationcontext cacontext=new Classpathxmlapplicationcontext ("Bean-cycle.xml");        person person = (Com.jeremy.spring.cycle.person) cacontext.getbean ("person");        Cacontext.close ();    }

Operation Result:
This is the life flow process with the Post processor bean as follows:

Person constructor ....
Setter............

Before Init...........class Com.jeremy.spring.cycle.person
Init...................
After Init...........class Com.jeremy.spring.cycle.person
Destory ........ ........

The life cycle of spring beans and the back processor of the bean

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.