The life cycle of the bean in the Spring application context

Source: Internet
Author: User

The bean is loaded into the life cycle of the spring application context,

Beans have undergone several stages from creation to destruction in the spring container, each of which can be customized to how spring manages beans, and the following code validates the lifecycle and personalization methods;

Beanlife implements aware interfaces, Initializingbean, Disposablebean interfaces, and custom life cycle methods.

/*** @name Bean life cycle*/ Public classBeanlifeImplementsbeannameaware,beanfactoryaware,applicationcontextaware,initializingbean,disposablebean{PrivateString Beanproperty;//Private Properties        PrivateString Beanname;//receive Beannameaware of Beanname incoming    PrivateBeanfactory beanfactory;//receive Beanfactoryaware of beanfactory incoming    PrivateApplicationContext ApplicationContext;//receive Applicationcontextaware of ApplicationContext incoming        /*** Constructor Function*/     PublicBeanlife (String beanproperty) {System.out.println ("Beanlife constructed with" +beanproperty+ "..."); }        /*** Bean attribute get*/     PublicString Getbeanproperty () {returnBeanproperty; }    /*** Bean Fill Properties*/     Public voidSetbeanproperty (String beanproperty) {System.out.println ("Beanlife Setbeanproperty:" +beanproperty);  This. Beanproperty =Beanproperty; }    /*** Init-method Correlation method*/     Public voidbegin () {SYSTEM.OUT.PRINTLN ("Init-method:begin ()"); }    /*** Destroy-method Correlation method*/     Public voidEnd () {System.out.println ("Destroy-method:end ()"); }        /*** Ready bean, use bean to do something*/     Public voiddosomething () {System.out.println ("Beanlife can be used:do something."); }        /*** Spring Beanfactory container instances into*/     Public voidSetbeanfactory (Beanfactory arg0)throwsbeansexception { This. beanfactory=arg0; System.out.println ("Beanfactoryaware---beanlife setbeanfactory:" + This. beanfactory.tostring ()); }        /*** Spring passes the Bean's ID*/     Public voidsetbeanname (String arg0) { This. Beanname =arg0; System.out.println ("Beannameaware---beanlife setbeanname:" + This. Beanname); }        /*** Spring applies a reference to the application context*/     Public voidSetapplicationcontext (ApplicationContext arg0)throwsbeansexception { This. ApplicationContext =arg0; System.out.println ("Applicationcontextaware---beanlife setapplicationcontext:" + This. Applicationcontext.getapplicationname ()); }    /*** After the property has been set*/     Public voidAfterpropertiesset ()throwsException {System.out.println ("Initializingbean---after SetProperties"); }    /*** Bean Destruction*/     Public voidDestroy ()throwsException {System.out.println ("Disposablebean---beanlife Bean destroy."); }}

Customizing a Beanpostprocessor to monitor callback method customization for spring container bean instantiation Injection

/*** @name beanpostprocessor implementation*/ Public classBeanlifepostprocessorImplementsbeanpostprocessor{/*** Instantiation, Dependency injection complete, completion of some custom initialization tasks before invoking the initialization of the display *@paramarg0 Bean Object *@paramID of the arg1 bean*/     PublicObject Postprocessbeforeinitialization (Object arg0, String arg1)throwsbeansexception {System.out.println ("Beanpostprocessor---before" +arg1+ "' s Initialization"); returnarg0; }        /*** Instantiation, Dependency injection, execution at completion of initialization *@paramarg0 Bean Object *@paramID of the arg1 bean*/     PublicObject Postprocessafterinitialization (Object arg0, String arg1)throwsbeansexception {System.out.println ("Beanpostprocessor---after" +arg1+ "' s Initialization"); returnarg0; }    }

Applicationcontext.xml Configuration

class= "Com.wjx.betalot.BeanLife" init-method= "Begin" destroy-method= "End" >    <constructor-arg value= "Beanlife construct param" ></constructor-arg>    class= " Com.wjx.betalot.BeanLifePostProcessor "></bean>

Validation test:

 Public Static void throws exception{        new classpathxmlapplicationcontext ("Classpath:applicationContext.xml");         = (Beanlife) context.getbean ("Beanlife");        Beanlife.dosomething ();         // Destroying Beans (         (classpathxmlapplicationcontext) context). Registershutdownhook ();}

Test results:

 beanlife constructed with Beanlife construct param ... Beanlife setbeanproperty:beanlife cyclebeannameaware ---beanlife Setbeanname: Beanlifebeanfactoryaware ---beanlife setbeanfactory:org.s[email  protected]5eed2fce:defining beans [beanlife,beanpostprocessor]; Root of Factory hierarchyapplicationcontextaware ---beanlife Setapplicationcontext:beanpostprocessor ---Before Beanlife ' s initialization initializingbean---after setpropertiesinit -method:begin () beanpostprocessor ---After Beanlife ' s initializationbeanlife can be used: do   something. Disposablebean ---beanlife Bean Destroy.destroy -method:end () 

It can be validated against the Bean's life cycle graph.

The life cycle of the bean in the Spring application context

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.