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
最新文章
- 顶点缓存对象(VBO)
- 头像上传功能实现,PC端的需要做兼容
- cocos2dx之lua派生类和方法重新
- codeforces A. New Year Candles 解题报告
- Moo University - Financial Aid
- HDU 5092
- myeclipse 清理项目缓存的几大步骤
- 如何使用CSL(翻译总结自TI官方文档)
- C#基础练习(时间的三连击)
- MySQL &#183; 特性分析 &#183; innodb 锁分裂继承与迁移
- poj2255
- 2016";百度之星"; - 资格赛(Astar Round1) 1001
- hibernate 基础
- Android 开发 实现文本搜索功能
- Jmeter自带录制功能
- C# 制作向导
- PHP爬取历史天气
- sap 软件架构
- 125 open source Big Data architecture papers for data professionals
- RDLC报表显示存储于数据库的图片
热门文章
- OpenYurt 与 FabEdge 集成验证——云边数据面通信初试
- fabric运行错误汇总
- Springboot+Mybatisplus替换mybatis整合报错Mapped Statements collection does not contain value
- HCNP Routing&;Switching之BGP路由过滤和AS-Path-Filter
- SpringCloud 2020.0.4 系列之 Stream 消息广播 与 消息分组 的实现
- [luogu7092]计数题
- airTest小程序自动化踩坑记(android设备)
- Java ArrayList小记
- MySQL数据库从入门到放弃(目录)
- LG 11 月 月赛 II T4