Spring容器中Bean的生命周期

来源:互联网 发布:sql模糊查询通配符 编辑:程序博客网 时间:2024/04/29 23:37

一、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
原创粉丝点击