Bean 的销毁方式:
- @PreDestroy 注解作用于方法
- 实现 DisposableBean 接口,复写 destroy 方法
- 自定义 destroyMethod:xml <bean destroy-method="beanDestroy">属性、@Bean(destroyMethod = "beanDestroy")、BeanDefinitionBuilder#setDestroyMethodName
示例代码:
spring 配置文件
<?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">
<bean id="user1" class="constxiong.User" destroy-method="beanDestroy">
<property name="source" value="xml"/>
</bean>
<!-- 开启注解能力 -->
<context:component-scan base-package="constxiong"/>
</beans>
User 类
package constxiong;
import javax.annotation.PreDestroy;
import org.springframework.beans.factory.DisposableBean;
public class User implements DisposableBean {
private String source;
private Integer id;
private String name;
public User() {
}
@PreDestroy
public void preDestroy() {
this.id = 1;
this.name = "cx1";
System.out.println(this);
System.out.println(source + ": User#preDestroy executed
");
}
@Override
public void destroy() throws Exception {
this.id = 2;
this.name = "cx2";
System.out.println(this);
System.out.println(source + ": User implements DisposableBean#destroy executed
");
}
public void beanDestroy() {
this.id = 3;
this.name = "cx3";
System.out.println(this);
System.out.println(source + ": User#beanDestroy executed
");
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
@Override
public String toString() {
return "User{" +
"source='" + source + ''' +
", id=" + id +
", name='" + name + ''' +
'}';
}
}
测试类
package constxiong;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 测试 Bean 的销毁
* @author ConstXiong
*/
@Configuration
public class Test {
public static void main(String[] args) {
// xml
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("META-INF/spring-bean-destroy.xml");
User user1 = context.getBean("user1", User.class);
// @Bean
User user2 = context.getBean("user2", User.class);
//BeanDefinition
((DefaultListableBeanFactory)context.getBeanFactory()).registerBeanDefinition("user3",
BeanDefinitionBuilder.rootBeanDefinition(User.class).setDestroyMethodName("beanDestroy").addPropertyValue("source", "BeanDefinition").getBeanDefinition());
context.getBean("user3");
context.close();
}
@Bean(destroyMethod = "beanDestroy", name = "user2")
public User getUser() {
User user = new User();
user.setSource("@Bean");
return user;
}
}
打印结果
User{source='BeanDefinition', id=1, name='cx1'}
BeanDefinition: User#preDestroy executed
User{source='BeanDefinition', id=2, name='cx2'}
BeanDefinition: User implements DisposableBean#destroy executed
User{source='BeanDefinition', id=3, name='cx3'}
BeanDefinition: User#beanDestroy executed
User{source='@Bean', id=1, name='cx1'}
@Bean: User#preDestroy executed
User{source='@Bean', id=2, name='cx2'}
@Bean: User implements DisposableBean#destroy executed
User{source='@Bean', id=3, name='cx3'}
@Bean: User#beanDestroy executed
User{source='xml', id=1, name='cx1'}
xml: User#preDestroy executed
User{source='xml', id=2, name='cx2'}
xml: User implements DisposableBean#destroy executed
User{source='xml', id=3, name='cx3'}
xml: User#beanDestroy executed
销毁过程中,方法的执行顺序:
@PreDestroy -> DisposableBean#destroy -> destroyMethod
【Java学习资源】整理推荐
- Bean 的作用域
- maven 集成 tomcat 以 jar 的方式打包 web 应用
- Spring 依赖注入与依赖查找来源的区别
- Spring 依赖注入的处理过程与 DependencyDescriptor 的说明
- Spring 各种 Aware 接口回调注入
- Spring Bean 生命周期内的 Exception 复现
- Spring 内建 Bean
- Spring Bean 的别名
- Spring Bean 未指定名称的命名规则
- Bean 何时被 GC
- Spring Bean 延迟加载
- ObjectFactory 与 BeanFactory 的区别
- Bean 销毁的方式与顺序
- Bean 初始化的方式与顺序
- Bean 的实例化方式
- Bean的注册方式
- 什么是 BeanDefinition?
- Spring IoC 容器启动时做了什么?
- BeanFactory 与 FactoryBean 的区别
- BeanFactory 与 ApplicationContext 的区别
- Spring IoC 依赖注入的实现方式
- Spring IoC 依赖注入(支持哪些数据类型?)
- Spring bean 依赖查找
- Spring-IoC
- Spring 的核心特性