三种不同实现初始化和销毁bean之前进行的操作的比较
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在销毁的过程中:@PreDestroy > DisposableBean > destroy-method
并且通过@PostConstruct进行初始化bean、@PreDestroy进行销毁bean之前的操作可以写明多个方法
三种不同实现初始化和销毁bean之前进行的操作的比较的更多相关文章
- Spring实现初始化和销毁bean之前进行的操作,三种方式
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...
- Spring源码学习之: 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...
- 在spring容器中定义初始化和销毁bean前所做的操作,有三种方式
1.使用注解,通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 package com.luoq.test.annotation.init; ...
- spring实战三装配bean之Bean的作用域以及初始化和销毁Bean
1.Bean的作用域 所有的spring bean默认都是单例.当容器分配一个Bean时,不论是通过装配还是调用容器的getBean()方法,它总是返回Bean的同一个实例.有时候需要每次请求时都获得 ...
- Spring学习笔记--初始化和销毁Bean
可以使用bean的init-method和destroy-method属性来初始化和销毁bean.定义一个Hero类: package com.moonlit.myspring; public cla ...
- spring容器初始化bean和销毁bean之前进行一些操作的定义方法
关于在spring 容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种,通过在xml中定义init-method和destory-method方法 第二种, ...
- UIViewController三种不同的初始化view的方式
You can specify the views for a view controller using a Storyboard created in Interface Builder. A s ...
随机推荐
- PHP通过传递对象参数调用asp.net Webservice 服务
asp.net 测试服务 ProcessRequest.asmx文件代码 public class ProcessRequest : System.Web.Services.WebService ...
- Linux页快速缓存与回写机制分析
參考 <Linux内核设计与实现> ******************************************* 页快速缓存是linux内核实现的一种主要磁盘缓存,它主要用来降低 ...
- Wowza流媒体Live直播和VOD点播配置实战-attach
Wowza是当今可以说最流行的流媒体服务器之一,近来因为需要搭建相应的服务器,但又不想用camera等作真实的直播,所以想办法用媒体文件转换成直播流再提供给Wowza进行直播.这里把该设置步骤以及设计 ...
- td中的值自动换行
设置td中的值自动换行在<td ></td> 中加上这样一句代码,可以简省设置,使长字符串换行.而不用设置width,height. style="word-wrap ...
- 《C语言及程序设计初步》网络课程主页
题记 CSDN要开在线教育频道,向我发出邀请,看能否开些课程. 我近日一直在关注着翻转课堂,试图在传统课堂中引入新的元素,这须要资源建设的积累.没有时间表的工作,非常难把握. 为CSDN做在线课程,为 ...
- Gimp教程:简约手机图标风格
效果: 在一个国外博客上翻到的图标制作教程,效果类似于Cowon J3的默认图标风格. 制作过程很简单,只需两三步,不多说了,上步骤 Step1.新建50×50的黑色背景 Step2.新建 ...
- 数据库备份还原工具EMS SQL Angel for SQL Server发布1.3版本
EMS公司,是专门从事企业数据库以及内置于多层次客户服务器结构自动化开发.其EMS SQL Angel for SQL Server工具,便是SQL Servers数据库数据备份还原工具,并且还能使用 ...
- IE支持CSS3圆角
在CSS中使用CSS插件文件即可让IE6/IE7/IE8浏览器. 具体CSS代码: .yuan { border: 2px solid #C0C0C0; -moz-border-radius: 10p ...
- android音乐播放器开发 SweetMusicPlayer
智能负载直插式歌词
在一份书面的使用MediaPlayer播放音乐, http://blog.csdn.net/huweigoodboy/article/details/39862773.假设没有本地歌词怎么办?如今来将 ...
- css3中webkit-box的用法(平分父元素)
display:box;box-flex是css3新添加的盒子模型属性,它的出现可以解决我们通过N多结构.css实现的布局方式.经典的一个布局应用就是布局的垂直等高.水平均分.按比例划分.目前box- ...