随笔都是学习笔记
随笔仅供参考,为避免笔记中可能出现的错误误导他人,请勿转载。

一、实现InitializingBean和DisposableBean接口中的方法可以观察Bean类的创建和销毁

 

 

 

xml中注入Bean:

 

 

 测试方法:

@Test
    public void fun6(){
        System.out.println("begin!");
        Person person = ioc.getBean("person", Person.class);
        System.out.println(person);
        ioc.close();
    }

 

 

 

运行结果:

 

 

 这里有两个初始化Person的原因是:

我创建了两个容器 ^o^

 

 二、基于配置的生命周期回调

初始化方法:init-method=""

销毁方法:destroy-method=""

在Person中创建:

xml配置:

 <!--生命周期回调-->
    <bean class="entity.Person" id="person" init-method="initByMy" destroy-method="destroyByMy"></bean>

 

结果:

 

 先调用接口实现的方法,在调用配置的方法。

 

posted on 2022-05-28 19:52  时间完全不够用啊  阅读(149)  评论(0编辑  收藏  举报