《Spring6》第14节:Bean的生命周期接口之InitializingBean和DisposableBean介绍
前面10几个小节的内容,已经将Spring中的依赖注入、自动装配、Bean标签相关属性都已经介绍完了,这里我们就继续学习Spring生命周期相关的内容。在之前的文章里面,虽然也有提到生命周期这个概念,但是都是简单的一笔带过,并没有深入的介绍,从这篇文章开始,我将专门来介绍一下Bean的生命周期中涉及到的常见接口。这一小节先介绍InitializingBean和DisposableBean两个接口。
1.1、InitializingBean接口
InitializingBean接口会在Bean的生命周期过程中回调的一个接口,在Bean对象完成属性赋值之后,就会立即调用这个InitializingBean接口中的afterPropertiesSet()方法。
afterPropertiesSet()方法的作用:从方法名称就可以看出来,它是在属性赋值之后回调的方法,作用和之前介绍的init-method初始化方法是一致的,可以说,它就是初始化方法,只不过是通过实现接口的方式来定义初始化方法。
注意:如果我们需要自定义一些初始化逻辑,那么可以实现InitializingBean接口,重写afterPropertiesSet()方法即可。
Spring中有三种定义初始化方法的方式,分别如下所示:
- 第一种:使用@PostContruct注解。
- 第二种:实现InitializingBean接口,重写afterPropertiesSet()方法。
- 第三种:使用init-method属性指定初始化方法。
上面三种定义初始化方法的方式中,如果一个Bean对象都采用了这三种方式,那么Spring会按照优先级依次调用初始化方法。
初始化方法的调用顺序,优先级:@PostContruct注解===>InitializingBean接口===>init-method属性。
下面就来看看Bean对象实现了InitializingBean接口的代码。
- 案例代码
package com.spring.study;
import org.springframework.beans.factory.InitializingBean;
public class AService implements InitializingBean {
public AService() {
System.out.println("AService 实例化");
}
private String name;
public void setName(String name) {
this.name = name;
System.out.println("AService name 属性赋值");
}
/**
* init-method 定义的方法
*/
public void initMethod() {
System.out.println("AService 调用 init-method 属性定义的 initMethod() 初始化方法");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("AService 调用 InitializingBean.afterPropertiesSet() 初始化方法");
}
}
- 添加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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
定义bean
-->
<bean id="aService" class="com.spring.study.AService" init-method="initMethod">
<property name="name" value="ADemo"/>
</bean>
</beans>
执行结果如下所示:
1.2、DisposableBean接口
DisposableBean接口的作用是:指定Bean对象的销毁方法,当IOC容器关闭之前(可以调用close()方法关闭IOC容器),Spring会调用Bean对象的销毁方法。和初始化方法一样,Spring也提供了三种定义销毁方法的方式,如下所示:
- 第一种:使用@PreDestroy注解。
- 第二种:实现DisposableBean接口,重写destroy()方法。
- 第三种:使用destroy-method属性指定销毁方法。
上面三种定义销毁方法的方式中,如果一个Bean对象都采用了这三种方式,那么Spring会按照优先级依次调用销毁方法。
初始化方法的调用顺序,优先级:@PreDestroy注解===>DisposableBean接口===>destroy-method属性。
下面就来看看Bean对象实现了DisposableBean接口的代码。
- 在实现InitializingBean接口的基础之上,我们继续实现DisposableBean接口,完整代码如下。
package com.spring.study;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class AService implements InitializingBean, DisposableBean {
public AService() {
System.out.println("AService 实例化");
}
private String name;
public void setName(String name) {
this.name = name;
System.out.println("AService name 属性赋值");
}
/**
* init-method 定义的方法
*/
public void initMethod() {
System.out.println("AService 调用 init-method 属性定义的 initMethod() 初始化方法");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("AService 调用 InitializingBean.afterPropertiesSet() 初始化方法");
}
@Override
public void destroy() throws Exception {
System.out.println("AService 调用 DisposableBean.destroy() 销毁方法");
}
public void destroyMethod() {
System.out.println("AService 调用 destroy-method 属性定义的 destroyMethod() 销毁方法");
}
}
- 在XMLL配置文件里面,给<bean>标签添加destroy-method属性。
<bean id="aService" class="com.spring.study.AService"
init-method="initMethod"
destroy-method="destroyMethod">
<property name="name" value="ADemo"/>
</bean>
- 执行测试代码,最终的输出结果如下所示:
1.3、源代码获取
源代码地址:
https://gitcode.com/knowledge-base/spring-study/tree/spring6-chapter-14
到此,Spring生命周期中InitializingBean和DisposableBean两个接口就介绍完了,我们需要掌握两个接口的作用以及对应方法是在哪个阶段被回调的,其中DisposableBean接口不常用,我在开发过程中,基本上没有使用过。
今天就到这里,未完待续~~