Bean 销毁的方式与顺序

2021-01-26

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\n");
	}

	@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\n");
	}

	public void beanDestroy() {
		this.id = 3;
		this.name = "cx3";
		System.out.println(this);
		System.out.println(source + ": User#beanDestroy executed\n");
	}

	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;
	}

}

 

打印结果

ConstXiong 备案号:苏ICP备16009629号-3