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. sqoop job命令自动生成

    批量生成 #!/usr/bin/env python import MySQLdb import datetime import time import os #source database HOS ...

  2. Latex感想

    看人家,一个小女生,被导师逼着,首先对电脑方面理解不多,3天时间,latex已经把Paper框架建立起来,我可以说我自愧不如吗?德鲁克<卓有成效的管理者>说的一次干一件事情,<冬吴相 ...

  3. 查看SQLServer最耗资源时间的SQL语句

    执行最慢的SQL语句 SELECT (total_elapsed_time / execution_count)/1000 N'平均时间ms' ,total_elapsed_time/1000 N'总 ...

  4. SVN服务器的搭建和使用

    VisualSVN Server和TortoiseSVN的下载,安装,汉化:SVN服务器搭建和使用(一) 如何使用VisualSVN Server建立版本库,以及TortoiseSVN的使用:SVN服 ...

  5. OnNcCalcSize改变标题栏等的高度

    在创建窗口时,当收到 WM_NCCALCSIZE 消息时才指定客户区.不管什么时候,只要 Windows 想知道窗口客户区的大小,它便会发送这个消息. NCCALCSIZE_PARAMS 结构保存三个 ...

  6. SNMP开发c#,高薪诚聘,或者外包开发

    现在手里有一个IT资产管理方面的项目,主要会用到SNMP协议,要用这个协议进行开发,对网络上的设备进行控制, 现在高薪诚聘高手,或者项目外包合作,有意向的同学们可以联系我,时间紧迫, QQ:39011 ...

  7. js原型基础

    js中的原型链是实现继承等面向对象的一种方式,而我这里不详细展开,仅仅是画了一张原型部分结构图,主要说明: 1._proto_和prototype之间的关系 2.原型链的起点是Object.proto ...

  8. Aptana插件安装方法

    本人用的是Zend Studio10.0,在开发项目过程中,发现该软件无法对css和js进行代码提示,这样用起来很不方便,然后在网上找了一下Aptana插件 进入Aptana官网:http://www ...

  9. 百度云 + GIT

    百度云同步 百度云同步,会将本地的某个文件目录和云端进行同步.如果在本地将这个同步的目录设置为Git的中心服务器,那么本地push到中心服务器的内容也会被同步到云端.其他开发者只要也进行相同的设置,就 ...

  10. C++小常识笔记

    1.C++的继承保护级别最好是显示的写出来.(注:class 的默认继承为private,struct的默认继承为public). 例: class Base{/**/}; struct D1 : B ...