换我心,为你心,始知相忆深。
概述
在本章中,我们学习Spring bean的生命周期。掌握bean生命周期的各个阶段,初始化和销毁回调方法。我们将学习使用XML配置和注释配置来控制bean生命周期事件。
Bean的声明周期
当容器启动时–-需要基于Java或XML bean定义实例化Spring bean。还需要执行一些初始化后的步骤,以使其进入可用状态。Spring Boot启动应用程序也具有相同的bean生命周期。
之后,当不再需要该bean时,它将被从IoC容器中删除。
Spring bean factory负责管理通过Spring容器创建的bean的生命周期。
生命周期回调
Spring bean factory控制bean的创建和销毁。为了执行一些自定义操作,它提供了回调方法,这些方法可以大致分为两类:
Post-initialization回调方法Pre-destruction回调方法
生命周期图解
生命周期回调方法
Spring框架提供了以下4种方法来控制Bean的生命周期事件:
InitializingBean和DisposableBean回调接口*Aware接口提供一些特殊的实现Bean配置文件中的自定义init()和destroy()方法@PostConstruct和@PreDestroy注解
InitializingBean和DisposableBean接口
org.springframework.beans.factory.InitializingBean接口允许bean在容器设置了bean的所有必要属性之后执行初始化工作。
InitializingBean接口指定一个方法:
void afterPropertiesSet() throws Exception;
这并不是初始化bean的首选方法,因为它将bean类与spring容器紧密地耦合在一起。更好的方法是在applicationContext.xml文件的bean定义中使用init-method属性。
类似地,实现org.springframework.beans.factory.DisposableBean接口允许Bean在包含它的容器被销毁时获得回调。
DisposableBean接口指定一个方法:
void destroy() throws Exception;
// 实现上述接口的示例bean:
package cn.howtodoinjava.task;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class DemoBean implements InitializingBean, DisposableBean
{
//Other bean attributes and methods
@Override
public void afterPropertiesSet() throws Exception
{
//Bean initialization code
}
@Override
public void destroy() throws Exception
{
//Bean destruction code
}
}
*Aware接口
Spring提供了一系列*Aware接口,允许bean向容器表明它们需要某种基础设施依赖。每个接口都需要您实现一个方法来将依赖项注入bean。
这些接口可以概括为:
*Aware接口 |
重写方法 | 目的 |
|---|---|---|
ApplicationContextAware |
` void setApplicationContext (ApplicationContext applicationContext) throws BeansException;` | 接口将由任何希望将其运行的ApplicationContext通知给它的对象来实现。 |
ApplicationEventPublisherAware |
void setApplicationEventPublisher (ApplicationEventPublisher applicationEventPublisher); |
设置此对象运行的ApplicationEventPublisher。 |
BeanClassLoaderAware |
void setBeanClassLoader (ClassLoader classLoader); |
将bean类加载器提供给bean实例的回调。 |
BeanFactoryAware |
void setBeanFactory (BeanFactory beanFactory) throws BeansException; |
将拥有的工厂提供给Bean实例的回调。 |
BeanNameAware |
void setBeanFactory (BeanFactory beanFactory) throws BeansException; |
在创建此bean的bean工厂中设置bean的名称。 |
BootstrapContextAware |
void setBootstrapContext (BootstrapContext bootstrapContext); |
设置该对象在其中运行的BootstrapContext。 |
LoadTimeWeaverAware |
void setLoadTimeWeaver (LoadTimeWeaver loadTimeWeaver); |
设置此对象包含ApplicationContext的LoadTimeWeaver。 |
MessageSourceAware |
void setMessageSource (MessageSource messageSource); |
设置此对象在其中运行的MessageSource。 |
NotificationPublisherAware |
void setNotificationPublisher (NotificationPublisher notificationPublisher); |
为当前的托管资源实例设置NotificationPublisher实例。 |
PortletConfigAware |
void setPortletConfig (PortletConfig portletConfig); |
设置运行该对象的PortletConfig。 |
PortletContextAware |
void setPortletContext (PortletContext portletContext); |
设置此对象在其中运行的PortletContext。 |
ResourceLoaderAware |
void setResourceLoader (ResourceLoader resourceLoader); |
设置此对象在其中运行的ResourceLoader。 |
ServletConfigAware |
void setServletConfig (ServletConfig servletConfig); |
设置运行该对象的ServletConfig。 |
ServletContextAware |
void setServletContext (ServletContext servletContext); |
设置运行该对象的ServletContext。 |
下面的Java代码块展示了使用*Aware接口 来控制bean生命周期的用法。
package cn.howtodoinjava.task;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.weaving.LoadTimeWeaverAware;
import org.springframework.core.io.ResourceLoader;
import org.springframework.instrument.classloading.LoadTimeWeaver;
import org.springframework.jmx.export.notification.NotificationPublisher;
import org.springframework.jmx.export.notification.NotificationPublisherAware;
public class DemoBean implements ApplicationContextAware,
ApplicationEventPublisherAware, BeanClassLoaderAware, BeanFactoryAware,
BeanNameAware, LoadTimeWeaverAware, MessageSourceAware,
NotificationPublisherAware, ResourceLoaderAware
{
@Override
public void setResourceLoader(ResourceLoader arg0) {
// TODO Auto-generated method stub
}
@Override
public void setNotificationPublisher(NotificationPublisher arg0) {
// TODO Auto-generated method stub
}
@Override
public void setMessageSource(MessageSource arg0) {
// TODO Auto-generated method stub
}
@Override
public void setLoadTimeWeaver(LoadTimeWeaver arg0) {
// TODO Auto-generated method stub
}
@Override
public void setBeanName(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void setBeanFactory(BeanFactory arg0) throws BeansException {
// TODO Auto-generated method stub
}
@Override
public void setBeanClassLoader(ClassLoader arg0) {
// TODO Auto-generated method stub
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher arg0) {
// TODO Auto-generated method stub
}
@Override
public void setApplicationContext(ApplicationContext arg0)
throws BeansException {
// TODO Auto-generated method stub
}
}
自定义init()和destroy()方法
bean配置文件中的默认init和destroy方法有两种定义方法:
- 适用于单个
Bean的Bean本地定义 - 全局定义适用于在
bean上下文中定义的所有bean
Bean本地定义
本地定义如下:
<beans>
<bean id="demoBean" class="com.howtodoinjava.task.DemoBean"
init-method="customInit"
destroy-method="customDestroy"></bean>
</beans>
全局定义
全局定义如下,这些方法将为<beans>标记下给出的所有bean定义调用。当你有一种配置可以为所有bean定义通用方法名称(如init()和destroy())时,这很实用。可帮助你不用为所有bean单独提及init和destroy方法。
<beans default-init-method="customInit" default-destroy-method="customDestroy">
<bean id="demoBean" class="com.howtodoinjava.task.DemoBean"></bean>
</beans>
Java程序代码示例:
package cn.howtodoinjava.task;
public class DemoBean
{
public void customInit()
{
System.out.println("Method customInit() invoked...");
}
public void customDestroy()
{
System.out.println("Method customDestroy() invoked...");
}
}
@PostConstruct和@PreDestroy 注解
从Spring 2.5开始,你还可以使用注解通过@PostConstruct和@PreDestroy注解指定生命周期方法。
@PostConstruct注解的方法将在使用默认构造函数构造bean之后调用,并在它的实例返回给请求对象之前调用。@PreDestroy注解方法在bean即将在bean容器中销毁之前被调用。
Java代码示例如下:
package cn.howtodoinjava.task;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class DemoBean
{
@PostConstruct
public void customInit()
{
System.out.println("Method customInit() invoked...");
}
@PreDestroy
public void customDestroy()
{
System.out.println("Method customDestroy() invoked...");
}
}
综上,这一切都与Spring容器内部的Spring bean生命周期有关。记住给定的生命周期事件类型,这是Spring面试中经常问到的问题。
🙂🙂🙂关注微信公众号java干货 不定期分享干货资料


