The life cycle of beans in an IOC container

Source: Internet
Author: User

First, Bean life cycle

The Spring IOC container manages the bean's life cycle, allowing custom tasks to be performed at specific points in the bean's life cycle.

The process by which the Spring IOC container manages the bean's life cycle is as follows:

    1. To create a bean instance from a constructor or factory method
    2. Setting values and references to other beans for the Bean's properties
    3. Invoking the Bean's initialization method
    4. Beans can use the
    5. Call the Bean's Destroy method when the container is closed

Set the Init-method and Destroy-method properties in the bean's declaration, specifying the initialization and destruction methods for the bean.

The following example is implemented as follows:

First, write a person class:

 Public classperson{String name; intAge ;    String sex;  PublicString GetName () {returnname; }     Public voidsetName (String name) {System.out.println ("Execute Name Property");  This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     PublicString Getsex () {returnsex; }     Public voidsetsex (String sex) { This. Sex =sex; } @Override PublicString toString () {return"Person [name=" + name + ", age=" + Age + ", sex=" + Sex + "]"; }     Public voidinit () {System.out.println ("Execute the Initialization Method!" "); }     PublicPerson () {System.out.println ("Execute constructor"); }     Public voiddestory () {System.out.println ("Execute Destruction Method"); }     Public voidFun () {System.out.println ("Using Bean instance"); }}

The configuration file Bean-cycle.xml is:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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" >    <BeanID= "Person"class= "Com.test.Person"Init-method= "Init"Destroy-method= "Destory">        < Propertyname= "Name"value= "Xujian"></ Property>        < Propertyname= "Age"value= "All"></ Property>        < Propertyname= "Sex"value= "Male"></ Property>    </Bean></Beans>

Test function:

 Public class main{    publicstaticvoid  Main (string[] args)    {        Classpathxmlapplicationcontext CTX=new classpathxmlapplicationcontext ("Bean-cycle.xml");        person person = (person) ctx.getbean ("person");
Person.fun (); Ctx.close (); }}

Operation Result:

  

As you can see, you first create an instance of the bean through a constructor, then set the property through the setter method, execute the Init initialization method before using the bean instance, and then execute the Destroy method.

Second, create the Bean post processor

Bean post processing allows for additional processing of the bean before and after invoking the initialization method, and the Bean post processor handles all bean instances of the IOC container one by one rather than a single instance. The typical application is to check the correctness of bean properties or change the Bean's properties according to specific criteria.

For the Bean post processor, the interface Beanpostprocessor needs to be implemented, and before and after the initialization method is called, Spring will pass each bean instance to the following two methods of the above interface respectively:

   

After adding the Bean post processor, the process of the Spring IOC container's lifecycle management of the Bean is:

    1. To create a Bean instance from a constructor or factory method
    2. Setting values and references to other beans for the Bean's properties
    3. Postprocessbeforeinitialization method for passing a bean instance to the Bean's post processor
    4. Invoking the Bean's initialization method
    5. Postprocessafterinitialization method for passing a bean instance to the Bean's post processor
    6. Beans can use the
    7. Call the Bean's Destroy method when the container is closed

Here is the above example to modify, first write a mybeanpostprocessor class and implement the Beanpostprocessor interface, the code is as follows:

 Public classMybeanpostprocessorImplementsbeanpostprocessor{@Override PublicObject Postprocessbeforeinitialization (Object bean, String beanname)throwsbeansexception {System.out.println ("Postprocessbeforeinitialization" +bean+ "," +beanname); returnBean; } @Override PublicObject Postprocessafterinitialization (Object bean, String beanname)throwsbeansexception {System.out.println ("Postprocessafterinitialization" +bean+ "," +beanname); returnBean; }}

Then add the configuration of this class in the Bean-cycle.xml

  

Run the test function again with the following results:

  

  

The life cycle of beans in an IOC container

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.