三种不同实现初始化和销毁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 ...
随机推荐
- IOS开发计算文本尺寸
在IOS开发中例如微博,QQ聊天界面中要显示大量的文字信息,这样需要计算出文字部分的尺寸,才能设计出合适的控件尺寸和位置.下面是IOS 7.0计算文本尺寸的方法.- (CGRect)boundingR ...
- 分布式服务弹性框架“Hystrix”实践与源码研究(一)
文章初衷 为了应对将来在线(特别是无线端)业务量的成倍增长,后端服务的分布式化程度需要不断提高,对于服务的延迟和容错管理将面临更大挑战,公司框架和开源团队选择内部推广Netflix的Hystrix,一 ...
- DirectX11 学习笔记2 - 加入关键事件 实现视角转换 旋转
上的程序的的基础上.在基类D3DBase添加摄像头功能 //录影机 void D3DBase::setCamera() { //关键事件 //假定A,S,D,W,Q,E,Z,X,C键被按下.动摄像机 ...
- Windows服务之启动、停止、暂停、继续
原文:Windows服务之启动.停止.暂停.继续 Windows服务之启动.停止.暂停.继续 2011-11-09 15:07:37 我来说两句 收藏 我要投稿 [字体:小 大] ...
- VS2015 Apache Cordova
VS2015 Apache Cordova第一个Android和IOS应用 前言 本人个人博客原文链接地址为http://aehyok.com/Blog/Detail/75.html. http: ...
- JQUERY简写案例
源代码: <script ttype="text/javascript"> $(function(){ $(".btn").eq(0).click( ...
- Android 发展 ------------- Unable to resolve target 'android-19'
又一次装完Ecplise+ATD+Android SDK 在Ecplise工作空间导入之前写过的Android项目会出现错误,大部分是SDK 版本号不符,例如以下错误提示: Error:Unable ...
- Redmine(Ruby)配置经验
Redmine(Ruby)配置经验记录在配置Redmine邮件同步过程中遇到的各种问题与解决方法 1. 如何安装Redminehttp://www.redmine.org/projects/redmi ...
- 一步一步实现基于Task的Promise库(四)无参数的WorkItem
接着上一篇我直接给出代码,现在支持了new Task(), then(), all(), any() 这些不传参的调用方式. (function(){ var isFunction = functio ...
- 使用fixed制作浮动广告(注意:解决闪屏问题,但适用于高版本浏览器,低版本的浏览器不适用)
<script language="javascript" type="text/javascript"> //隐藏广告 function clos ...