• 在Spring Bean的生命周期中各方法的执行顺序


    Spring 容器中的 Bean 是有生命周期的,Spring 允许在 Bean 在初始化完成后以及 Bean 销毁前执行特定的操作,常用的设定方式有以下十种:

    1. 通过实现 InitializingBean 接口来定制初始化之后的操作方法;
    2. 通过实现DisposableBean 接口来定制销毁之前的操作方法;
    3. 通过元素的 init-method属性指定初始化之后调用的操作方法;
    4. 通过元素的 destroy-method属性指定销毁之前调用的操作方法;
    5. 在指定方法上加上 @PostConstruct 注解来制定该方法是在初始化之后调用;
    6. 在指定方法上加上 @PreDestroy 注解来制定该方法是在销毁之前调用;
    7. 实现BeanNameAware接口设置Bean的ID或者Name;
    8. 实现BeanFactoryAware接口设置BeanFactory;
    9. 实现ApplicationContextAware接口设置ApplicationContext;
    10. 注册实现了BeanPostProcessor的Bean后处理器,通过postProcessBeforeInitialization和postProcessAfterInitialization 方法对初始化的Bean进行自定义处理;

    那么这十种方式所定义方法执的行顺序是什么?
    鄙人经过大量的代码实验得到了一些结果记录下来,以供用到时查阅。

    初始化过程中各方法的执行顺序如下:

    1. 调用构造器 Bean.constructor,进行实例化;
    2. 调用 Setter 方法,设置属性值;
    3. 调用 BeanNameAware.setBeanName,设置Bean的ID或者Name;
    4. 调用 BeanFactoryAware.setBeanFactory,设置BeanFactory;
    5. 调用 ApplicationContextAware.setApplicationContext,置ApplicationContext;
    6. 调用BeanPostProcessor的预先初始化方法,如下:
      BeanPostProcessor1.postProcessBeforeInitialization
      BeanPostProcessor2.postProcessBeforeInitialization
      BeanPostProcessor3.postProcessBeforeInitialization
      ……
    7. 调用由 @PostConstruct 注解的方法;
    8. 调用 InitializingBean.afterPropertiesSet;
    9. 调用 Bean.init-mehod 初始化方法;
    10. 调用BeanPostProcessor的后初始化方法,如下:
      BeanPostProcessor1.postProcessAfterInitialization
      BeanPostProcessor2.postProcessAfterInitialization
      BeanPostProcessor3.postProcessAfterInitialization
      ……

    容器关闭时,Bean销毁过程中各方法的执行顺序如下:

    1. 调用由 @PreDestroy 注解的方法
    2. 调用DisposableBean的destroy();
    3. 调用定制的destroy-method方法;

    实验代码如下:

    文件 bean.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:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           "
           default-init-method="init_method" default-destroy-method="destroy_method">
      <context:component-scan   base-package="spring" />
      <bean class="spring.InitHelloWorld2"/>
      <bean class="spring.InitHelloWorld"/>
      <bean id="hello" class="spring.Hello1" >
        <property name="message" value="ccccccccc"/>
      </bean>
    </beans>
    

    文件 Hello1.java 的代码如下:

    package spring;
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.*;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;    
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;     
    public class Hello1 implements InitializingBean, BeanNameAware,BeanFactoryAware,ApplicationContextAware,DisposableBean{
      private String message;      
      public String getMessage(){
        return message;
      }      
      public void setMessage(String message){
        System.out.println("Hello1 setMessage");
        this.message = message;
      }      
      public Hello1(){
        System.out.println("Hello1 构造器");
      }      
      public void hello(){
        System.out.println("Hello1 我是:" + message);
      }      
      public void init_method(){
        System.out.println("Hello1 init-method ");
      }    
      @PostConstruct
      public void postConstruct(){
        System.out.println("Hello1 @PostConstruct");
      }      
      @Override
      public void afterPropertiesSet() throws Exception{
        System.out.println("Hello1 InitializingBean.afterPropertiesSet");
      }      
      @Override
      public void setBeanName(String s){
        System.out.println("Hello1 setBeanName " + s);
      }      
      @Override
      public void setBeanFactory(BeanFactory beanFactory) throws BeansException{
        System.out.println("Hello1 setBeanFactory " );
      }      
      @Override
      public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{      
        System.out.println("Hello1 setApplicationContext " );
      }      
      @Override
      public void destroy() throws Exception{
        System.out.println("Hello1 DisposableBean.destroy " );
      }      
      public void destroy_method() throws Exception{
        System.out.println("Hello1 destroy-method " );
      }      
      @PreDestroy
      public void preDestroy() throws Exception{
        System.out.println("Hello1 @PreDestroy " );
      }
    }
    

    文件 InitHelloWord.java 的代码如下:

    package spring;    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.InitializingBean;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    import org.springframework.core.Ordered;    
    public class InitHelloWorld implements BeanPostProcessor,Ordered{
      @Override
      public Object postProcessBeforeInitialization(Object o, String s) throws BeansException{
        System.out.println("BeanPostProcessor1.postProcessBeforeInitialization:" + s);
        return o;
      }      
      @Override
      public Object postProcessAfterInitialization(Object o, String s) throws BeansException{
        System.out.println("BeanPostProcessor1.postProcessAfterInitialization:" + s);
        return o;
      }      
      @Override
      public int getOrder(){
        return 10;
      }
    }
    

    文件 InitHelloWord2.java 的代码如下:

    package spring;    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    import org.springframework.core.Ordered;
    public class InitHelloWorld2 implements BeanPostProcessor,Ordered{
      @Override
      public Object postProcessBeforeInitialization(Object o, String s) throws BeansException{
        System.out.println("BeanPostProcessor2.postProcessBeforeInitialization:" + s);
        return o;
      }      
      @Override
      public Object postProcessAfterInitialization(Object o, String s) throws BeansException{
        System.out.println("BeanPostProcessor2.postProcessAfterInitialization:" + s);
        return o;
      }      
      @Override
      public int getOrder(){
        return 20;
      }
    }
    

    文件 MainApp.java 代码如下:

    package spring;    
    import org.springframework.context.support.AbstractApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;    
    public class MainApp{
      public static void main(String... args){
        AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
        Hello1 hello = (Hello1) ctx.getBean("hello");
        hello.hello();
        ctx.close();
      }
    }
    

    代码执行结果如下:

    Hello1 构造器
    Hello1 setMessage
    Hello1 setBeanName hello
    Hello1 setBeanFactory 
    Hello1 setApplicationContext 
    BeanPostProcessor1.postProcessBeforeInitialization:hello
    BeanPostProcessor2.postProcessBeforeInitialization:hello
    Hello1 @PostConstruct
    Hello1 InitializingBean.afterPropertiesSet
    Hello1 init-method 
    BeanPostProcessor1.postProcessAfterInitialization:hello
    BeanPostProcessor2.postProcessAfterInitialization:hello
    Hello1 我是:ccccccccc
    Hello1 @PreDestroy 
    Hello1 DisposableBean.destroy 
    Hello1 destroy-method 




    链接:https://www.jianshu.com/p/80d4fa132747

  • 相关阅读:
    React Native For Android 环境搭建
    MySql 打开日志文件
    PHP使用JSON通信
    PHPStorm 调式JS /同时调式PHP和jS
    PHP list,explode的使用
    PHP foreach使用
    PHP eof的使用
    使用Spring缓存的简单Demo
    android源码的目录结构
    在Ubuntu上安装LAMP服务器
  • 原文地址:https://www.cnblogs.com/miracle77hp/p/10107106.html
Copyright © 2020-2023  润新知