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之前进行的操作的比较的更多相关文章

  1. Spring实现初始化和销毁bean之前进行的操作,三种方式

    关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...

  2. Spring源码学习之: 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

    关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...

  3. 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

    关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...

  4. 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进

    关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...

  5. 在spring容器中定义初始化和销毁bean前所做的操作,有三种方式

    1.使用注解,通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 package com.luoq.test.annotation.init; ...

  6. spring实战三装配bean之Bean的作用域以及初始化和销毁Bean

    1.Bean的作用域 所有的spring bean默认都是单例.当容器分配一个Bean时,不论是通过装配还是调用容器的getBean()方法,它总是返回Bean的同一个实例.有时候需要每次请求时都获得 ...

  7. Spring学习笔记--初始化和销毁Bean

    可以使用bean的init-method和destroy-method属性来初始化和销毁bean.定义一个Hero类: package com.moonlit.myspring; public cla ...

  8. spring容器初始化bean和销毁bean之前进行一些操作的定义方法

    关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种:        第一种,通过在xml中定义init-method和destory-method方法        第二种, ...

  9. UIViewController三种不同的初始化view的方式

    You can specify the views for a view controller using a Storyboard created in Interface Builder. A s ...

随机推荐

  1. c# md5

              还可以加盐,更难以破解 public static string GetMD5(string sDataIn)           {               MD5Crypt ...

  2. 微信公众平台消息接口开发-封装weixin.class.php

    原文:微信公众平台消息接口开发-封装weixin.class.php 一.封装weixin.class.php 由于微信公众平台的通信使用的是特定格式的XML数据,每次接受和回复都要去做一大堆的数据处 ...

  3. iOS 学习

    iOS 学习资料 (适合初学者) 本文资料来源于GitHub 一.视频教程(英文) Developing iOS 7 Apps for iPhone and iPad斯坦福开放教程之一, 课程主要讲解 ...

  4. 【 c语言中无符号和有符号的加法运算】【深入理解】--【sky原创】

    原文:[ c语言中无符号和有符号的加法运算][深入理解]--[sky原创]   第一题 #include<stdio.h> int main() { unsigned int a=6; i ...

  5. 在 VS2013的ASPNET站点开发中用 xheditor v1.1.13 作为HTML编辑器

    要用vs2013开发一个博客站点,.net   framework  4,须要一个HTML编辑器作为写文章的工具.经多方试用,排除了dotnettextbox.kceditor.认为xheditor ...

  6. jQuery.form Ajax无刷新上传错误 (jQuery.handleError is not a function) 解决方案

    今天,随着ajaxfileupload时间firebug财报显示,"jQuery.handleError is not a function"错误.因为一旦使用jQuery.for ...

  7. MySQL之查询优化方式(笔记)

    1.COUNT() 对COUNT的优化可以通过下面的SQL实现 mysql> select count(gnp<10000 or null) as '<<<<',c ...

  8. leetcode第21题--Generate Parentheses

    problem: Given n pairs of parentheses, write a function to generate all combinations of well-formed ...

  9. 如何将C#对象转化为JSON字符串

    System.Web.Extensions.dll中类JavaScriptSerializer可以帮助我们把C#对象转化为JSON字符串. 有一个Person类 public class Person ...

  10. bat批量目光声明

    写bat同一批次,盯着函数应使用.这个程序对可读性 在批处理,凝视节还有一种更常用的方法: goto start      = 能够是多行文本,能够是命令      = 能够包括重定向符号和其它特殊字 ...