Spring Bean 的生命周期

1.Spring Bean生命周期概述

传统java对象的生命开始于调用实例化对象的new运算符,并且在对象符合垃圾回收的条件时调用finalize()方法。与传统的java对象相比,Spring bean的生命周期不同。

Spring框架提供了以下可用来控制Bean生命周期的方法:

  1. InitializingBeanDisposableBean回调接口;
  2. BeanName, BeanFactoryApplicationContext用于特定行为的Aware接口;
  3. Bean配置文件中配置自定义的init()destroy()方法;
  4. 基于注解的配置 - @PostConstruct@PreDestroy注解;

下图显示了完整的生命周期方法(从实例化到准备使用)。

image_1

下图显示销毁时调用的方法。

image_2

1.1 InitializingBeanDisposbleBean回调接口

  • InitalizingBean接口是在org.springframework.beans.factory包下定义的,并声明了一个可用于添加任何初始化相关代码的方法。实现InitalizingBean的任何Bean都需要提供afterPropertiesSet()方法的实现。
    方法的签名是:
   void afterPropertiesSet() throws Exception; 
  • 同样,DisposableBean接口在org.springframework.beans.factory下定义并声明了一个单独的方法,当Bean被销毁时可以被执行,并且可以用来添加任何清理相关的代码。实现DisposableBean的任何Bean都需要提供destroy()方法的实现。
    方法的签名是:
    void destroy() throws Exception;

这种方法使用起来很简单,但不推荐,因为它会在我们的Bean实现中与Spring框架紧密耦合。

1.2 BeanNameBeanFactoryApplicationContextAware接口

Several times functionality requires infrastructure or we can say application context information in a bean
Bean中多数功能需要用到基础架构信息,或者我们可以说是应用程序的上下文信息。为了实现这些功能,Spring框架提供了一系列Aware接口,每个接口都要求我们实现一个方法来在Bean中注入依赖。
最常用的是 -

  • BeanFactoryAware - 该接口提供了setBeanFactory()方法,该方法将拥有的Bean工厂实例提供给Bean.
    方法签名是:
    void setBeanFactory(BeanFactory beanFactory) throws BeansException
  • BeanNameAware - 该接口提供了setBeanName()方法,该方法在创建该BeanBean工厂中设置Bean的名称.
    方法签名是:
    void setBeanName(String name);     
  • ApplicationContextAware -该接口提供了setApplicationContext()方法,该方法将所拥有的应用程序上下文实例提供给Bean
    方法签名是:
    void setApplicationContext(ApplicationContext applicationContext) throws BeansException

1.3 在Bean配置文件中自定义init()destroy()方法

实现InitalizingBeanDisposableBean接口很容易使用,但在我们的Bean实现中与Spring框架紧密结合。
或者,我们可以在Spring Bean配置文件中为Beaninit-methoddestroy-method自定义属性值。这是推荐的方法,因为没有直接依赖Spring框架,我们可以创建自己的方法。
注意:post-initpre-destroy方法都应该没有参数,但可以引发异常。

<beans>
    <bean id="bean_id" class="bean.class" 
      init-method="customInitmethod"
     destroy-method="customDestroymethod">
  </bean>
</beans>

我们可以配置将应用于所有Bean的默认init-methoddestroy-method(全局)。当我们有一个为所有Bean一致定义常用方法名称,如init()destroy()的模式时,它们非常有用。

<beans default-init-method=”customDefaultInitMethod” default-destroy-method=”customDefaultDestroyMethod” >
    <bean id="bean_id" class="bean.class" >
</bean>
</beans>

2.Spring Aware

有时我们需要在我们的bean中使用Spring Framework对象来执行一些操作,例如读取ServletConfigServletContext参数,或者了解由ApplicationContext加载的bean定义,这就是为什么Spring框架提供了一堆* Aware接口,我们可以在我们的bean类中实现这些接口。

Spring Aware接口类似于具有回调方法和实现观察者设计模式的Servlet Listener

一些重要的Aware接口:

  • ApplicationContextAware – 注入ApplicationContext对象, 示例用法是获取bean定义名称的数组.
  • BeanFactoryAware – 注入BeanFactory对象, 示例用法是检查bean的范围.
  • BeanNameAware – 了解配置文件中定义的bean名称.
  • ResourceLoaderAware – 注入ResourceLoader对象, 示例用法是获取类路径中文件的输入流.
  • ServletContextAware – 在MVC应用程序中注入ServletContext对象, 示例用法是读取上下文参数和属性.
  • ServletConfigAware – 在MVC应用程序中注入ServletConfig对象, example 用法是获取servlet配置参数.

【参考资料】

  1. Spring Bean Life Cycle
  2. Spring - Bean Life Cycle
  3. 07 - Spring Bean Life Cycle

猜你喜欢

转载自blog.csdn.net/qq_35070673/article/details/80254727
今日推荐