您的位置:首页 > 其它

三种不同实现初始化和销毁bean之前进行的操作的比较

2015-05-04 20:38 141 查看
Spring容器中的bean是有生命周期的,Spring 允许在 Bean 在初始化完成后以及 Bean 销毁前执行特定的操作,常用的设定方式有以下三种:

通过实现 InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法;

通过 <bean> 元素的 init-method/destroy-method属性指定初始化之后 /销毁之前调用的操作方法;

在指定方法上加上@PostConstruct 或@PreDestroy注解来制定该方法是在初始化之后还是销毁之前调用。

这是我们就有个疑问,这三种方式是完全等同的吗,孰先孰后?

这里给大家做了一个测试

1.定义相关的实现类:

package com.fdd;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
/**
*  比较不同方式初始化、销毁bean的顺序
*2015年5月4日 下午5:53:07
*chenshunyang
*/
public class InitSequenceService implements InitializingBean,DisposableBean{
/**
* 构造方法
*/
public InitSequenceService(){
System.out.println("InitSequenceService:constructor:"+message);
}
/**
* 业务方法
*
* 2015年5月4日 下午8:11:16
* chenshunyang
*/
public void sayHello(){
System.out.println("hello "+message);
}

/**
* 通过实现DisposableBean接口来实现销毁bean之前的操作
*2015年5月4日 下午8:11:41
* chenshunyang
*/
public void destroy() throws Exception {
System.out.println("InitSequenceService:destroy:"+message);
}
/**
* 通过实现InitializingBean接口来初始化bean
*2015年5月4日 下午8:12:35
* chenshunyang
*/
public void afterPropertiesSet() throws Exception {
System.out.println("InitSequenceService:afterPropertiesSet:"+message);

}

/**
* 通过使用@PostConstruct来实现初始化bean
*
* 2015年5月4日 下午8:12:54
* chenshunyang
*/
@PostConstruct
public void postConstruct1() {
System.out.println("InitSequenceService: postConstruct1:"+message);
}

/**
* 通过使用@PreDestroy来实现销毁bean之前操作
*
* 2015年5月4日 下午8:13:15
* chenshunyang
*/
@PreDestroy
public void preDestroy1(){
System.out.println("InitSequenceService: preDestroy1:"+message);
}

@PostConstruct
public void postConstruct2() {
System.out.println("InitSequenceService: postConstruct2:"+message);
}

@PreDestroy
public void preDestroy2(){
System.out.println("InitSequenceService: preDestroy2:"+message);
}
/**
* 通过在配置文件中指定init-method方法来初始化bean
*
* 2015年5月4日 下午8:13:57
* chenshunyang
*/
public void initMethod(){
System.out.println("InitSequenceService: init-method:"+message);
}
/**
* 通过在配置文件中指定destroy-method来实现销毁bean之前操作
* 2015年5月4日 下午8:14:25
* chenshunyang
*/
public void  destroyMethod(){
System.out.println("InitSequenceService: destroy-method:"+message);
}

public String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}


2.定义配置文件

<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<!--     spring 容器采用注解配置:扫描注解配置-->
<context:annotation-config />

<bean id="initSequenceService" class="com.fdd.InitSequenceService" init-method="initMethod" destroy-method="destroyMethod">
<property name="message" value="123"></property>
</bean>
</beans>


3.编写测试代码

package com.fdd;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

public static void main(String[] args) {
AbstractApplicationContext  context = new ClassPathXmlApplicationContext("classpath*:spring-service.xml");
InitSequenceService initSequenceService =(InitSequenceService)context.getBean("initSequenceService");
initSequenceService.sayHello();
context.registerShutdownHook();

}

}


4.观察运行结果



5.结论

通过上述输出结果,三者的先后顺序也就一目了然了:

初始化bean:Constructor > @PostConstruct > InitializingBean > init-method

bean在销毁的过程中:[b]@PreDestroy > DisposableBean > destroy-method[/b]

并且通过[b]@PostConstruct进行初始化bean、[b][b]@PreDestroy进行销毁bean之前的操作可以写明多个方法[/b][/b][/b]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐