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的时候,可以配置beaninit-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定义的方法。

也就是说springbean提供了两种初始化的方式,第一种实现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

也就是说,在对象销毁的时候,会去调用DisposableBeandestroy方法。

同样,在对象销毁有一个参数配置destroy-method,和init-method相同,在调用销毁的时候,先执行 DisposableBeandestroy方法,后执行 destroy-method声明的方法。

代码地址:https://gitee.com/shxz130/SpringInterfaceDemo

https://blog.csdn.net/xiaoxufox/article/details/80420739

最新文章

  1. 顶点缓存对象(VBO)
  2. 头像上传功能实现,PC端的需要做兼容
  3. cocos2dx之lua派生类和方法重新
  4. codeforces A. New Year Candles 解题报告
  5. Moo University - Financial Aid
  6. HDU 5092
  7. myeclipse 清理项目缓存的几大步骤
  8. 如何使用CSL(翻译总结自TI官方文档)
  9. C#基础练习(时间的三连击)
  10. MySQL &#183; 特性分析 &#183; innodb 锁分裂继承与迁移
  11. poj2255
  12. 2016&quot;百度之星&quot; - 资格赛(Astar Round1) 1001
  13. hibernate 基础
  14. Android 开发 实现文本搜索功能
  15. Jmeter自带录制功能
  16. C# 制作向导
  17. PHP爬取历史天气
  18. sap 软件架构
  19. 125 open source Big Data architecture papers for data professionals
  20. RDLC报表显示存储于数据库的图片

热门文章

  1. OpenYurt 与 FabEdge 集成验证——云边数据面通信初试
  2. fabric运行错误汇总
  3. Springboot+Mybatisplus替换mybatis整合报错Mapped Statements collection does not contain value
  4. HCNP Routing&amp;Switching之BGP路由过滤和AS-Path-Filter
  5. SpringCloud 2020.0.4 系列之 Stream 消息广播 与 消息分组 的实现
  6. [luogu7092]计数题
  7. airTest小程序自动化踩坑记(android设备)
  8. Java ArrayList小记
  9. MySQL数据库从入门到放弃(目录)
  10. LG 11 月 月赛 II T4