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方法后然后再销毁
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
Spring @DateTimeFormat日期格式化时注解场景分析
这篇文章主要介绍了Spring @DateTimeFormat日期格式化时注解场景分析,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-05-05Java开发中POJO和JSON互转时如何忽略隐藏字段的问题
这篇文章主要介绍了Java开发中POJO和JSON互转时如何忽略隐藏字段的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2021-02-02基于params、@PathVariabl和@RequestParam的用法与区别说明
这篇文章主要介绍了方法参数相关属性params、@PathVariabl和@RequestParam用法与区别,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-08-08
最新评论