三种不同实现初始化和销毁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 ...
随机推荐
- spark in eclipse---Spark学习笔记3
想要调试源码,还是要放到eclipse里面去.先生成eclipse项目,下载依赖包 victor@victor-ubuntu:~/software/incubator-spark-0.8.1-incu ...
- iOS开发---转换坐标系
- (void)viewDidLoad { [super viewDidLoad]; // 蓝色 UIView *blue = [[UIView alloc] init]; blue.backgrou ...
- SQL Server调优系列基础篇(常用运算符总结)
原文:SQL Server调优系列基础篇(常用运算符总结) 前言 上一篇我们介绍了如何查看查询计划,本篇将介绍在我们查看的查询计划时的分析技巧,以及几种我们常用的运算符优化技巧,同样侧重基础知识的掌握 ...
- 2014Esri国际用户大会ArcGIS Online
1.基于什么是新的ArcGISOnline? ArcGISOnline不断更新.大约每四个月就会把新的增强的功能公布到各部分中.有新的空间分析的应用程序,如 Explorer forArcGIS,ap ...
- Asp.net 4.0,首次请求目录下的文件时响应很慢
原文:Asp.net 4.0,首次请求目录下的文件时响应很慢 1. 问题起因2. 尝试过的处理思路3. 解决方法 1. 问题起因 一个从VS2003(.Net Framework 1.1)升级到.ne ...
- 一个简单的dom查询函数
var regid = /^#([\w-]*)$/, regClass = /^\.([\w-]*)$/, regName = /^(div|a|p|ul|li|input|select|docume ...
- 从一道数学题弹程序员的思维:数学题,求证:(a+b%c)%c=(a+b)%c
在学校论坛看到这道题目,全忘了的感觉. 如果你是高中的,那我觉得你完全没问题.但是,在这个博客园的圈子,觉得全部人都是程(ban)序(zhuan)员(gong)相关的人员,解决这个问题有点难度,毕竟, ...
- APACHE启动失败是SYSTEM对apache目录没权限导致
表现如下: Apache: 1.The Apache service named reported the following error:>>> (OS 5)拒绝访问. : htt ...
- .NET Reflector 7.6.1.824安装及破解(刚试了,绝对能用)
首先下载在这里http://download.csdn.net/detail/gattaca2011/4578752,不要到官网去了,由于官网已经是8.0了. 然后就是安装,执行注冊机(注意断网),详 ...
- 我的Android 4 学习系列
Android 简介 开始入手 创建应用程序和Activity 创建用户界面 Intent 和 Broadcast Revicever 使用 Internet 资源 文件,保存状态和首选项 数据库和C ...