Spring bean 实现初始化、销毁方法的方式及顺序
Spring 允许 Bean 在初始化完成后以及销毁前执行特定的操作,常用方法有三种:
- 使用注解,在指定方法上加上@PostConstruct或@PreDestroy注解来制定该方法是在初始化之后还是销毁之前调用;
- 使用xml配置,通过<bean> 元素的 init-method/destroy-method属性指定初始化之后 /销毁之前调用的操作方法;
- 实现InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法。
这三种实现方式,在执行顺序上还是有一定差异,简单测试代码:
java类:
package com.test.spring.initorder; import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy; import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean; public class InitOrderBean implements InitializingBean,DisposableBean { public InitOrderBean() {
super();
System.out.println("InitOrderBean执行构造方法......");
} @Override
public void destroy() throws Exception {
System.out.println("InitOrderBean执行 DisposableBean destory 方法........"); } @Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitOrderBean执行InitializingBean 初始化方法 ....."); } @PostConstruct
public void postConstruct() {
System.out.println("InitOrderBean执行postConstruct Anno: postConstruct");
} @PreDestroy
public void preDestroy() {
System.out.println("InitOrderBean执行preDestroy Anno: preDestroy");
} public void init (){
System.out.println("InitOrderBean执行xml init......");
} public void destory(){
System.out.println("InitOrderBean执行xml destory.........");
} }
xml 配置applicationContext.xml:
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> <context:component-scan base-package="com.test.spring.initorder" />
<context:annotation-config/> <bean class="com.test.spring.initorder.InitOrderBean" init-method="init" destroy-method="destory">
</bean> </beans>
java 测试类:
package com.test.spring.initorder;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");
context.close();
}
}
执行结果:
InitOrderBean执行构造方法......
InitOrderBean执行postConstruct Anno: postConstruct
InitOrderBean执行InitializingBean 初始化方法 .....
InitOrderBean执行xml init......
InitOrderBean执行preDestroy Anno: preDestroy
InitOrderBean执行 DisposableBean destory 方法........
InitOrderBean执行xml destory.........
由此可得出:
Bean在实例化的过程中:Constructor --> @PostConstruct -->InitializingBean --> init-method
Bean在销毁的过程中:@PreDestroy --> DisposableBean --> destroy-method
Spring bean 实现初始化、销毁方法的方式及顺序的更多相关文章
- Spring3实战第二章第一小节 Spring bean的初始化和销毁三种方式及优先级
Spring bean的初始化和销毁有三种方式 通过实现 InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法: 优先级第二通过 <bean& ...
- 【Spring Framework】Spring注解设置Bean的初始化、销毁方法的方式
bean的生命周期:创建---初始化---销毁. Spring中声明的Bean的初始化和销毁方法有3种方式: @Bean的注解的initMethod.DestroyMethod属性 bean实现Ini ...
- spring bean的初始化以及销毁
spring bean初始化或销毁时执行某些方法,有很多使用场景.比如初始化时,启动bean中的线程池.销毁时释放资源,个人比较喜欢实现InitializingBean和 DisposableBean ...
- Spring bean的初始化及销毁
Spring bean的几个属性:scope.init-method.destroy-method.depends-on等. Scope 在Spring容器中是指其创建的Bean对象相对于其他Bean ...
- 注解在Spring中的运用(对象获取、对象单例/多例、值的注入、初始化/销毁方法、获取容器)
1.注解的方式获取对象 (1)导包: (2)书写配置文件(要保证已经导入了约束): <?xml version="1.0" encoding="UTF-8" ...
- Spring Bean生命周期回调方法
参阅官方文档:https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory ...
- [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- spring bean的初始化
scope:作用域 singleton prototype request session 默认为singleton lazy-init:default=false ,false ,tru ...
- Spring课程 Spring入门篇 3-2 Spring bean装配(上)之bean的生命周期
课程链接: 本节主要讲了三大块内容 1 bean的生命周期概念 2 bean的初始化和销毁的三种方式对比(代码演练) 3 总结 1 bean的生命周期概念 1.1 bean的定义:xml中关于bean ...
随机推荐
- 使用delphi+intraweb进行微信开发5—准备实现微信API,先从获取AccessToken开始
在前4讲中我们已经使iw开发的应用成功和微信进行了对接,再接下来的章节中我们开始逐一尝试和实现微信的各个API,开始前先来点准备工作 首先需要明确的是,微信的API都是通过https调用实现的,分为p ...
- css狂记
接着上一篇 html狂记,同样是DIV+CSS传统站点开发,同样只是收集.整理... CSS 背景属性(Background) 属性 描述 CSS background 在一个声明中设置所有的背景属性 ...
- 诺基亚N900使用技巧
一直都对 Linux 的掌上终端挺感冒的,最近从闲鱼上入后一台欧版的,开机进行评测和使用.以下经验仅供新手参考. 选择 N900 是有原因的,首先 N900 相对来说比较小巧(作为 MID,对比起手机 ...
- linux下的gcc编译器
1. 编译单个文件成可执行文件 gcc hello.c 生成默认的可执行文件a.out 2. 编译单个文件为指定名字的可执行文件,使用-o选项指定文件名字 gcc -o test main.c 生成文 ...
- hashmap两种遍历方法
第一种:使用entryset来进行遍历 Map map=new HashMap(); Iterator iter=map.entrySet().iterator(); while(iter.hasNe ...
- Access denied for user 'root'@'localhost' (using password:YES)
版权所有,未经博主允许不得转载. 今天发现服务器mysql连接不上,报 can't connect to mysql server on 'root'@'ip' (61) 照例ssh到服务器,发现my ...
- js原型基础
js中的原型链是实现继承等面向对象的一种方式,而我这里不详细展开,仅仅是画了一张原型部分结构图,主要说明: 1._proto_和prototype之间的关系 2.原型链的起点是Object.proto ...
- cocoapods安装及常用命令
如果不是因为我重装了电脑,我大概也不会写这篇博客,网上关于cocoapods的安装可谓是不可胜数. 但是对于新版的系统来说,的确存在很多的坑点,这点不是我一个人遇到的问题 如果是新装的系统,默认是安装 ...
- 正确理解ThreadLocal
想必很多朋友对 ThreadLocal并不陌生,今天我们就来一起探讨下ThreadLocal的使用方法和实现原理.首先,本文先谈一下对ThreadLocal的理 解,然后根据ThreadLocal类的 ...
- JS 中的事件绑定、事件监听、事件委托
事件绑定 要想让 JavaScript 对用户的操作作出响应,首先要对 DOM 元素绑定事件处理函数.所谓事件处理函数,就是处理用户操作的函数,不同的操作对应不同的名称. 在JavaScript中,有 ...