Spring之InitializingBean接口和DisposableBean接口的使用

 更新时间:2024年01月13日 10:36:23   作者:波波烤鸭  
这篇文章主要介绍了Spring之InitializingBean接口和DisposableBean接口的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

1.InitializingBean

该接口的作用是:

允许一个bean在它的所有必须属性被BeanFactory设置后,来执行初始化的工作,该接口中只有一个方法,afterPropertiesSet

public interface InitializingBean {

	/**
	 * Invoked by the containing {@code BeanFactory} after it has set all bean properties
	 * and satisfied {@link BeanFactoryAware}, {@code ApplicationContextAware} etc.
	 * <p>This method allows the bean instance to perform validation of its overall
	 * configuration and final initialization when all bean properties have been set.
	 * @throws Exception in the event of misconfiguration (such as failure to set an
	 * essential property) or if initialization fails for any other reason
	 */
	void afterPropertiesSet() throws Exception;

}

2.DisposableBean

该接口的作用是:允许在容器销毁该bean的时候获得一次回调。

DisposableBean接口也只规定了一个方法:destroy

public interface DisposableBean {

	/**
	 * Invoked by the containing {@code BeanFactory} on destruction of a bean.
	 * @throws Exception in case of shutdown errors. Exceptions will get logged
	 * but not rethrown to allow other beans to release their resources as well.
	 */
	void destroy() throws Exception;

}

3.案例演示

/**
 * 实现InitializingBean和DisposableBean接口
 * @author dengp
 *
 */
public class User implements InitializingBean,DisposableBean{

	private int id;
	
	private String name;
	
	private String beanName;
	
	public User(){
		System.out.println("User 被实例化");
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		System.out.println("设置:"+name);
		this.name = name;
	}

	public String getBeanName() {
		return beanName;
	}

	public void setBeanName(String beanName) {
		this.beanName = beanName;
	}
	
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", beanName=" + beanName + "]";
	}

	@Override
	public void destroy() throws Exception {
		// TODO Auto-generated method stub
		System.out.println("destory ....");
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("afterPropertiesSet....");
	}
}

配置文件

<?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 
 http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean class="com.dpb.pojo.User" id="user" >
		<property name="name" value="波波烤鸭"></property>
	</bean>
	
</beans>

测试代码

@Test
public void test1() {
	ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
	User user = ac.getBean(User.class);
	System.out.println(user);
	ac.registerShutdownHook();
}

输出结果:

User 被实例化
设置:波波烤鸭
afterPropertiesSet....
User [id=0, name=波波烤鸭, beanName=null]
destory ....

通过输出能够显示spring初始化bean的时候,如果bean实现了InitializingBean接口,会自动调用afterPropertiesSet方法,在bean被销毁的时候如果实现了DisposableBean接口会自动回调destroy方法后然后再销毁

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • RocketMQ事务消息原理与使用详解

    RocketMQ事务消息原理与使用详解

    RocketMQ事务消息(Transactional Message)是指应用本地事务和发送消息操作可以被定义到全局事务中,要么同时成功,要么同时失败。RocketMQ的事务消息提供类似 X/Open XA 的分布式事务功能,通过事务消息能达到分布式事务的最终一致
    2023-02-02
  • 详解Java Fibonacci Search斐波那契搜索算法代码实现

    详解Java Fibonacci Search斐波那契搜索算法代码实现

    这篇文章主要介绍了详解Java Fibonacci Search斐波那契搜索算法代码实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • Java检查字符串是否一致的四种方法

    Java检查字符串是否一致的四种方法

    字符串比较是常见的操作,包括比较相等、比较大小、比较前缀和后缀串等,在 Java 中,比较字符串的常用方法有四个:equals(),equalsIgnoreCase(),compareTo()和compareToIgnoreCase(),下面详细介绍这四个方法的使用
    2024-04-04
  • SpringBoot原生组件注入实现两种方式介绍

    SpringBoot原生组件注入实现两种方式介绍

    SpringBoot是Spring全家桶的成员之一,基于约定优于配置的思想(即有约定默认值,在不配置的情况下会使用默认值,在配置文件下配置的话会使用配置的值)。SpringBoot是一种整合Spring技术栈的方式(或者说是框架),同时也是简化Spring的一种快速开发的脚手架
    2022-10-10
  • MyBatis完成CRUD 详细细节内容剖析

    MyBatis完成CRUD 详细细节内容剖析

    这篇文章主要介绍了MyBatis完成CRUD 详细细节内容剖析,本文通过图文示例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧
    2024-05-05
  • maven中下载jar包源码和javadoc的命令介绍

    maven中下载jar包源码和javadoc的命令介绍

    这篇文章主要介绍了maven中下载jar包源码和javadoc的命令介绍,本文讲解了Maven命令下载源码和javadocs、通过配置文件添加、配置eclipse等内容,需要的朋友可以参考下
    2015-03-03
  • SpringBoot3通过GraalVM生成exe执行文件问题

    SpringBoot3通过GraalVM生成exe执行文件问题

    文章介绍了如何安装GraalVM和Visual Studio,并通过Spring Boot项目将Java应用程序封装成可执行文件(.exe)
    2024-12-12
  • Spring声明式事务管理从原理到实战示例

    Spring声明式事务管理从原理到实战示例

    本文主要介绍了Spring声明式事务管理的概念、实现原理、配置方法、异常回滚机制、不同类的事务配置以及响应式事务的支持,声明式事务通过配置或注解简化了事务管理,使用AOP实现,支持多种传播行为和隔离级别,感兴趣的朋友跟随小编一起看看吧
    2025-11-11
  • Automapper实现自动映射的实例代码

    Automapper实现自动映射的实例代码

    这篇文章主要介绍了Automapper实现自动映射的实例代码,需要的朋友可以参考下
    2017-09-09
  • Java创建线程及配合使用Lambda方式

    Java创建线程及配合使用Lambda方式

    这篇文章主要介绍了Java创建线程及配合使用Lambda方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08

最新评论