一、引入

在java的实际开发过程中,我们可能需要在spring实例化一个bean的过程中,使用到初始化一个对象(bean)后立即初始化(加载)一些数据,或者在销毁一个对象之前进行执行一些事情等等。
因此Spring为我们提供了一系列的方式:

方式 初始化 init 销毁destroy
1 @bean 注解,指定属性initMethod @bean 注解,指定属性destroyMethod
2 xml形式,指定 init-method xml形式,指定 destroy-method
3 实现InitializingBean接口,重写afterPropertiesSet方法 实现DisposableBean接口,重写destroy方法
4 注解@PostConstruct 注解@PreDestroy

二、方式详解

  1. @bean 注解
@Bean(name="user",initMethod = "init",destroyMethod = "destroy")public User user(String name) {return new User(name);}
  1. xml形式

<bean id="user" class="com.demo.user" init-method="init" destroy-method="destroy"></bean>
  1. 接口InitializingBean和DisposableBean
    实现InitializingBean接口,重写afterPropertiesSet()方法

public interface InitializingBean {/*** Invoked by the containing {@code BeanFactory} after it has set all bean properties* and satisfied {@link BeanFactoryAware}, {@code ApplicationContextAware} etc.* <p>This method allows the bean instance to perform validation of its overall* configuration and final initialization when all bean properties have been set.* @throws Exception in the event of misconfiguration (such as failure to set an* essential property) or if initialization fails for any other reason*/void afterPropertiesSet() throws Exception;}

实现DisposableBean方法,重写destroy方法

/*** Interface to be implemented by beans that want to release resources* on destruction. A BeanFactory is supposed to invoke the destroy* method if it disposes a cached singleton. An application context* is supposed to dispose all of its singletons on close.** <p>An alternative to implementing DisposableBean is specifying a custom* destroy-method, for example in an XML bean definition.* For a list of all bean lifecycle methods, see the BeanFactory javadocs.** @author Juergen Hoeller* @since 12.08.2003* @see org.springframework.beans.factory.support.RootBeanDefinition#getDestroyMethodName* @see org.springframework.context.ConfigurableApplicationContext#close*/
public interface DisposableBean {/*** Invoked by a BeanFactory on destruction of a singleton.* @throws Exception in case of shutdown errors.* Exceptions will get logged but not rethrown to allow* other beans to release their resources too.*/void destroy() throws Exception;}
  1. @PostConstruct和@PreDestroy

//初始化后执行的方法,必须为voidlei'xing类型@PostConstructpublic void init() {***do someThing}//bean销毁前执行的方法
@PreDestroy
public void init() {***do someThing
}

从Java EE 5规范开始,Servlet中增加了两个影响Servlet生命周期的注解:@PostConstruct和@PreDestroy。
被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。
被@PreDestroy修饰的方法会在服务器卸载Servlet的时候运行,并且只会被服务器调用一次,类似于Servlet的destroy()方法。被@PreDestroy修饰的方法会在destroy()方法之后运行,在Servlet被彻底卸载之前。

三、总结

一)对Spring版本的要求

  • xml和实现接口的方式是Spring最原始的方式,Spring任何版本均支持

  • @PostConstruct和@PreDestroy需要Spring2.5及以上版本

  • @bean :需要Spring 3.0以上版本支持

    二)执行顺序不一样
    其中
    此处引入Spring中bean生命周期的介绍

引自: 深究Spring中Bean的生命周期.

初始化之后执行顺序: @PostConstruct > InitializingBean > Beaninit-method(xml注解或者@Bean)

销毁之前执行顺序:@preDestroy > DisposableBean > destoryMethod(xml注解或者@Bean)

Spring中bean的执行初始化和销毁方法的4种方式详解相关推荐

  1. Spring中Bean管理操作基于XML配置文件方法实现

    Spring中Bean管理操作基于XML配置文件方法实现 基于XML配置文件方式实现 1.基于`xml`方式创建对象 2.基于`xml`方式注入属性 1.创建类,定义属性和对应的set方法 2.在Sp ...

  2. mysql 删除数据表中数据_Mysql-删除数据表-三种方式详解

    Mysql 删除数据表的三种方式详解 用法: 1.当你不再需要该表时, 用 drop; 2.当你仍要保留该表,但要删除所有记录时, 用 truncate; 3.当你要删除部分记录或者有可能会后悔的话, ...

  3. java clone方法_干货满满:Java中创建对象的五种方式详解

    通常来说,对象具有状态和行为,变量用来表明对象的状态,方法表明对象所具有的行为. 作为Java开发者,我们通常都是使用依赖管理系统,比如Spring去创建Java对象,但使用管理系统创建对象并不是唯一 ...

  4. spring bean实现init/destory生命周期方法的三种方式

    实现InitializingBean和DisposableBean接口,实现afterPropertiesSet()和destroy()方法 使用@PostConstruct和@PreDestroy进 ...

  5. java中for的常规用法_Java for循环的几种用法详解

    本文非常适合初学Java的程序员,主要是来了解一下Java中的几种for循环用法,分析得十分详细,一起来看看. J2SE 1.5提供了另一种形式的for循环.借助这种形式的for循环,可以用更简单地方 ...

  6. Pandas中常见的20多种数据筛选方法,116张图详解 | 图解Pandas-图文第8篇

    01写在前面 大家好,我是阳哥,欢迎来到「Python数据之道」. 本次是<图解Pandas>系列图文内容的 第 08 篇,主要介绍 Pandas 中常见的数据筛选 . 本文是付费阅读文章 ...

  7. Spring官网阅读(九)Spring中Bean的生命周期(上)

    文章目录 生命周期回调 1.Bean初始化回调 2.Bean销毁回调 3.配置默认的初始化及销毁方法 4.执行顺序 5.容器启动或停止回调 Lifecycle 接口 LifecycleProcesso ...

  8. 面试题------Spring中Bean的初始化以及销毁init-method、destory-method

    面试题------Spring中Bean的生命周期 通过Spring工厂,可以控制bean的生命周期. 在xml中配置Bean的初始化和销毁方法 通过init-method属性指定初始化后的调用方法. ...

  9. Spring中Bean初始化和销毁的多种方式

    Spring中Bean初始化和销毁的多种方式 一.Bean的多种初始化方式 1.PostConstruct注解 2.实现InitializingBean接口 3.声明init-method方法 二.B ...

最新文章

  1. DASH流媒体MPD文件解析
  2. 职称计算机 菏泽,山东菏泽2016年职称计算机首批考试时间
  3. Matplotlib实例教程 | 配色表 colors
  4. 深度洞见|起底元宇宙风潮,如何重塑未来数字营销?
  5. 为什么python输入中文变成竖的_Python中文竖排显示的方法
  6. springmvc,spring,hibernate5.0整合
  7. 【PAT - 1014】福尔摩斯的约会(简单模拟)
  8. Oracle sqlldr
  9. 开源协议神图介绍 MIT 与 Apache 等
  10. 多项式拟合怎么确定次数_PyTorch入门4 搭建多项式回归模型
  11. Bailian2886 能被3除尽的数之和【入门】
  12. I/O多路复用之select,poll,epoll简介
  13. binlog的基本介绍和操作
  14. 华为笔记本电脑home键和end键快捷方式
  15. win10升级后删除自带的微软输入法
  16. 飞腾CPU BIOS固件生成教程
  17. 2021年中国饮料酒产业链及产量现状分析:产量同比增长2.73%[图]
  18. 基于vue实现sku商品选择
  19. oeasy教您玩转vim - 89 - # 高亮细节Highlight
  20. 我查查 并行数据处理应对突发流量

热门文章

  1. java protostuff 好处,java序列化Protostuff和Serializable的区别
  2. npm和npx的区别
  3. 一个成功人士应该具备的能力
  4. 数据结构之八大排序——希尔排序
  5. c# 创建excel表头及内容
  6. 效果图如何做出插画风格?SketchUp 插画风效果图渲染技巧
  7. 还是经典的键盘,还是熟悉的味道:黑莓KEY2是否值得买?
  8. GNSS/INS组合导航实习面试
  9. Gnocchi 7、Gnocchi中的水平扩展
  10. 微信小程序开发(九):使用扩展组件库