Spring4.0 Study Notes (5)--Managing the bean's life cycle

Source: Internet
Author: User

Spring IOC Container manages bean lifecycle, Spring allows custom tasks to be performed at specific points in the bean lifecycle

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

1. Create an instance of the bean through a constructor or factory method

2. Set values for bean properties and references to other beans

3. Initialize method of invoking Bean

4, beans can use the

5. When the container is closed, the method of destroying the bean is called

Bean file

 Packagecom.spring.cycle; Public classCar { PublicCar () {System.out.println ("Car ' s constructor ..."); } @Override PublicString toString () {return"Car [brand=" + Brand + "]"; }    PrivateString brand;  Public voidSetbrand (String brand) {System.out.println ("Setbrand ...");  This. Brand =brand; }         Public voidinit () {System.out.println ("Init ..."); }         Public voiddestroy () {System.out.println ("Destroy ..."); }    }

Configuration file

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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.0.xsd">    <BeanID= "Car"class= "Com.spring.cycle.Car"Init-method= "Init"Destroy-method= "Destroy">        < Propertyname= "Brand"value= "Audi"></ Property>    </Bean>        <Beanclass= "Com.spring.cycle.MyBeanPostProcesser">    </Bean></Beans>
 Packagecom.spring.cycle;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classMain { Public Static voidMain (string[] args) {Classpathxmlapplicationcontext ctx=NewClasspathxmlapplicationcontext ("Bean-cycle.xml"); Car Car= (CAR) ctx.getbean ("Car"));        SYSTEM.OUT.PRINTLN (car); //Close IOC ContainerCtx.close (); }}

Postprocessafterinitialization and Postprocessbeforeinitialization

 Packagecom.spring.cycle;Importorg.springframework.beans.BeansException;ImportOrg.springframework.beans.factory.config.BeanPostProcessor; Public classMybeanpostprocesserImplementsBeanpostprocessor { PublicObject Postprocessafterinitialization (Object arg0, String arg1)throwsbeansexception {System.out.println ("Postprocessafterinitialization" +arg1); returnarg0; }     PublicObject Postprocessbeforeinitialization (Object arg0, String arg1)throwsbeansexception {System.out.println ("Postprocessbeforeinitialization" +arg1); returnarg0; }}

Bean's life cycle

Car ' s constructor ... "Call constructor"
Setbrand ... "Setting properties"
Postprocessbeforeinitializationcar "Before instantiation method"
Init ... "instantiating beans"
Postprocessafterinitializationcar "After instantiation method"
Car [Brand=audi] "Call the ToString () method"
Destroy ... "Destroying beans"

Spring4.0 Study Notes (5)--Managing the bean's life cycle

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.