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. android sdk manager 代理设置(送给牛逼的)

    解决android sdk更新慢的问题(公司竟然把sdk更新给墙了). 第一步:如下图 第二部:进入代理设置页面,进行设置.如下图  

  2. sencha touch+phonegap+node.js打包

    这讲我们来讲解下如何使用phonegapa创建项目环境并通过她们将sencha touch打包成app,这里我们只讲解打包android的apk,打包ios的过程有点类似,但是需要在mac环境下,最后 ...

  3. ReactNative win10初体验

    根据RN中文网上的文档搭建环境,有的地方说的不是很清楚,所以小白的我搞了很久,终于搭建成功,这里分享一下 1. java: jdk和jre,我下载的是1.8(因为官方文档上说AndroidStudio ...

  4. Life cycle of plist in Lockdown changes dramatically in iOS 10

    We could take advantage of plist to bypass Trust Relationship so as to extract data from a iDevice. ...

  5. 登陆判读,并跳转到指定页面(window.location.href='http://localhost/index.html')

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 functio ...

  6. TCP应用编程

    TCP是Transmission Control Protocol(传输控制协议)的简称,是TCP/IP体系中面向连接的运输层协议,在网络中提供全双工的和可靠的服务. TCP协议最主要的特点是: 1) ...

  7. Linux内核分析总结

    张潇月 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 学习目录: (1)计算机是如何工作的   h ...

  8. Nodejs+Express创建HTTPS服务器

    为了使我的Nodejs服务器提供HTTPS服务,学习了一下如何利用express创建https服务器,现记录如下.(一点一点的积累与掌握吧) 1. Http与Https 介绍 HTTP: 超文本传输协 ...

  9. SpringMVC无法获取请求中的参数的问题的调查与解决(2)

    由于Request的getInputSteam()一旦获取一次后,就再也无法获取了 在实际项目中导致下面的问题: 1,多个拦截器,Filter都需要从InputStream中拿数据的情况无法处理: 2 ...

  10. mybatis单个插入和批量插入的简单比较

    在J2EE项目中,mybatis作为主流持久层框架,许多知识值得我们去钻研学习,今天,记录一下数据插入性能(单个插入和批量插入). 一,测试对象 public class Test { private ...