美文网首页
Spring--Bean初始化和销毁前的操作顺序

Spring--Bean初始化和销毁前的操作顺序

作者: 栗子酥小小 | 来源:发表于2018-07-18 22:51 被阅读0次

Spring 允许在 Bean 在初始化完成后以及 Bean 销毁前执行特定的操作,常用的设定方式有以下三种:

  1. 通过Bean实现 InitializingBean/DisposableBean 接口来定制初始化之后/销毁之前的操作方法;【缺点:要依赖Spring】

  2. 通过 <bean> 元素的 init-method/destroy-method属性指定初始化之后 /销毁之前调用的操作方法;(也可以不是xml配置,而是在@Bean上注解,效果相同)【优点:不依赖于Spring的接口】

  3. 在指定方法上加上@PostConstruct 或@PreDestroy 注解来制定该方法是在初始化之后还是销毁之前调用【在servlet中,要考虑的执行流程是:servlet构造函数 > PostConstruct > init() > service() > destory() > PreDestroy】

注意:子类实例化过程中会调用父类中的@PostConstruct方法!

但他们之前并不等价。即使3个方法都用上了,也有先后顺序.

Bean在实例化的过程中:Constructor > @PostConstruct >InitializingBean > init-method
Bean在销毁的过程中:@PreDestroy > DisposableBean > destroy-method
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class InitAndDestroySeqBean implements InitializingBean,DisposableBean {

    public InitAndDestroySeqBean(){
        System.out.println("执行InitAndDestroySeqBean: 构造方法");
    }
    
    @PostConstruct
    public void postConstruct() {  
       System.out.println("执行InitAndDestroySeqBean: postConstruct");  
    }  
    
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("执行InitAndDestroySeqBean: afterPropertiesSet"); 
    }
    
    public void initMethod() {
        System.out.println("执行InitAndDestroySeqBean: init-method");
    }

    @PreDestroy
    public void preDestroy()  {
        System.out.println("执行InitAndDestroySeqBean: preDestroy");
    }
    
    @Override
    public void destroy() throws Exception {
        System.out.println("执行InitAndDestroySeqBean: destroy");
    }
    
    public void destroyMethod() {
        System.out.println("执行InitAndDestroySeqBean: destroy-method");
    }
    
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("com/chj/spring/bean.xml");
        context.close();
    }
}

<?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:context="http://www.springframework.org/schema/context"  
     xsi:schemaLocation="http://www.springframework.org/schema/beans  
 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
 http://www.springframework.org/schema/context  
 http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
 
   <context:annotation-config/>  
   
   <bean id="initAndDestroySeqBean" class="com.chj.spring.InitAndDestroySeqBean" init-method="initMethod" destroy-method="destroyMethod"/>
</beans>

相关文章

  • Spring--Bean初始化和销毁前的操作顺序

    Spring 允许在 Bean 在初始化完成后以及 Bean 销毁前执行特定的操作,常用的设定方式有以下三种: 通...

  • 构造器与多态

    构造器与多态 通常情况下,对象销毁都是由jvm垃圾回收器进行处理,这里只是通过代码展示初始化顺序和清理操作顺序。 ...

  • Spring容器初始化和销毁定义操作

    Spring容器初始化和销毁定义操作 第一种:通过@PostConstruct 和 @PreDestroy 方法 ...

  • bean生命周期

    创建--》初始化(赋值)--》销毁 设置初始化和销毁调用函数 继承接口也可以实现初始化和销毁 JSR250规范方式...

  • java反射机制

    一、java类初始化 初始化操作:按照源代码从上到下的顺序依次执行静态代码块和初始化静态域。在一个类被初始化前,其...

  • 线程安全之信号量

    相关API 初始化信号量 销毁 p操作 v操作 互斥实现 同步实现

  • spring 初始化和销毁方法

    在容器创建前叫做初始化方法,容器关闭后叫做销毁方法。 配置如下: a.初始化方法: b.销毁方法 销毁方法需要用...

  • 数据结构 线性表的实现(2) ——基本操作

    在数据结构 线性表的实现(1) ——创建与销毁 中,简要的说明了顺序表的声明、初始化和销毁。对于我们使用者来说,顺...

  • Bean的生命周期:

    配置Bean的初始化和销毁的方法:配置初始化和销毁的方法: init-method=”setup” destroy...

  • 数据结构(顺序表常用操作)

    上篇有顺序表的大概介绍和初始化,以及存储数据的操作,接下来是顺序表的常用操作 顺序表插入元素 向已有的顺序表插入元...

网友评论

      本文标题:Spring--Bean初始化和销毁前的操作顺序

      本文链接:https://www.haomeiwen.com/subject/wszapftx.html