(五) Spring Bean 的生命周期

(五) Spring Bean 的生命周期

 

 

1、简介

      在传统的 Java 应用中,Bean 的生命周期很简单,使用关键字 new 实例化 Bean,当不需要该 Bean 时,由 Java 自动进行垃圾回收。
      Spring 中 Bean 的生命周期较复杂,可以表示为:Bean 的定义   ->   Bean 的初始化   ->   Bean 的使用  ->   Bean 的销毁。

      为了定义安装和拆卸一个 bean,我们只要声明带有 init-method 和/或 destroy-method 参数的 。init-method 属性指定一个方法,在实例化 bean 时,立即调用该方法。同样,destroy-method 指定一个方法,只有从容器中移除 bean 之后,才能调用该方法。

2、Spring Bean生命周期执行流程

Spring 容器在确保一个 Bean 能够使用之前,会进行很多工作。Spring 容器中 Bean 的生命周期流程如下图所示。

java spring 注入bean生命周期 spring加载bean的生命周期_初始化

 Bean 生命周期的整个执行过程描述如下:

1、Spring 启动,查找并加载需要被 Spring 管理的 Bean,并实例化 Bean。

2、利用依赖注入完成 Bean 中所有属性值的配置注入。

3、如果 Bean 实现了 BeanNameAware 接口,则 Spring 调用 Bean 的 setBeanName() 方法传入当前 Bean 的 id 值。

4、如果 Bean 实现了 BeanFactoryAware 接口,则 Spring 调用 setBeanFactory() 方法传入当前工厂实例的引用。

5、如果 Bean 实现了 ApplicationContextAware 接口,则 Spring 调用 setApplicationContext() 方法传入当前 ApplicationContext 实例的引用。

6、如果 Bean 实现了 BeanPostProcessor 接口,则 Spring 调用该接口的预初始化方法 postProcessBeforeInitialzation() 对 Bean 进行加工操作,此处非常重要,Spring 的 AOP 就是利用它实现的。

7、如果 Bean 实现了 InitializingBean 接口,则 Spring 将调用 afterPropertiesSet() 方法。

8、如果在配置文件中通过 init-method 属性指定了初始化方法,则调用该初始化方法。

9、如果 BeanPostProcessor 和 Bean 关联,则 Spring 将调用该接口的初始化方法 postProcessAfterInitialization()。此时,Bean 已经可以被应用系统使用了。

10、如果在 <bean> 中指定了该 Bean 的作用域为 singleton,则将该 Bean 放入 Spring IoC 的缓存池中,触发 Spring 对该 Bean 的生命周期管理;如果在 <bean> 中指定了该 Bean 的作用域为           prototype,则将该 Bean 交给调用者,调用者管理该 Bean 的生命周期,Spring 不再管理该 Bean。

11、如果 Bean 实现了 DisposableBean 接口,则 Spring 会调用 destory() 方法销毁 Bean;如果在配置文件中通过 destory-method 属性指定了 Bean 的销毁方法,则 Spring 将调用该方法对 Bean 进行销毁。

 

注意:  Spring 为 Bean 提供了细致全面的生命周期过程,实现特定的接口或设置 <bean> 的属性都可以对 Bean 的生命周期过程产生影响。建议不要过多的使用 Bean 实现接口,因为这样会导致代码的耦合性过高。了解 Spring 生命周期的意义就在于,可以利用 Bean 在其存活期间的指定时刻完成一些相关操作。一般情况下,会在 Bean 被初始化后和被销毁前执行一些相关操作。
Spring 官方提供了 3 种方法实现初始化回调和销毁回调:

  1. 实现 InitializingBean 和 DisposableBean
  2. 在 XML 中配置 init-method 和 destory-method;
  3. 使用 @PostConstruct 和 @PreDestory

在一个 Bean 中有多种生命周期回调方法时,优先级为:注解 > 接口 > XML

接口比XML配置效率高,但是xml配置消除了对spring的依赖。而实现InitializingBean接口依然采用对spring的依赖不建议使用接口和注解,这会让 pojo 类和 Spring 框架紧耦合。

 在Spring初始化bean的时候,如果该bean实现了InitializingBean接口,并且同时在xml配置文件中指定了init-method,系统则是先调用afterPropertieSet()方法,然后再调用init-method中指定的方法。

通过查看Spring加载bean的源码类 AbstractAutowiredCapableBeanFactory 可以看出其中的奥妙,AbstractAutowiredCapableBeanFactory类中的invokeInitMethods说的非常清楚,如下:

protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd) throws Throwable {
    //判断该bean是否实现了实现了InitializingBean接口,如果实现了InitializingBean接口,则只掉调用bean的afterPropertiesSet方法
    boolean isInitializingBean = (bean instanceof InitializingBean);
    if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
        if (logger.isDebugEnabled()) {
            logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
        }
         
        if (System.getSecurityManager() != null) {
            try {
                AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
                    public Object run() throws Exception {
                        //直接调用afterPropertiesSet
                        ((InitializingBean) bean).afterPropertiesSet();
                        return null;
                    }
                },getAccessControlContext());
            } catch (PrivilegedActionException pae) {
                throw pae.getException();
            }
        }                
        else {
            //直接调用afterPropertiesSet
            ((InitializingBean) bean).afterPropertiesSet();
        }
    }
    if (mbd != null) {
        String initMethodName = mbd.getInitMethodName();
        //判断是否指定了init-method方法,如果指定了init-method方法,则再调用制定的init-method
        if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
                !mbd.isExternallyManagedInitMethod(initMethodName)) {
            //进一步查看该方法的源码,可以发现init-method方法中指定的方法是通过反射实现
            invokeCustomInitMethod(beanName, bean, mbd);
        }
    }
}

 

3、使用

    3.1、初始化回调

1. 使用接口

实现 org.springframework.beans.factory.InitializingBean 接口提供了以下方法:

void afterPropertiesSet() throws Exception;

您可以实现以上接口,在 afterPropertiesSet 方法内指定 Bean 初始化后需要执行的操作。

<bean id="..." class="..." />
public class User implements InitializingBean {
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("调用接口:InitializingBean,方法:afterPropertiesSet,无参数");
    }
}

2. 配置XML

可以通过 init-method 属性指定 Bean 初始化后执行的方法。

<bean id="..." class="..." init-method="init"/>
public class User {
    public void init() {
        System.out.println("调用init-method指定的初始化方法:init" );
    }
}

3. 使用注解

使用 @PostConstruct 注解标明该方法为 Bean 初始化后的方法。

public class ExampleBean {
    @PostConstruct
    public void init() {
        System.out.println("@PostConstruct注解指定的初始化方法:init" );
    }
}

 

   3.2、销毁回调

1. 使用接口

实现 org.springframework.beans.factory.DisposableBean 接口提供了以下方法:

void destroy() throws Exception;

您可以实现以上接口,在 destroy 方法内指定 Bean 初始化后需要执行的操作。

<bean id="..." class="..." />
public class User implements DisposableBean {
    @Override
    public void destroy() throws Exception {
        System.out.println("调用接口:destroy,方法:destroy,无参数");
    }
}

2. 配置XML

可以通过 destroy-method 属性指定 Bean 销毁后执行的方法。

<bean id="..." class="..." destroy-method="destroy"/>
public class User {
    public void destroy() {
        System.out.println("调用destroy-method指定的销毁方法:destroy" );
    }
}

3. 使用注解

使用 @PreDestory 注解标明该方法为 Bean 销毁前执行的方法。

public class ExampleBean {
    @PreDestory 
    public void destroy() {
        System.out.println("@PreDestory注解指定的初始化方法:destroy" );
    }
}

 

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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="helloWorld" class="com.dw.study.HelloWorld"
        init-method="init" destroy-method="destroy">
        <property name="message" value="Hello World!" />
    </bean>
</beans>

HelloWorld 类代码如下:

public class HelloWorld {
    private String message;
    public void setMessage(String message) {
        this.message = message;
    }
    public void getMessage() {
        System.out.println("message : " + message);
    }
    public void init() {
        System.out.println("Bean正在进行初始化");
    }
    public void destroy() {
        System.out.println("Bean将要被销毁");
    }
}

 

   3.3、默认的初始化和销毁方法

如果你有太多具有相同名称的初始化或者销毁方法的 Bean,那么你不需要在每一个 bean 上声明初始化方法销毁方法。框架使用 元素中的 default-init-method 和 default-destroy-method 属性提供了灵活地配置这种情况,如下所示:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
    default-init-method="init" 
    default-destroy-method="destroy">

   <bean id="..." class="...">
       <!-- collaborators and configuration for this bean go here -->
   </bean>

</beans>