博主说未经同意,不能转载,我这种小码农,他应该不会在乎

原创地址:http://blog.csdn.net/caihaijiang/article/details/8629725

spring 允许 Bean 在初

始化完成后以及销毁前执行特定的操作。下面是常用的三种指定特定操作的方法:

  • 通过实现InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法;
  • 通过<bean> 元素的 init-method/destroy-method属性指定初始化之后 /销毁之前调用的操作方法;
  • 在指定方法上加上@PostConstruct或@PreDestroy注解来制定该方法是在初始化之后还是销毁之前调用。

这几种配置方式,执行顺序是怎样的呢?我们通过例子来研究下:

Java类:

  1. import javax.annotation.PostConstruct;
  2. import javax.annotation.PreDestroy;
  3. import org.springframework.beans.factory.DisposableBean;
  4. import org.springframework.beans.factory.InitializingBean;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. public class InitAndDestroySeqBean implements InitializingBean,DisposableBean {
  7. public InitAndDestroySeqBean(){
  8. System.out.println("执行InitAndDestroySeqBean: 构造方法");
  9. }
  10. @PostConstruct
  11. public void postConstruct() {
  12. System.out.println("执行InitAndDestroySeqBean: postConstruct");
  13. }
  14. @Override
  15. public void afterPropertiesSet() throws Exception {
  16. System.out.println("执行InitAndDestroySeqBean: afterPropertiesSet");
  17. }
  18. public void initMethod() {
  19. System.out.println("执行InitAndDestroySeqBean: init-method");
  20. }
  21. @PreDestroy
  22. public void preDestroy()  {
  23. System.out.println("执行InitAndDestroySeqBean: preDestroy");
  24. }
  25. @Override
  26. public void destroy() throws Exception {
  27. System.out.println("执行InitAndDestroySeqBean: destroy");
  28. }
  29. public void destroyMethod() {
  30. System.out.println("执行InitAndDestroySeqBean: destroy-method");
  31. }
  32. public static void main(String[] args) {
  33. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("com/chj/spring/bean.xml");
  34. context.close();
  35. }
  36. }

Spring配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  9. <context:annotation-config/>
  10. <bean id="initAndDestroySeqBean" class="com.chj.spring.InitAndDestroySeqBean" init-method="initMethod" destroy-method="destroyMethod"/>
  11. </beans>

运行InitAndDestroySeqBean的main方法,结果如下:

  1. 2013-03-03 17:11:19,098 DEBUG support.DefaultListableBeanFactory - Creating instance of bean 'initAndDestroySeqBean'
  2. 执行InitAndDestroySeqBean: 构造方法
  3. 2013-03-03 17:11:19,114 DEBUG annotation.CommonAnnotationBeanPostProcessor - Found init method on class [com.alibaba.chj.spring.InitAndDestroySeqBean]: public void com.alibaba.chj.spring.InitAndDestroySeqBean.postConstruct()
  4. 2013-03-03 17:11:19,114 DEBUG annotation.CommonAnnotationBeanPostProcessor - Found destroy method on class [com.alibaba.chj.spring.InitAndDestroySeqBean]: public void com.alibaba.chj.spring.InitAndDestroySeqBean.preDestroy()
  5. 2013-03-03 17:11:19,129 DEBUG support.DefaultListableBeanFactory - Eagerly caching bean 'initAndDestroySeqBean' to allow for resolving potential circular references
  6. 2013-03-03 17:11:19,129 DEBUG annotation.CommonAnnotationBeanPostProcessor - Invoking init method on bean 'initAndDestroySeqBean': public void com.alibaba.chj.spring.InitAndDestroySeqBean.postConstruct()
  7. 执行InitAndDestroySeqBean: postConstruct
  8. 2013-03-03 17:11:19,129 DEBUG support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'initAndDestroySeqBean'
  9. 执行InitAndDestroySeqBean: afterPropertiesSet
  10. 2013-03-03 17:11:19,129 DEBUG support.DefaultListableBeanFactory - Invoking init method  'initMethod' on bean with name 'initAndDestroySeqBean'
  11. 执行InitAndDestroySeqBean: init-method
  12. 2013-03-03 17:11:19,129 DEBUG support.DefaultListableBeanFactory - Finished creating instance of bean 'initAndDestroySeqBean'
  13. 2013-03-03 17:11:19,129 INFO  support.ClassPathXmlApplicationContext - Closing org.springframework.context.support.ClassPathXmlApplicationContext@56a499: display name [org.springframework.context.support.ClassPathXmlApplicationContext@56a499]; startup date [Sun Mar 03 17:11:17 CST 2013]; root of context hierarchy
  14. 2013-03-03 17:11:19,129 INFO  support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1292d26: defining beans [org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,initAndDestroySeqBean]; root of factory hierarchy
  15. 2013-03-03 17:11:19,129 DEBUG annotation.CommonAnnotationBeanPostProcessor - Invoking destroy method on bean 'initAndDestroySeqBean': public void com.alibaba.chj.spring.InitAndDestroySeqBean.preDestroy()
  16. 执行InitAndDestroySeqBean: preDestroy
  17. 2013-03-03 17:11:19,145 DEBUG support.DisposableBeanAdapter - Invoking destroy() on bean with name 'initAndDestroySeqBean'
  18. 执行InitAndDestroySeqBean: destroy
  19. 2013-03-03 17:11:19,145 DEBUG support.DisposableBeanAdapter - Invoking destroy method 'destroyMethod' on bean with name 'initAndDestroySeqBean'
  20. 执行InitAndDestroySeqBean: destroy-method

从执行结果可以看出:

Bean在实例化的过程中:Constructor > @PostConstruct >InitializingBean > init-method

Bean在销毁的过程中:@PreDestroy > DisposableBean > destroy-method

spring实现listener(转)的更多相关文章

  1. Spring以及tomcat中的Listener

    tomcat容器的listener: ServletContextListener HttpSessionListener ServletRequestListener Spring的listener ...

  2. Spring Boot的Listener机制的用法和实现原理详解

    之前在介绍了在spring-boot启动过程中调用runner的原理,今天我们介绍另外一种可以实现相似功能的机制:spring-boot的Listener机制. 通过注册Listener,可以实现对于 ...

  3. Java Listener中Spring接口注入的使用

    在项目中使用Spring通常使用他的依赖注入可以很好的处理,接口与实现类之间的耦合性,但是通常的应用场景中都是Service层和DAO层,或者web层的话, 也是与Strust2来整合,那么如何在Li ...

  4. axis2+spring集成

    转载自:http://www.cnblogs.com/linjiqin/archive/2011/07/05/2098316.html 1.新建一个web project项目,最终工程目录如下: 注意 ...

  5. SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)【转】

    使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...

  6. [Java] Spring + SpringMVC + Maven + JUnit 搭建

    示例项目下载: https://github.com/yangyxd/SpringDemo 利用前面 SpringMVC 项目的配置方式,完成初步的项目创建.下面只讲一些不同之处. 传送门: [Jav ...

  7. struts2+spring的两种整合方式

    也许有些人会因为学习了struts1,会以为struts2.struts1与spring的整合也是一样的,其实这两者相差甚远.下面就来讲解一下struts2与spring的整合两种方案.(部分转载,里 ...

  8. Maven+Spring MVC Spring Mybatis配置

    环境: Eclipse Neon JDK1.8.0 Tomcat8.0 先决条件: Eclipse先用maven向导创建web工程.参见本站之前随笔. 本机安装完成mysql5:新建用户xuxy03设 ...

  9. 配置struts2+spring,springmvc

    Struts2+Spring整合 一.spring负责注入,struts2负责它自己的工作.这样不是很符合spring作为ioc容器的全部功能,不推荐. 二.spring负责全部bean和struts ...

随机推荐

  1. 熟悉Junit单元测试方法

    定义: JUnit是一个Java语言的单元测试框架.它由Kent Beck和Erich Gamma建立,逐渐成为源于Kent Beck的sUnit的xUnit家族中最为成功的一个. JUnit有它自己 ...

  2. Linux一行命令处理批量文件

    前言 最好的方法不一定是你最快能想到的.这里提供一种使用sed命令构造命令解决处理批量文件的技巧,供参考. 需求案例1 将当前目录下所有的0_80_91.txt.0_80_92.txt.0_80_93 ...

  3. PeopleSoft进程卡在“已排队”状态诊断和解决

    进程卡在“已排队”状态的原因很多.最常见的原始是进程调度器服务挂掉了(可以在“进程监视器”页面的“服务器”tab页中查看). 除此之外,还可以进行下面诊断:1. 检查下面3张表:PSPRCSRQSTP ...

  4. mysql Table 'user' is marked as crashed and should be repaired

    myisamchk -f x:\xxxxxxxxx\MySQL\data\mysql\*.MYI

  5. MYSQL批量删除大量数据

    DELETE FROM '表' WHERE '字段'=1 会发现删除失败,因为lock wait timeout exceed的错误: 通过LIMIT参数分批删除,因为如果不用limit,删除大量数据 ...

  6. 定时器&改变定时器的执行频率

    static System.Threading.Timer timer; static void Main(string[] args) { Console.WriteLine("Press ...

  7. contains用法

    判断List<UserInfoDto>里是否有重复的UserInfoDto,不能使用contains 例: List<UserInfoDto> list = new Array ...

  8. SpringCloud Zuul网关的简单理解

    Zuul网关功能 请求路由.服务路由.请求过滤 请求路由 参数配置如下所示,所有能够配置path规则的请求,都会被zuul网关转发到对应的url上. zuul.routes.user-service. ...

  9. pyautogui 文档(四):消息框功能

    消息框功能 PyAutoGUI利用PyMsgBox中的消息框函数提供跨平台的纯Python方式来显示JavaScript样式的消息框.提供了四个消息框功能: alert()函数 >>> ...

  10. 【c # 数据库】存储过程

    可理解存储过程是方法,快速调用,方便使用. 数据库建立新的存储过程: CREATE PROCEDURE myProc -- Add the parameters for the stored proc ...