yuyee

          IOC監管Bean

          1.InitializingBean接口,在初始化Bean時容器會調用前者的afterPropertiesSet()方法

          2.DisposableBean接口,在析構Bean時容器會調用destroy()方法

          3.BeanFactoryAware接口,當它被BeanFactory創建后,它會擁有一個指向創建它的BeanFactory的引用

          4.BeanPostProcessor接口,這個接口兩個方法,postProcessBeforeInitialization(Object bean, String beanName)和postProcessAfterInitialization(Object bean, String beanName) 在其他Bean構造前后執行

          5.BeanFactoryPostProcessor接口,Spring IoC容器允許BeanFactoryPostProcessor在容器實際實例化任何其它的bean之前讀取配置元數據,并有可能修改它

          package com.google.springioctest;

          import org.springframework.beans.BeansException;
          import org.springframework.beans.factory.BeanFactory;
          import org.springframework.beans.factory.BeanFactoryAware;
          import org.springframework.beans.factory.DisposableBean;
          import org.springframework.beans.factory.InitializingBean;
          import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
          import org.springframework.beans.factory.config.BeanPostProcessor;
          import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

          public class A implements BeanPostProcessor, InitializingBean,
          BeanFactoryAware, BeanFactoryPostProcessor,DisposableBean {

          public A() {
          System.out.println("Class A");
          }
          private void init(){
          System.out.println("Class A init");
          }

          @Override
          public void afterPropertiesSet() throws Exception {
          // TODO Auto-generated method stub
          System.out.println("Class A afterPropertiesSet()");

          }

          @Override
          public void destroy() throws Exception {
          System.out.println("Class A destroy()");

          }

          @Override
          public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
          // TODO Auto-generated method stub
          System.out.println("Class A setBeanFactory()");
          }

          @Override
          public Object postProcessAfterInitialization(Object bean, String beanName)
          throws BeansException {
          System.out.println("Class A postProcessAfterInitialization");
          return null;
          }

          @Override
          public Object postProcessBeforeInitialization(Object bean, String beanName)
          throws BeansException {
          // TODO Auto-generated method stub
          System.out.println("Class A postProcessBeforeInitialization");
          return null;
          }


          @Override
          public void postProcessBeanFactory(
          ConfigurableListableBeanFactory beanFactory) throws BeansException {
          // TODO Auto-generated method stub
          System.out.println("Class A postProcessBeanFactory");
          }

          }
          執行結果:
          Class A
          Class A setBeanFactory()
          Class A afterPropertiesSet()
          Class A init
          Class A postProcessBeanFactory
          創建一個B類,由A來監管B類,B實現 InitializingBean,BeanFactoryAware,BeanFactoryPostProcessor
          package com.google.springioctest;

          import org.springframework.beans.BeansException;
          import org.springframework.beans.factory.BeanFactory;
          import org.springframework.beans.factory.BeanFactoryAware;
          import org.springframework.beans.factory.DisposableBean;
          import org.springframework.beans.factory.InitializingBean;
          import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
          import org.springframework.beans.factory.config.BeanPostProcessor;
          import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

          public class B implements InitializingBean, BeanFactoryAware,
          BeanFactoryPostProcessor {

          public B() {
          System.out.println("Class B");
          }

          public void init() {
          System.out.println("Class B init");
          }

          @Override
          public void afterPropertiesSet() throws Exception {
          System.out.println("Class B afterPropertiesSet");

          }

          @Override
          public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
          System.out.println("Class B beanFactory");

          }

          @Override
          public void postProcessBeanFactory(
          ConfigurableListableBeanFactory beanFactory) throws BeansException {
          // TODO Auto-generated method stub
          System.out.println("Class B postProcessBeanFactory");
          }

          }
          執行結果:
          Class A
          Class A setBeanFactory()
          Class A afterPropertiesSet()
          Class A init
          Class B
          Class B beanFactory
          Class B afterPropertiesSet
          Class B init
          Class A postProcessBeanFactory
          Class B postProcessBeanFactory

          可以看出A并沒有監管B類,也就是沒調用BeanPostProcessor這個接口的2個方法
          再來去掉B上的BeanFactoryPostProcessor接口
          package com.google.springioctest;

          import org.springframework.beans.BeansException;
          import org.springframework.beans.factory.BeanFactory;
          import org.springframework.beans.factory.BeanFactoryAware;
          import org.springframework.beans.factory.DisposableBean;
          import org.springframework.beans.factory.InitializingBean;
          import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
          import org.springframework.beans.factory.config.BeanPostProcessor;
          import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;

          public class B implements InitializingBean,
          BeanFactoryAware{

          public B() {
          System.out.println("Class B");
          }

          public void init() {
          System.out.println("Class B init");
          }

          @Override
          public void afterPropertiesSet() throws Exception {
          System.out.println("Class B afterPropertiesSet");

          }

          @Override
          public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
          System.out.println("Class B beanFactory");

          }

          public void postProcessBeanFactory(
          ConfigurableListableBeanFactory beanFactory) throws BeansException {
          // TODO Auto-generated method stub
          System.out.println("Class B postProcessBeanFactory");
          }


          }
          執行輸出:Class A
          Class A setBeanFactory()
          Class A afterPropertiesSet()
          Class A init
          Class A postProcessBeanFactory
          2010-11-3 21:33:31 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
          信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@8916a2: defining beans [A,B]; root of factory hierarchy
          Class B
          Class B beanFactory
          Class A postProcessBeforeInitialization
          2010-11-3 21:33:31 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
          信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@8916a2: defining beans [A,B]; root of factory hierarchy
          Class A destroy()
          Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'B' defined in class path resource [icoContext.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
          at java.security.AccessController.doPrivileged(Native Method)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
          at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
          at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
          at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
          at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
          at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
          at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
          at com.google.springioctest.Test.main(Test.java:8)
          Caused by: java.lang.NullPointerException
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1393)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1375)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
          ... 15 more
          拋異常了。。。。。
          原因是A類里的postProcessBeforeInitialization,postProcessAfterInitialization2個方法沒有返回bean,修改下
          執行輸出:
          Class A
          Class A setBeanFactory()
          Class A afterPropertiesSet()
          Class A init
          Class A postProcessBeanFactory
          2010-11-3 21:37:10 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
          信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1193779: defining beans [A,B]; root of factory hierarchy
          Class B
          Class B beanFactory
          Class A postProcessBeforeInitialization
          Class B afterPropertiesSet
          Class B init
          Class A postProcessAfterInitialization
          在B類被初始化之后,也就是調用afterPropertiesSet之前那段時間,屬性初始化完成后,進行了回調,控制B類

          注意:在寫被監控的Bean的時候,不能實現BeanFactoryPostProcessor這個接口,沒看源碼,其實也不知道是什么原因,哈哈,只能硬記了

          posted on 2010-11-03 17:04 羔羊 閱讀(192) 評論(0)  編輯  收藏 所屬分類: IOC

          主站蜘蛛池模板: 故城县| 鄂伦春自治旗| 阿图什市| 龙泉市| 五常市| 蓝田县| 罗源县| 五家渠市| 闽侯县| 云安县| 静宁县| 佛学| 武平县| 沂水县| 安义县| 沈丘县| 嘉禾县| 定远县| 韩城市| 永川市| 盈江县| 吉木乃县| 昌黎县| 呼图壁县| 康马县| 襄城县| 垣曲县| 昭通市| 石狮市| 中宁县| 临颍县| 连南| 武城县| 靖州| 治县。| 泾阳县| 吉安市| 金乡县| 伊通| 鄂托克旗| 桂东县|