Spring中Bean初始化及销毁方法(InitializingBean接口、DisposableBean接口、@PostConstruct注解、@PreDestroy注解、以及init-method

一、
在Spring中Bean的初始化时以及销毁时的回调方式有:

  • init-method:是指创建bean时调用的方法,注意,不是创建bean的方法。

  • destroy-method:是指销毁bean时调用的方法,同样,不是销毁bean的方法。

  • @PostConstruct注解:在bean实例化和注入后,进行初始化

  • @PreDestroy:在bean销毁时回调

  • InitializingBean接口:

查看InitializingBean接口的源码可以发现,只有一个方法afterPropertiesSet,允许一个bean在它的所有必须属性被BeanFactory设置后,来执行初始化的工作
public interface InitializingBean {
	void afterPropertiesSet() throws Exception;
}
  • DisposableBean接口:
查看DisposableBean接口发现只有一个方法destroy,允许在容器销毁该bean的时候获得一次回调
public interface DisposableBean {
	void destroy() throws Exception;
}

二、案例
Pojo类:
需要pojo继承InitializingBean接口并实现afterPropertiesSet方法来在bean实例化和注入后初始化
需要pojo继承DisposableBean接口并实现destroy方法,在bean被销毁前进行回调

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class StudentA implements InitializingBean,DisposableBean{
	private String name;
	private String stuId;
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
		System.out.println("设值注入Name:"+name);
	}
	
 	public String getStuId() {
		return stuId;
	}
 	
	public void setStuId(String stuId) {
		this.stuId = stuId;
		System.out.println("设值注入stuId:"+stuId);
	}
	
	public StudentA(String name, String stuId) {
		super();
		this.name = name;
		this.stuId = stuId;
	}
	
	public StudentA() {
		super();
		// TODO Auto-generated constructor stub
		System.out.println("通过无参构造...StudentA实例化");
	}
	
	@Override
	public String toString() {
		return "StudentA [name=" + name + ", stuId=" + stuId + "]";
	}
	
	/**
	 * 重写DisposableBean接口里的方法
	 * 在销毁时回调
	 */
	@Override
	public void destroy() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("DisposableBean接口的destroy()");
	}
	
	/**
	 * 重写InitializingBean接口里的方法
	 * 在实例化并设值注入后初始化回调
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("InitializingBean接口的afterPropertiesSet()");
	}
	
	/**
	 * 自定义初始化时回调
	 * 对应着bean里的init-method="start"
	 */
	public void start() {
		// TODO Auto-generated method stub
		System.err.println("自定义初始化回调...init-method=\"start\"");
	}
	
	/**
	 * 自定义销毁时回调
	 * 对应着bean里的destroy-method="end"
	 */
	public void end() {
		// TODO Auto-generated method stub
		System.out.println("自定义销毁回调...destroy-method=\"end\"");
	}
	
	/**
	 * 通过@PreDestroy注解
	 * 初始化后回调方法
	 */
	@PostConstruct
	public void postConstruct(){
		System.out.println("初始化后回调方法...@postConstruct注解");
	}
	
	
	/**
	 * 通过@PreDestroy注解
	 * 销毁时回调方法
	 */
	@PreDestroy
	public  void preDestory(){
		System.out.println("销毁前回调方法...@preDestory注解");
	}
	
}

applicationContext.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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
		
		
		<!-- 如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor -->
		<context:annotation-config/>
		
		<!-- 注册studentA对象 -->
		<bean class="com.pojo.StudentA" id="studentA" init-method="start" destroy-method="end">
			<!-- 设值注入 -->
			<property name="name" value="zsl"/>
			<property name="stuId" value="zsl33"/>
		</bean>
</beans>

测试:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.pojo.StudentA;

public class Test {
	@SuppressWarnings("resource")//去警告,问题不大不要慌
	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext = 
				new ClassPathXmlApplicationContext("applicationContext.xml");
		
		StudentA bean = (StudentA) applicationContext.getBean("studentA");
		System.out.println(bean);
		
		/**
		 * 在非web环境下需要手动关闭IOC容器,则需调用registerShutdownHook()方法
		 * 而web环境下已有相应的配置进行关闭IOC容器
		 */
		applicationContext.registerShutdownHook();
	}
}

结果:

三、执行顺序:
通过上面的案例和结果可以看出它们之间的执行顺序:

初始化时:
构造函数Construct ->属性注入 ->@PostConstruct ->InitializingBean接口 ->bean的init-method自定义的方法 
销毁时:@PreDestroy ->DisposableBean接口 ->bean的destoryMethod自定义的方法
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

偷偷学习被我发现

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值