Spring学习之bean的生命周期

实例化(当我们的程序加载beans.xml文件),把我们的bean(前提是scope=singleton)实例化到内存

调用set方法设置属性

如果你实现了bean名字关注接口(BeanNameAware) 则,可以通过setBeanName获取id号

如果你实现了 bean工厂关注接口,(BeanFactoryAware),则可以获取BeanFactory

如果你实现了 ApplicationContextAware接口,则调用方法
//该方法传递ApplicationContext
public void setApplicationContext(ApplicationContext arg0)throws BeansException {
    // TODO Auto-generated method stub
    System.out.println("setApplicationContext"+arg0);
}

如果bean 和 一个后置处理器关联,则会自动去调用 Object postProcessBeforeInitialization方法

如果你实现InitializingBean 接口,则会调用 afterPropertiesSet

如果自己在<bean init-method=”init” /> 则可以在bean定义自己的初始化方法.

如果bean 和 一个后置处理器关联,则会自动去调用 Object postProcessAfterInitialization方法

使用我们的bean

容器关闭

可以通过实现DisposableBean 接口来调用方法 destory

可以在<bean destory-method=”fun1”/> 调用定制的销毁方法

猜你喜欢

转载自chenzheng8975.iteye.com/blog/1686081