The life cycle of spring beans

Source: Internet
Author: User

This blog post is the spring life cycle of the detailed, currently limited to the author's own level for some of the content may only know what it is, so if there are errors in the blog to welcome you point out, at the same time I will gradually upgrade their level, and strive to be able to publish some of the benefits of the blog post.

Do not live much, put the code first.

1, class file

The person class is used to demonstrate the underlying class of the bean life cycle.

 PackageCom.spring.beanlife.beans;Importorg.springframework.beans.BeansException;Importorg.springframework.beans.factory.BeanFactory;ImportOrg.springframework.beans.factory.BeanFactoryAware;ImportOrg.springframework.beans.factory.BeanNameAware;ImportOrg.springframework.beans.factory.InitializingBean;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.ApplicationContextAware; Public classPersonImplementsBeannameaware,beanfactoryaware,applicationcontextaware,initializingbean {PrivateString name;  PublicPerson () {System.out.println ("Parameterless constructor"); }         PublicPerson (String name) {System.out.println ("Parametric constructor"); Name= This. Name; }     PublicString GetName () {returnname; }     Public voidsetName (String name) {System.out.println ("SetName (String name) method run!!!!! ");  This. Name =name; }         Public voidSayhi () {System.out.println ("HI" +name); } @Override/**the *bean name concerns that the interface can get the ID of the bean configured in the Bean's Id,xml by this method*/     Public voidsetbeanname (String arg0) {System.out.println ("Bean--------->" +arg0); } @Override/*** Bean Factory pays attention to interface through this method can obtain to Beanfactory*/     Public voidSetbeanfactory (Beanfactory arg0)throwsbeansexception {System.out.println (arg0); } @Override/*** ApplicationContext Attention interface Through this method can get to ApplicationContext*/     Public voidSetapplicationcontext (ApplicationContext arg0)throwsbeansexception {System.out.println (arg0); } @Override/*** Call Afterpropertiesset*/     Public voidAfterpropertiesset ()throwsException {System.out.println ("Afterpropertiesset"); }        /***@ Invoking the custom Init method*/     Public voidinit () {System.out.println ("Invoke the Custom Init method"); }        /***@ Calling the custom Destory method*/     Public voiddestory () {System.out.println ("Calling a custom Destory method"); }}
Mybeanpostprocessor implements the class of the Beanpostprocessor post processor
 PackageCom.spring.beanlife.beans;Importorg.springframework.beans.BeansException;ImportOrg.springframework.beans.factory.config.BeanPostProcessor; Public classMybeanpostprocessorImplementsBeanpostprocessor {@Override PublicObject Postprocessafterinitialization (Object arg0, String arg1)throwsbeansexception {System.out.println ("After method of the post processor"); returnarg0; } @Override PublicObject Postprocessbeforeinitialization (Object arg0, String arg1)throwsbeansexception {System.out.println ("Before method for post processor"); returnarg0; }}

The main function method of the app test life cycle

 Packagecom.spring.beanlife.test;Importorg.springframework.beans.factory.BeanFactory;Importorg.springframework.beans.factory.xml.XmlBeanFactory;ImportOrg.springframework.core.io.ClassPathResource;ImportCom.spring.beanlife.beans.Person; Public classApp { Public Static voidMain (string[] args) {/*@ by the way of the core container **/        /*ApplicationContext applicationcontext = new Classpathxmlapplicationcontext ("Com/spring/beanlife/beans.xml");        Person person = (person) applicationcontext.getbean ("person"); Person.sayhi ();*/Beanfactory Factory=NewXmlbeanfactory (NewClasspathresource ("Com/spring/beanlife/beans.xml")); Person Person= (person) factory.getbean (' person ');    Person.sayhi (); }}

XML 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"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsdhttp//Www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><bean id= "Person" init-method= "init" destroy-method= "Destory"class= "Com.spring.beanlife.beans.Person" > <property name= "name" value= "Liukun"/> </bean> <bean Id= "Mybeanpostprocessor"class= "Com.spring.beanlife.beans.MyBeanPostProcessor"/></beans>

2,bean Life Cycle Steps (demo in Appliccationcontext container)

1, instantiate (when our program loads the Beans.xml file), instantiate our bean (if scope=singlton) into memory

2, calling the Set method setting properties

3, if you implement the Bean name Attention Interface (Beannameaware), you can get the ID number by setbeanname

4, if you implement the Bean Factory Focus Interface (Beanfactoryaware), you can get beanfactory by setbeanfactory

5, if you implement the Applicationcontextaware Focus interface (Applicationcontextaware), you can get ApplicationContext by Setapplicationcontext

6, if the bean is associated with a post processor (beanpostprocessor), the Postprocessbeforeinitialization (before) method is called

7, if you implement the Initializingbean Attention Interface (Initializingbean), you can call Afterpropertiesse

8, the custom initialization method is called if the initialization method is customized (note that the custom initialization method is configured in the Bean by the Init-method method)

9, if the bean is associated with a post processor (beanpostprocessor), the Postprocessafterinitialization (after) method is called

10,bean can use the

11, Container closed

12, if the implementation of the Disposablebean interface can call its Destory () method, you can also customize the destruction method (note that the custom initialization method in the bean through the Init-method method configuration)

PS: Common operation steps in development 1>2>6>10>9>11

Perform operation results

  

Results with beanfactory operation execution

Conclusion: The life cycle of beanfactory and ApplicationContext is not the same, the Beanfactory life cycle is simple, the related functions of the Post processor are not used, and the core container related methods are not obtained.

The life cycle of spring beans

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.