Bean 销毁的方式与顺序

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

 

完整代码:009-spring-bean-destroy

 


【Java学习资源】整理推荐

 

 


【Java面试题与答案】整理推荐

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值