本文目的是通过示例告诉大家sprng bean生命周期的控制。
主要通过两个接口InitializingBean、
DisposableBea
,验证bean在容器中初始化以及销毁时候的触发事件。
当然也可以通过配置xml的文件,init-method、
destroy-method等同于上述两个接口
1.InitializingBean and DisposableBean interfaces
package com.javacodegeeks.snippets.enterprise.services; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class Employee implements InitializingBean, DisposableBean { private Long id; private String position; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getPosition() { return position; } public void setPosition(String position) { this.position = position; } @Override public String toString() { return "id " + id + " and position " + position; } @PostConstruct public void initIt() throws Exception { System.out.println("Init method after properties are set : " + id + " " + position); } @PreDestroy public void cleanUp() throws Exception { System.out.println("Spring Clean Up! Employee is cleaned up now."); } @Override public void destroy() throws Exception { System.out.println("I am in destroy... "); } @Override public void afterPropertiesSet() throws Exception { System.out.println("I am in afterPropertiesSet... "); } }
2.Use of init-method and destroy-method attributes in bean definition
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd"> <bean id="employeeBean" class="com.javacodegeeks.snippets.enterprise.services.Employee" init-method="initIt" destroy-method="cleanUp"> <property name="id" value="123"/> <property name="position" value="marketing"/> </bean> </beans>
3.@PostConstruct and @PreDestroy annotations
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd"> <context:annotation-config /> <bean id="employeeBean" class="com.javacodegeeks.snippets.enterprise.services.Employee"> <property name="id" value="123"/> <property name="position" value="marketing"/> </bean> </beans>
4.OutPut