spring实现listener(转)
博主说未经同意,不能转载,我这种小码农,他应该不会在乎
原创地址:http://blog.csdn.net/caihaijiang/article/details/8629725
spring 允许 Bean 在初
始化完成后以及销毁前执行特定的操作。下面是常用的三种指定特定操作的方法:
- 通过实现InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法;
- 通过<bean> 元素的 init-method/destroy-method属性指定初始化之后 /销毁之前调用的操作方法;
- 在指定方法上加上@PostConstruct或@PreDestroy注解来制定该方法是在初始化之后还是销毁之前调用。
这几种配置方式,执行顺序是怎样的呢?我们通过例子来研究下:
Java类:
- import javax.annotation.PostConstruct;
- import javax.annotation.PreDestroy;
- import org.springframework.beans.factory.DisposableBean;
- import org.springframework.beans.factory.InitializingBean;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class InitAndDestroySeqBean implements InitializingBean,DisposableBean {
- public InitAndDestroySeqBean(){
- System.out.println("执行InitAndDestroySeqBean: 构造方法");
- }
- @PostConstruct
- public void postConstruct() {
- System.out.println("执行InitAndDestroySeqBean: postConstruct");
- }
- @Override
- public void afterPropertiesSet() throws Exception {
- System.out.println("执行InitAndDestroySeqBean: afterPropertiesSet");
- }
- public void initMethod() {
- System.out.println("执行InitAndDestroySeqBean: init-method");
- }
- @PreDestroy
- public void preDestroy() {
- System.out.println("执行InitAndDestroySeqBean: preDestroy");
- }
- @Override
- public void destroy() throws Exception {
- System.out.println("执行InitAndDestroySeqBean: destroy");
- }
- public void destroyMethod() {
- System.out.println("执行InitAndDestroySeqBean: destroy-method");
- }
- public static void main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("com/chj/spring/bean.xml");
- context.close();
- }
- }
Spring配置文件:
- <?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:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <context:annotation-config/>
- <bean id="initAndDestroySeqBean" class="com.chj.spring.InitAndDestroySeqBean" init-method="initMethod" destroy-method="destroyMethod"/>
- </beans>
运行InitAndDestroySeqBean的main方法,结果如下:
- 2013-03-03 17:11:19,098 DEBUG support.DefaultListableBeanFactory - Creating instance of bean 'initAndDestroySeqBean'
- 执行InitAndDestroySeqBean: 构造方法
- 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()
- 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()
- 2013-03-03 17:11:19,129 DEBUG support.DefaultListableBeanFactory - Eagerly caching bean 'initAndDestroySeqBean' to allow for resolving potential circular references
- 2013-03-03 17:11:19,129 DEBUG annotation.CommonAnnotationBeanPostProcessor - Invoking init method on bean 'initAndDestroySeqBean': public void com.alibaba.chj.spring.InitAndDestroySeqBean.postConstruct()
- 执行InitAndDestroySeqBean: postConstruct
- 2013-03-03 17:11:19,129 DEBUG support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'initAndDestroySeqBean'
- 执行InitAndDestroySeqBean: afterPropertiesSet
- 2013-03-03 17:11:19,129 DEBUG support.DefaultListableBeanFactory - Invoking init method 'initMethod' on bean with name 'initAndDestroySeqBean'
- 执行InitAndDestroySeqBean: init-method
- 2013-03-03 17:11:19,129 DEBUG support.DefaultListableBeanFactory - Finished creating instance of bean 'initAndDestroySeqBean'
- 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
- 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
- 2013-03-03 17:11:19,129 DEBUG annotation.CommonAnnotationBeanPostProcessor - Invoking destroy method on bean 'initAndDestroySeqBean': public void com.alibaba.chj.spring.InitAndDestroySeqBean.preDestroy()
- 执行InitAndDestroySeqBean: preDestroy
- 2013-03-03 17:11:19,145 DEBUG support.DisposableBeanAdapter - Invoking destroy() on bean with name 'initAndDestroySeqBean'
- 执行InitAndDestroySeqBean: destroy
- 2013-03-03 17:11:19,145 DEBUG support.DisposableBeanAdapter - Invoking destroy method 'destroyMethod' on bean with name 'initAndDestroySeqBean'
- 执行InitAndDestroySeqBean: destroy-method
从执行结果可以看出:
Bean在实例化的过程中:Constructor > @PostConstruct >InitializingBean > init-method
Bean在销毁的过程中:@PreDestroy > DisposableBean > destroy-method
转载于:https://www.cnblogs.com/kimobolo/p/7059888.html
spring实现listener(转)相关推荐
- SSM框架整合(Spring+SpringMVC+MyBatis)
输出结果 1.Maven Web项目创建 之前有写过Eclipse+Maven创建web项目的帖子,如果需要,请参考这里写链接内容 创建好项目之后因为入下图: 2.SSM整合 2.1 引入需要的JAR ...
- struts2+hibernate+Spring分层开发
web.xml中要加Spring的listener,struts2的filter的配置. UI struts2: jsp struts.xml Action Spring ...
- SSM框架整合(IntelliJ IDEA + maven + Spring + SpringMVC + MyBatis)
使用IDEA创建Spring + SpringMVC + MyBatis 框架的Maven的项目. 一. 创建maven项目 1. File -> New Module,进入创建项目窗口. 2. ...
- [转]SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
使用SSM(spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...
- Spring MVC 框架搭建及详解
现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...
- Spring+SpringMVC+MyBatis整合教程
2019独角兽企业重金招聘Python工程师标准>>> 1.基本概念 1.1.Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框 ...
- Spring Boot 2.6 正式发布:循环依赖默认禁止、增加SameSite属性...
昨天,Spring官方正式发布了Spring Boot今年最后一个特性版本:2.6.0 同时,也宣布了2.4.x版本的终结. 那么这个新版本又带来了哪些新特性呢?下面就一起跟着DD来看看吧! 重要特性 ...
- Spring 面试题(2021最新版)赶紧收藏!
Spring概述 什么是spring? Spring是一个轻量级Java开发框架,最早有Rod Johnson创建,目的是为了解决企业级应用开发的业务逻辑层和其他各层的耦合问题.它是一个分层的Java ...
- 重磅!Spring Boot 2.5.0火热发布,还学得动吗?
今年520的事情是真的多,把Spring Boot 2.5.0的版本发布都给忽略了! 今天跟我一起看看Spring Boot 2.5.0又都带来了哪些振奋人心的新特性吧! 主要更新 支持 Java 1 ...
最新文章
- 收藏 | CNN的一些可视化方法!
- 006_url-pattern配置
- nginx安装及编译参数详解
- AIDE支持实时错误检查、代码重构、代码智能导航、生成APK
- spring 中 Hibernate 事务和JDBC事务嵌套问题
- http服务ajax编程
- linux drm 内存管理,Linux RK3399 DRM 框架及代码流程 · Younix’s Studio
- android 4.2 安全新特性(I)
- JS事件 鼠标移开事件(onmouseout)鼠标移开事件,当鼠标移开当前对象时,执行onmouseout调用的程序。...
- qq安全使用指南.txt
- 文件服务器定时开关机,云服务器定时开关机
- 黑客都怎么制作外挂?C++黑客编程收集的源码集合
- 双足机器人Maltab腿部建模,正运动学分析
- Unity移动的三种方式
- Linux服务篇之SSH服务
- PS抠图工具Topaz ReMask中文汉化(win/mac版,含教程)
- 过去式和过去分词的不规则列表
- 华为、海尔之后,阿里在全屋智能领域有新动作,这次牵手的是萤石
- 【flutter】tabbar文字抖动处理方案
- 面向过程(PO)和面向对象(OO)的区别(思维导图)
热门文章
- 深入理解空间搜索算法 ——数百万数据中的瞬时搜索
- getHibernateTemplate()的用法 (转)
- 解决更新到os x10.11后openssl头文件无法找到的问题
- Git入门第一集!(Git的下载地址、下载资源)
- testng 定时构建_Jenkins使用安装:持续集成自动化方案 | Web自动化测试方案
- bigint对应java什么类型_「JAVA」从格式化输出到扫描输入,深究Java正则表达式匹配之道
- python面向对象编程138讲_Python---面向对象编程-1
- linux查看服务依赖关系,服务管理(1)
- arduino项目_quot;Arduino智能车项目quot;实战班开班!
- python背景怎么自定义铃声_python爬取手机铃声