Spring容器中Bean的生命周期

一、Spring装配Bean的过程

  1. 实例化;
  2. 设置属性值;
  3. 如果实现了BeanNameAware接口,调用setBeanName设置Bean的ID或者Name;
  4. 如果实现BeanFactoryAware接口,调用setBeanFactory 设置BeanFactory;
  5. 如果实现ApplicationContextAware,调用setApplicationContext设置ApplicationContext
  6. 调用BeanPostProcessor的预先初始化方法;
  7. 调用InitializingBean的afterPropertiesSet()方法;
  8. 调用定制init-method方法;
  9. 调用BeanPostProcessor的后初始化方法;

Spring容器关闭过程
1. 调用DisposableBean的destroy();
2. 调用定制的destroy-method方法;

二、验证

创建LifeCycleBean 类,代码如下


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class LifeCycleBean implements InitializingBean, 
        DisposableBean,BeanNameAware,BeanFactoryAware, ApplicationContextAware{
        private String str = "default";
        public LifeCycleBean() {
            System.out.println("----------------------------------------------------------");
            System.out.println("construct LifecycleBean ***************");
        }
        public LifeCycleBean(String str) {
            this.str = str;
        }
        public String getStr() {
            return str;
        }
        public void setStr(String str) {
            System.out.println("setStr ***************");
            this.str = str;
        }
        public void init() {
            System.out.println("init mtd ***************");
        }
        public void cleanup() {
            System.out.println("cleanup mtd ***************");
        }
        public void afterPropertiesSet() throws Exception {
            System.out.println("afterPropertiesSet ***************");
        }
        public void destroy() throws Exception {
            System.out.println("destroy ***************");
        }
        public void setApplicationContext(ApplicationContext arg0) throws BeansException {
            System.out.println("setApplicationContext***************");
        }
        public void setBeanFactory(BeanFactory arg0) throws BeansException {
            System.out.println("setBeanFactory***************");
        }
        public void setBeanName(String arg0) {
            System.out.println("setBeanName***************" + arg0);            
        }
}

创建配置文件(init.xml),代码如下

    <bean name="lifeCycleBean" class="org.ifly.edu.spring.bean.init.LifeCycleBean" 
         init-method="init" destroy-method="cleanup">  
         <property name="str" value="hello"/>  
    </bean> 
    public static void main(String[] args) {

        AbstractApplicationContext context = new ClassPathXmlApplicationContext(
                "org/ifly/edu/spring/bean/init/init.xml");
        LifeCycleBean bean1 = (LifeCycleBean)context.getBean("lifeCycleBean",LifeCycleBean.class); 
        System.out.println("***********" + bean1 +  "   " + bean1.getStr());  
        context.registerShutdownHook();
    }

运行结果:
construct LifecycleBean *****
setStr *****
setBeanName***************lifeCycleBean
setBeanFactory***************
setApplicationContext***************
**** MyBeanPostProcessor postProcessBeforeInitialization Bean ‘lifeCycleBean
afterPropertiesSet *****
init mtd *****
**** MyBeanPostProcessor postProcessAfterInitialization Bean ‘lifeCycleBean
***********org.ifly.edu.spring.bean.init.LifeCycleBean@11dba45 hello
destroy *****
cleanup mtd *****
DisposableBean interface….123

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值