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的时候,可以配置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

posted @   edda_huang  阅读(472)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示