Spring (3.2.3)-Beans (10): Life cycle

Source: Internet
Author: User

The Spring container manages the life cycle of the singleton scope Bean, which enables the container to track the creation and destruction of bean instances. There are two main opportunities to manage Bean lifecycle behavior:
After the Bean dependency has been injected
About to destroy between beans

Behavior after Dependency injection

There are three ways to perform a specific behavior after all of the Bean's property settings are successful:
Implementing the Org.springframework.beans.factory.InitializingBean Interface
Using the Init-method property
Using @PostConstruct annotations

Example of implementing the Initializingbean interface

The Bean definition:

 Public classExamplebeanImplements Initializingbean {PrivateString field1; PrivateString Field2;  Public voidsetField1 (String field1) { This. field1 =field1; System.out.println ("Field1 was set."); }         Public voidsetField2 (String field2) { This. field1 =Field2; System.out.println ("Field2 was set."); }         PublicExamplebean () {System.out.println ("In Examplebean Constructor."); }    Public   void afterpropertiesset () throws  Exception {System.out.println ("all Properties were set. "); }    }

Spring configuration:

<BeanID= "EB"class= "Com.huey.dream.bean.ExampleBean">    < Propertyname= "Field1"value=""/>    < Propertyname= "Field2"value=""/></Bean>

Test method:

@Test  Public void throws Exception {    =           new classpathxmlapplicationcontext ("Applicationcontext.xml");}

Result output:

In Examplebean Constructor.field1 is Set.field2 was set. All properties were set.
Example of using the Init-method property

The Bean definition:

 Public classExamplebean {PrivateString field1; PrivateString Field2;  Public voidsetField1 (String field1) { This. field1 =field1; System.out.println ("Field1 was set."); }         Public voidsetField2 (String field2) { This. field1 =Field2; System.out.println ("Field2 was set."); }         PublicExamplebean () {System.out.println ("In Examplebean Constructor."); }    Public   void init () throws  Exception {System.out.println ("in Init method.") ); }    }

Spring configuration:

<BeanID= "EB"class= "Com.huey.dream.bean.ExampleBean" init-method= "Init" >    < Propertyname= "Field1"value=""/>    < Propertyname= "Field2"value=""/></Bean>
Using @PostConstruct annotations

The Bean definition:

 Public classExamplebean {PrivateString field1; PrivateString Field2;  Public voidsetField1 (String field1) { This. field1 =field1; System.out.println ("Field1 was set."); }         Public voidsetField2 (String field2) { This. field1 =Field2; System.out.println ("Field2 was set."); }         PublicExamplebean () {System.out.println ("In Examplebean Constructor."); } @PostConstruct  Public voidInit ()throwsException {System.out.println ("In Init method."); }    }

Spring configuration:

<BeanID= "EB"class= "Com.huey.dream.bean.ExampleBean">    < Propertyname= "Field1"value=""/>    < Propertyname= "Field2"value=""/></Bean>

Behavior before the Bean is destroyed

Similar to the custom initialization behavior, there are three ways to perform a specific behavior before the Bean instance is destroyed:
Implementing the Org.springframework.beans.factory.DisposableBean Interface
Using the Destroy-method property
Using @PreDestroy annotations

Spring (3.2.3)-Beans (10): 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.