Spring生命周期回调
Spring Framework提供了几个回调接口,以更改容器中bean的行为;他们包括 InitializingBean和 DisposableBean。
Spring Bean 的生命周期很容易理解。
当实例化Bean时,可能需要执行一些初始化以将其纳入可用状态。
同样,当Bean不再需要并且从容器中删除时,可能需要一些清理。
初始化回调:
实施 org.springframework.beans.factory.InitializingBean接口允许bean在容器设置bean上的所有必要属性后执行初始化工作。
这 InitializingBean接口指定完全的方法:
void afterPropertiesSet() throws Exception;
一般来说,使用 InitializingBean可以避免界面,实际上令人沮丧,因为它不必要地将代码耦合到Spring.You必须使用 afterPropertiesSet(),我们不能更改方法名称。
例如:替代方案:基于XML的配置元数据。
这是使用的 'init-method' 标签属性。
它提供了更改方法名称的灵活性。
<bean id="countryBean" class="org.igi.javapostsforlearning.Country" init-method="init"
public class Country{
public void init() {
//do some initialization work
}
}
...与...完全相同
<bean id="countryBean" class="org.igi.javapostsforlearning.Country"
public class Country implements InitializingBean {
public void afterPropertiesSet() {
//do some initialization work
}
}
...但不将代码耦合到Spring。
销毁回调:
实施 org.springframework.beans.factory.DisposableBean接口允许bean当包含它被销毁的容器时,才能获得回调。
这 DisposableBean接口指定单个方法:
void destroy() throws Exception;
一般来说,使用 DisposableBean可以避免界面,实际上令人沮丧,因为它不必要地将代码耦合到Spring.You必须使用 destroy(),我们不能更改方法名称。
例如:替代方案:基于XML的配置元数据。
这是使用的 'destroy-method' 标签属性。
它提供了更改方法名称的灵活性。
<bean id="countryBean" class="org.igi.javapostsforlearning.Country" init-method="destroy"
public class Country{
public void destroy() {
//do some destruction work(like releasing pooled connections)
}
}
...与...完全相同
<bean id="countryBean" class="org.igi.javapostsforlearning.Country"
public class Country implements DisposableBean{
public void destroy() {
//do some destruction work(like releasing pooled connections)
}
}
...但不将代码耦合到Spring。
Spring生命周期回调示例:
在Eclipse IDE中配置Spring,请参阅Hello World示例
1.Country.java.
这是一个简单的pojo类,其中一些属性所以这里的国家有名字。
在package org.igi.javapostssforlarearning中创建country.java .Copy内容到Country.java之后。
package org.igi.javapostsforlearning;
public class Country {
String countryName ;
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public void init()
{
System.out.println("In init block of country");
}
public void destroy()
{
System.out.println("In destroy block of country");
}
}
2.ApplicationContext.xml.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="country" class="org.igi.javapostsforlearning.Country" init-method="init" destroy-method="destroy"> <property name="countryName" value="Netherlands" </bean> </beans>
3.lifetimecallbacksmain.java.
此类包含Main Function.Create LifetimeCallBacksmain.java在package org.igi.javapostsforlearning。
内容到LifetimeCallBacksmain.java之后的内容.copy
package org.igi.javapostsforlearning;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class LifetimeCallbacksMain{
public static void main(String[] args) {
AbstractApplicationContext appContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
Country countryObj = (Country) appContext.getBean("country");
System.out.println("Country Name: "+countryObj.getCountryName());
appContext.registerShutdownHook();
}
}
其中我们需要注册在抽象应用程序上声明的关机钩注册器HUTDOWNHOOK()方法。
这将确保优雅的关机并调用相关的Destroy方法。
4.运行
当我们将运行上面的应用程序时,我们将按照输出作为输出。
In init block of country Country Name: Netherlands In destroy block of country
默认初始化和销毁方法:
如果我们有太多的Beans具有具有相同名称的初始化和或者销毁方法,则无需在每个单个bean上声明init-method和destroy-方法。
相反,Framework提供了在元素上使用默认init-method和default-destroy-methation属性配置此类情况的灵活性,如下所示:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" default-init-method="init" default-destroy-method="destroy" > <bean id="country" class="org.igi.javapostsforlearning.Country"> <property name="countryName" value="Netherlands" </bean> </beans>

