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
spring实现listener(转)的更多相关文章
- Spring以及tomcat中的Listener
tomcat容器的listener: ServletContextListener HttpSessionListener ServletRequestListener Spring的listener ...
- Spring Boot的Listener机制的用法和实现原理详解
之前在介绍了在spring-boot启动过程中调用runner的原理,今天我们介绍另外一种可以实现相似功能的机制:spring-boot的Listener机制. 通过注册Listener,可以实现对于 ...
- Java Listener中Spring接口注入的使用
在项目中使用Spring通常使用他的依赖注入可以很好的处理,接口与实现类之间的耦合性,但是通常的应用场景中都是Service层和DAO层,或者web层的话, 也是与Strust2来整合,那么如何在Li ...
- axis2+spring集成
转载自:http://www.cnblogs.com/linjiqin/archive/2011/07/05/2098316.html 1.新建一个web project项目,最终工程目录如下: 注意 ...
- SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)【转】
使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...
- [Java] Spring + SpringMVC + Maven + JUnit 搭建
示例项目下载: https://github.com/yangyxd/SpringDemo 利用前面 SpringMVC 项目的配置方式,完成初步的项目创建.下面只讲一些不同之处. 传送门: [Jav ...
- struts2+spring的两种整合方式
也许有些人会因为学习了struts1,会以为struts2.struts1与spring的整合也是一样的,其实这两者相差甚远.下面就来讲解一下struts2与spring的整合两种方案.(部分转载,里 ...
- Maven+Spring MVC Spring Mybatis配置
环境: Eclipse Neon JDK1.8.0 Tomcat8.0 先决条件: Eclipse先用maven向导创建web工程.参见本站之前随笔. 本机安装完成mysql5:新建用户xuxy03设 ...
- 配置struts2+spring,springmvc
Struts2+Spring整合 一.spring负责注入,struts2负责它自己的工作.这样不是很符合spring作为ioc容器的全部功能,不推荐. 二.spring负责全部bean和struts ...
随机推荐
- redis的使用与 django的redis的使用
1. 使用redis数据库分为两种: 第一种是在python语言中直接使用的方式, 第二种就是在django中使用django_redis模块来数用 第一种直接在python语言中使用redis im ...
- element-ui:el-table时间格式化
如果想对表格某一列的内容格式化,可用 formatter 属性.属性绑定格式化的方法即可 <el-table-column prop="update_time" label= ...
- C#操作Access数据库中遇到的问题(待续)
(1)在向Access中插入数据时,显示语法错误,后来将生成的sql语句单独拿到Access数据库中运行,能正确插入数据,从网上寻找资料,有人的sql语句正常,但是该语句在Access中运行错误,错误 ...
- activiti官网实例项目activiti-explorer之扩展多选框回显功能
相关参考链接:https://blog.csdn.net/murongxuesheng/article/details/76147380 回显:确认选中属性ng-model,循环属性ng-repeat ...
- Golang源码探索(一) 编译和调试源码(转)
GO可以说是近几年最热门的新兴语言之一了, 一般人看到分布式和大数据就会想到GO,这个系列的文章会通过研究golang的源代码来分析内部的实现原理,和CoreCLR不同的是, golang的源代码已经 ...
- Linux安装Intel Threading Building Blocks(TBB)
编译安装: wget https://codeload.github.com/01org/tbb/tar.gz/2019_U3 tar zxvf 2019_U3 cd tbb-2019_U3 make ...
- Python 学习笔记02篇
之前很庆幸早点报名课程 可以早点看到视频讲解,不至于后期太赶 不得不说原本21天压缩到14天再压缩到7天,如果可以完整且独立地完成至少三个作业,那水平应该真的很不错吧
- 服务主机superFetch占用磁盘过多
有用户升级到Win10系统后,发现系统运行不快,查看任务管理器中正在运行的进程,然后查看到有一个名称为“服务主机:本地系统(网络受限)”的进程的磁盘占用率很高.如果想要解决这个问题,那么不妨参考下面介 ...
- echars踩坑之图表缓存
针对echars 在一个图表内渲染多次数据时,图表会缓存上一次的数据导致下一次的数据图表变形.使用clear()清除图表缓存. 不让页面缓存的方法 按F12打开Network在Disable cach ...
- 基于Eureka的服务治理
代码地址如下:http://www.demodashi.com/demo/11927.html 一.服务的注册与发现 关系调用说明: 服务生产者启动时,向服务注册中心注册自己提供的服务 服务消费者启动 ...