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 实现初始化、销毁方法的方式及顺序的更多相关文章

  1. Spring3实战第二章第一小节 Spring bean的初始化和销毁三种方式及优先级

    Spring bean的初始化和销毁有三种方式 通过实现 InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法: 优先级第二通过 <bean& ...

  2. 【Spring Framework】Spring注解设置Bean的初始化、销毁方法的方式

    bean的生命周期:创建---初始化---销毁. Spring中声明的Bean的初始化和销毁方法有3种方式: @Bean的注解的initMethod.DestroyMethod属性 bean实现Ini ...

  3. spring bean的初始化以及销毁

    spring bean初始化或销毁时执行某些方法,有很多使用场景.比如初始化时,启动bean中的线程池.销毁时释放资源,个人比较喜欢实现InitializingBean和 DisposableBean ...

  4. Spring bean的初始化及销毁

    Spring bean的几个属性:scope.init-method.destroy-method.depends-on等. Scope 在Spring容器中是指其创建的Bean对象相对于其他Bean ...

  5. 注解在Spring中的运用(对象获取、对象单例/多例、值的注入、初始化/销毁方法、获取容器)

    1.注解的方式获取对象 (1)导包: (2)书写配置文件(要保证已经导入了约束): <?xml version="1.0" encoding="UTF-8" ...

  6. Spring Bean生命周期回调方法

    参阅官方文档:https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory ...

  7. [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  8. spring bean的初始化

    scope:作用域   singleton  prototype  request session   默认为singleton lazy-init:default=false ,false ,tru ...

  9. Spring课程 Spring入门篇 3-2 Spring bean装配(上)之bean的生命周期

    课程链接: 本节主要讲了三大块内容 1 bean的生命周期概念 2 bean的初始化和销毁的三种方式对比(代码演练) 3 总结 1 bean的生命周期概念 1.1 bean的定义:xml中关于bean ...

随机推荐

  1. js 处理 html 标签转义 处理json中含有的ascii 编码

    function escape2Html(str) { var arrEntities = { 'lt': '<', 'gt': '>', 'nbsp': ' ', 'amp': '&am ...

  2. Lucene学习之一:使用lucene为数据库表创建索引,并按关键字查询

    最近项目中要用到模糊查询,开始研究lucene,期间走了好多弯路,总算实现了一个简单的demo. 使用的lucene jar包是3.6版本. 一:建立数据库表,并加上测试数据.数据库表:UserInf ...

  3. mvc ajax csrf

    http://www.cnblogs.com/zhyp/p/5556980.html http://www.asp.net/web-api/overview/security/preventing-c ...

  4. 慕课网-Java入门第一季-6-10 练习题

    来源:http://www.imooc.com/ceping/1596 以下关于二维数组的定义和访问正确的是() A int[ ][ ] num = new int[ ][ ]; B int[ ][ ...

  5. extends 和 implements

    extends是继承类与类的 implements是实现接口的 类与类之间只支持单继承 接口与接口之间支持多继承

  6. HTML与CSS二三事

    概述 HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这样就可以让浏览器 ...

  7. clientTop、offsetTop和scrollTop的区分

    页可见区域宽: document.body.clientWidth; 网页可见区域高: document.body.clientHeight; 网页可见区域宽: document.body.offse ...

  8. mongoDB在windows下安装与配置方案

    首先在官网下载mongoDB的安装包: https://www.mongodb.org/downloads 百度云盘下载:http://pan.baidu.com/s/1slUSGYp (安装版 wi ...

  9. 从request获取远程IP地址

    public static String getIpAddr(HttpServletRequest request) { String ip = request.getHeader("X-F ...

  10. /proc/net/tcp中各项参数说明

    /proc/net/tcp中的内容由tcp4_seq_show()函数打印,该函数中有三种打印形式,我们这里这只列出状态是TCP_SEQ_STATE_LISTENING或TCP_SEQ_STATE_E ...