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