Spring中的InitializingBean与DisposableBean
InitializingBean
顾名思义,应该是初始化Bean
相关的接口。
先看一下该接口都定义了哪些方法:
public interface InitializingBean { void afterPropertiesSet() throws Exception; }
看方法名,应该是在读完Properties
文件,之后执行的方法,不是很了解,先写个bean
测试一下。
- 首先声明一个
Bean
package com.github.jettyrun.springinterface.demo.initializingbean; import org.springframework.beans.factory.InitializingBean; /** * Created by jetty on 18/1/31. */ public class InitBean implements InitializingBean{ public void afterPropertiesSet() throws Exception { System.out.println("init-afterPropertiesSet()"); } public void test(){ System.out.println("init-test()"); } }
- 然后用
Spring
管理,初始化一个Bean
。
<bean id="initBean" class="com.github.jettyrun.springinterface.demo.initializingbean.InitBean"></bean>
- 加载一下上下文
public static void main(String[] args) { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:application-usertag.xml"); }
- 运行结果
init-afterPropertiesSet()
我们能看到,在spring在初始化initBean
的时候,调用了afterPropertiesSet
方法,也就是说 spring
在初始化实现了InitializingBean
接口的bean
的时候,会执行afterPropertiesSet()
方法。
我们也知道,在spring
初始化bean
的时候,可以配置bean
的init-method
属性,在initBean
上添加一个试试。
配置如下:
<bean id="initBean" init-method="test" class="com.github.jettyrun.springinterface.demo.initializingbean.InitBean"></bean>
加载一下上下文得到运行结果:
init-afterPropertiesSet()
init-test()
我们看到,在初始化bean的过程中,先调用了 afterPropertiesSet()
方法,然后执行init-method
定义的方法。
也就是说spring
为bean
提供了两种初始化的方式,第一种实现InitializingBean
接口,实现afterPropertiesSet
方法,第二种配置文件中通过init-method
指定,两种方式可以同时使用,同时使用先调用afterPropertiesSet
方法,后执行init-method
指定的方法。
那DisposableBean
接口又是干嘛的,看一下接口定义:
public interface DisposableBean { void destroy() throws Exception; }
只定义了一个方法,destroy,看名字应该是对象在销毁时执行的,给上面的InitBean
也实现一下DisposableBean
接口,代码如下:
public class InitBean implements InitializingBean,DisposableBean { public void destroy() throws Exception { System.out.println("destroy"); } public void afterPropertiesSet() throws Exception { System.out.println("init-afterPropertiesSet()"); } public void test(){ System.out.println("init-test()"); }
再运行一下main方法:
public class Main { public static void main(String[] args) { AbstractApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:application-usertag.xml"); // InitBean initBean=(InitBean)applicationContext.getBean("initBean"); System.out.println("init-success"); applicationContext.registerShutdownHook(); } }
执行结果:
init-afterPropertiesSet() init-test() init-success destroy
也就是说,在对象销毁的时候,会去调用DisposableBean
的destroy
方法。
同样,在对象销毁有一个参数配置destroy-method
,和init-method
相同,在调用销毁的时候,先执行 DisposableBean
的destroy
方法,后执行 destroy-method
声明的方法。
https://blog.csdn.net/xiaoxufox/article/details/80420739
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了