bean实例化的两种方法
指定初始化方法
spring bean 的程序执行流程:
1、构造方法
2、setter方法
3、调用写在xml文件里的init方法。init-method属性指定
实现InitializingBean接口
该接口只有一个afterPropertiesSet方法,重写后实现与指定初始化方法相同的效果,但是由于会将代码与spring耦合起来 具有侵入性。
不推荐使用该方法。
bean在web应用时可以恰当的关闭Spring但是非web应用环境下需手动关闭。
调用AbstractApplicationContext的registerShutDownHook方法。
程序将会:
1、调用bean的销毁方法
2、关闭spring applicationContext容器
3、退出jvm
bean销毁的两种方法
1、destroy-method属性 与初始化类似
2、实现DisposableBean接口
当两个bean相互依赖时,如果一个的作用域是singleton另一个是prototype,那么会出问题
此时需要用到方法注入<lookup-method>属性