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

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

spring 允许 Bean 在初

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

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

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

Java类:

[java] view plaincopy
  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配置文件:

[html] view plaincopy
  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方法,结果如下:

[html] view plaincopy
  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

转载于:https://www.cnblogs.com/kimobolo/p/7059888.html

spring实现listener(转)相关推荐

  1. SSM框架整合(Spring+SpringMVC+MyBatis)

    输出结果 1.Maven Web项目创建 之前有写过Eclipse+Maven创建web项目的帖子,如果需要,请参考这里写链接内容 创建好项目之后因为入下图: 2.SSM整合 2.1 引入需要的JAR ...

  2. struts2+hibernate+Spring分层开发

    web.xml中要加Spring的listener,struts2的filter的配置. UI  struts2:      jsp   struts.xml   Action Spring     ...

  3. SSM框架整合(IntelliJ IDEA + maven + Spring + SpringMVC + MyBatis)

    使用IDEA创建Spring + SpringMVC + MyBatis 框架的Maven的项目. 一. 创建maven项目 1. File -> New Module,进入创建项目窗口. 2. ...

  4. [转]SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)

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

  5. Spring MVC 框架搭建及详解

    现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...

  6. Spring+SpringMVC+MyBatis整合教程

    2019独角兽企业重金招聘Python工程师标准>>> 1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框 ...

  7. Spring Boot 2.6 正式发布:循环依赖默认禁止、增加SameSite属性...

    昨天,Spring官方正式发布了Spring Boot今年最后一个特性版本:2.6.0 同时,也宣布了2.4.x版本的终结. 那么这个新版本又带来了哪些新特性呢?下面就一起跟着DD来看看吧! 重要特性 ...

  8. Spring 面试题(2021最新版)赶紧收藏!

    Spring概述 什么是spring? Spring是一个轻量级Java开发框架,最早有Rod Johnson创建,目的是为了解决企业级应用开发的业务逻辑层和其他各层的耦合问题.它是一个分层的Java ...

  9. 重磅!Spring Boot 2.5.0火热发布,还学得动吗?

    今年520的事情是真的多,把Spring Boot 2.5.0的版本发布都给忽略了! 今天跟我一起看看Spring Boot 2.5.0又都带来了哪些振奋人心的新特性吧! 主要更新 支持 Java 1 ...

最新文章

  1. 如何添加引文标_如何在Google文档中查找和添加引文
  2. 云计算如何使企业的业务受益?
  3. 学python需要什么基础-零基础学Python应该学习哪些入门知识及学习步骤安排
  4. mockito 外部接口_原创 |使用JUnit、AssertJ和Mockito编写单元测试和实践TDD (五)第一个单元测试...
  5. vmware vcenter 4.1升级到5.0
  6. 如何用数据驱动的广告效果
  7. 异常通知辅助模块 5.3 (try/catch)
  8. 【计算机科学】【2020.05】基于深度学习的计算蛋白质结构预测
  9. 第五人格深渊金币每周更新时间
  10. 普通用户登录出现“sorry, that didn‘t work please try again”
  11. JAVA 协程Quasar初探
  12. run vue task的项目报错:Error while running task C:\IT\xxxxxx:serve with message‘spawn vue-cli-service
  13. 【MarkDown使用技巧】轻松搞定MarkDown
  14. 如何用微信小程序实现优惠券功能
  15. Kafka知识总结之消费者简单使用
  16. 富士伺服smart-plus接线及其用法
  17. Unix系统 - 进程管理
  18. 日语的计算机词汇,常用日语计算机词汇~~
  19. Dueling bandits——《Relative Upper Confidence Bound for the K-Armed Dueling Bandit Problem》算法梳理(RUCB)
  20. 小技巧!无需插件,一键批量下载微信公众号的图片!

热门文章

  1. 【计算机网络】 DNS学习笔记 (>﹏<)
  2. SQL Server 数据库的维护(一)__存储过程(procedure)
  3. 表单FORM的5个属性name,method,action,enctype,target
  4. oracle 安装程序异常终止,Oracle安装错误“程序异常终止
  5. 计算机复试题库.doc,计算机基础知识面试题库.doc
  6. 为什么很多网逃抓不到_为什么很多人找不到长久合作的毛刷厂家?
  7. css中的代码图标,认识CSS中字体图标(示例代码)
  8. python新特性赋值_变量与赋值_Python入门视频课程_Python视频-51CTO学院
  9. linux ext4 img解包打包教程,解打包.img.ext4(转)
  10. C++知识点49——类继承与类的构造、拷贝、operator=和析构函数