当前位置: 代码迷 >> Web前端 >> Spring Bean 初始化进程
  详细解决方案

Spring Bean 初始化进程

热度:496   发布时间:2012-07-08 17:43:43.0
Spring Bean 初始化过程
Life类
Java代码 复制代码?收藏代码
  1. package?com.open.bean; ??
  2. import?org.springframework.beans.BeansException; ??
  3. import?org.springframework.beans.factory.BeanFactory; ??
  4. import?org.springframework.beans.factory.BeanFactoryAware; ??
  5. import?org.springframework.beans.factory.BeanNameAware; ??
  6. import?org.springframework.beans.factory.DisposableBean; ??
  7. import?org.springframework.beans.factory.InitializingBean; ??
  8. public?class?Life?implements?BeanFactoryAware,?BeanNameAware, ??
  9. ????????InitializingBean,?DisposableBean?{ ??
  10. ????private?String?msg; ??
  11. ????public?Life()?{ ??
  12. ????????System.out.println("msg="+msg); ??
  13. ????????System.out.println("构造函数"); ??
  14. ????} ??
  15. ????public?void?setBeanFactory(BeanFactory?beanFactory)?throws?BeansException?{ ??
  16. ????????System.out.println("setBeanFactory"); ??
  17. ????} ??
  18. ????public?void?setBeanName(String?name)?{ ??
  19. ????????System.out.println("setBeanName"); ??
  20. ????} ??
  21. ????public?void?init()?{ ??
  22. ????????System.out.println("初始化"); ??
  23. ????} ??
  24. ????public?Object?postProcessBeforeInitialization(Object?bean,?String?beanName) ??
  25. ????????????throws?BeansException?{ ??
  26. ????????System.out.println("postProcessBeforeInitialization"); ??
  27. ????????return?null; ??
  28. ????} ??
  29. ????public?Object?postProcessAfterInitialization(Object?bean,?String?beanName) ??
  30. ????????????throws?BeansException?{ ??
  31. ????????System.out.println("postProcessAfterInitialization"); ??
  32. ????????return?null; ??
  33. ????} ??
  34. ????public?void?afterPropertiesSet()?throws?Exception?{ ??
  35. ????????System.out.println("afterPropertiesSet"); ??
  36. ????} ??
  37. ????public?void?destroy()?throws?Exception?{ ??
  38. ????????System.out.println("destroy"); ??
  39. ????} ??
  40. ????public?String?getMsg()?{ ??
  41. ????????return?msg; ??
  42. ????} ??
  43. ????public?void?setMsg(String?msg)?{ ??
  44. ????????this.msg?=?msg; ??
  45. ????} ??
  46. }??
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;
    }
}

?

?

BeanPostProcessorImp类

Java代码 复制代码?收藏代码
  1. package?com.open.bean; ??
  2. import?org.springframework.beans.BeansException; ??
  3. import?org.springframework.beans.factory.config.BeanPostProcessor; ??
  4. public?class?BeanPostProcessorImp?implements?BeanPostProcessor?{ ??
  5. ????public?Object?postProcessBeforeInitialization(Object?bean,?String?beanName) ??
  6. ????????????throws?BeansException?{ ??
  7. ????????System.out.println("postProcessBeforeInitialization"); ??
  8. ????????return?bean; ??
  9. ????} ??
  10. ????public?Object?postProcessAfterInitialization(Object?bean,?String?beanName) ??
  11. ????????????throws?BeansException?{ ??
  12. ????????System.out.println("postProcessAfterInitialization"); ??
  13. ????????return?bean; ??
  14. ????} ??
  15. }??
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;
    }
}

?

?

BeanCounter类

Java代码 复制代码?收藏代码
  1. package?com.open.bean; ??
  2. import?org.springframework.beans.BeansException; ??
  3. import?org.springframework.beans.factory.config.BeanFactoryPostProcessor; ??
  4. import?org.springframework.beans.factory.config.ConfigurableListableBeanFactory; ??
  5. public?class?BeanCounter?implements?BeanFactoryPostProcessor?{ ??
  6. ????public?void?postProcessBeanFactory( ??
  7. ????????????ConfigurableListableBeanFactory?beanFactory)?throws?BeansException?{ ??
  8. ????????System.out.println("类的数量="+beanFactory.getBeanDefinitionCount()); ??
  9. ????} ??
  10. }??
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());
    }
}

??

?

?

?

bean.xml

Java代码 复制代码?收藏代码
  1. <?xml?version="1.0"?encoding="UTF-8"?> ??
  2. <!DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"??
  3. ????"http://www.springframework.org/dtd/spring-beans.dtd"> ??
  4. <beans> ??
  5. ????<bean?id="life"?name="life_name"?class="com.open.bean.Life"? ??
  6. ????????init-method="init"> ??
  7. ????????<property?name="msg"?value="lifexxxx"/> ??
  8. ????</bean>???? ??
  9. ????<bean?id="processor"?class="com.open.bean.BeanPostProcessorImp"/> ??
  10. ????<bean?id="counter"?class="com.open.bean.BeanCounter"/> ??
  11. </beans>??
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="life" name="life_name" class="com.open.bean.Life" 
        init-method="init">
        <property name="msg" value="lifexxxx"/>
    </bean>    
    <bean id="processor" class="com.open.bean.BeanPostProcessorImp"/>
    <bean id="counter" class="com.open.bean.BeanCounter"/>
</beans>

?

?

?

测试类

?

Java代码 复制代码?收藏代码
  1. package?com.open.bean; ??
  2. import?org.springframework.context.support.ClassPathXmlApplicationContext; ??
  3. public?class?Test?{ ??
  4. ????public?static?void?main(String[]?args)?{ ??
  5. ????????ClassPathXmlApplicationContext?cx= ??
  6. ????????????new?ClassPathXmlApplicationContext("bean.xml"); ??
  7. ?????????Life?life=(Life)cx.getBean("life");????? ??
  8. ????} ??
  9. }??????
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");     
    }
}    

?

?

输出结果

?

类的数量=3
msg
=null
构造函数
setBeanName
setBeanFactory
postProcessBeforeInitialization
afterPropertiesSet
初始化
postProcessAfterInitialization

  相关解决方案