ITeye博客

spring中Bean的生命周期

gyht0808 · 2009-10-13 16:00 · Spring · Spring · Go · XML · Bean · Markdown

先贴代码: Life类 [code="java"]package com.open.bean; 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; public class Life implements BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean { private String msg; public Life() { System.out.println("msg="+msg); System.out.println("构造函数"); } public void setBeanFactory(BeanFactory beanFactory) throws BeansException { System.out.println("setBeanFactory"); } public void setBeanName(String name) { System.out.println("setBeanName"); } public void init() { System.out.println("初始化"); } public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessBeforeInitialization"); return null; } public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessAfterInitialization"); return null; } public void afterPropertiesSet() throws Exception { System.out.println("afterPropertiesSet"); } public void destroy() throws Exception { System.out.println("destroy"); } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }[/code] BeanPostProcessorImp类 [code="java"]package com.open.bean; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class BeanPostProcessorImp implements BeanPostProcessor { public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessBeforeInitialization"); return bean; } public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessAfterInitialization"); return bean; } }[/code] BeanCounter类 [code="java"]package com.open.bean; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; public class BeanCounter implements BeanFactoryPostProcessor { public void postProcessBeanFactory( ConfigurableListableBeanFactory beanFactory) throws BeansException { System.out.println("类的数量="+beanFactory.getBeanDefinitionCount()); } }[/code] bean.xml [code="xml"] [/code] 测试类 [code="java"]package com.open.bean; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ClassPathXmlApplicationContext cx= new ClassPathXmlApplicationContext("bean.xml"); Life life=(Life)cx.getBean("life"); } } [/code] 输出结果 [list] [*]类的数量=3 [*]msg=null [*]构造函数 [*]setBeanName [*]setBeanFactory [*]postProcessBeforeInitialization [*]afterPropertiesSet [*]初始化 [*]postProcessAfterInitialization [/list] 解释: [list] [*]调用BeanFactoryPostProcessor接口的postProcessBeanFactory方法 [*]-->实例化,调用构造函数-->设置属性值 [*]-->调用BeanNameAware的setBeanName()方法 [*]-->调用BeanFactoryAware接口的setBeanFactory [*]-->调用BeanPostProcessor接口的postProcessBeforeInitialization() [*]-->调用InitializingBean接口的afterPropertiesSet() [*]-->调用bean.xml中制定的init-method指定init()方法 [*]-->调用BeanPostProcessor接口的postProcessAfterInitialization() [*]容器关闭 [*]-->DisposableBean接口的destroy() [*]-->调用bean.xml中制定的destroy-method指定的go()方法 [/list]

评论(1)

  1. #1 楼 · supben · 2010-09-14 13:56
    adddd
    

评论功能维护中,当前仅展示历史评论。