Spring (3.2.3), spring3.2.3
Spring containers can manage the lifecycle of beans in the singleton scope. containers can track the creation and destruction of Bean instances. There are two main opportunities to manage Bean lifecycle behaviors:
After Bean dependency Injection
Between beans to be destroyed
Behavior after dependency Injection
There are three ways to execute specific actions after all Bean attributes are set successfully:
Implement the org. springframework. beans. factory. InitializingBean Interface
Use the init-method attribute
Use the @ PostConstruct Annotation
Example of InitializingBean interface implementation
Bean definition:
public class ExampleBean implements InitializingBean { private String field1; private String field2; public void setField1(String field1) { this.field1 = field1; System.out.println("field1 was set."); } public void setField2(String field2) { this.field1 = field2; System.out.println("field2 was set."); } public ExampleBean() { System.out.println("In ExampleBean Constructor."); } public void afterPropertiesSet() throws Exception { System.out.println("All properties were set."); } }
Spring Configuration:
<bean id="eb" class="com.huey.dream.bean.ExampleBean"> <property name="field1" value=""/> <property name="field2" value=""/></bean>
Test method:
@Testpublic void testLifecycle() throws Exception { ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");}
Result output:
In ExampleBean Constructor.field1 was set.field2 was set.All properties were set.
Example of using the init-method attribute
Bean definition:
public class ExampleBean { private String field1; private String field2; public void setField1(String field1) { this.field1 = field1; System.out.println("field1 was set."); } public void setField2(String field2) { this.field1 = field2; System.out.println("field2 was set."); } public ExampleBean() { System.out.println("In ExampleBean Constructor."); } public void init() throws Exception { System.out.println("In init method."); } }
Spring Configuration:
<bean id="eb" class="com.huey.dream.bean.ExampleBean" init-method="init"> <property name="field1" value=""/> <property name="field2" value=""/></bean>
Use the @ PostConstruct Annotation
Bean definition:
public class ExampleBean { private String field1; private String field2; public void setField1(String field1) { this.field1 = field1; System.out.println("field1 was set."); } public void setField2(String field2) { this.field1 = field2; System.out.println("field2 was set."); } public ExampleBean() { System.out.println("In ExampleBean Constructor."); } @PostConstruct public void init() throws Exception { System.out.println("In init method."); } }
Spring Configuration:
<bean id="eb" class="com.huey.dream.bean.ExampleBean"> <property name="field1" value=""/> <property name="field2" value=""/></bean>
Behavior before Bean destruction
Similar to custom initialization, there are also three methods to execute a specific behavior before the Bean instance is destroyed:
Implement the org. springframework. beans. factory. DisposableBean Interface
Use the destroy-method attribute
Use the @ PreDestroy Annotation