`
haofenglemon
  • 浏览: 243942 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

BeanPostProcessor

阅读更多
BeanPostProcessor

我们关注的第一个扩展点是BeanPostProcessor借口,它定义了两个方法postProcessBeforeInitialization(Object bean, String beanName)和postProcessAfterInitialization(Object bean, String beanName)。实现该接口可提供自定义的实例化逻辑,依赖解析逻辑等。如果你想在Spring容器完成bean的实例化,配置和其他的初始化后执行一些自定义逻辑,我们可以通过插入一个或多个BeanPostProcessor实现。

书上说可以通过设置order属性来控制BeanPostProcessor的执行次序,前提是BeanPostProcessor还要实现Ordered接口的时候。不好意思,这个我没有测试过。

BeanPostProcessor可以对bean(或对象)的多个实例进行操作,也就是说Ioc容器会为你实例化bean,然后BeanPostProcessor去处理它。另外,BeanPostProcessor的作用域是容器级德,它只对容器中的bean进行后置处理,它不会对另一个容器中的bean进行后置处理。

当BeanPostProcessor接口的实现类被注册为容器的后置处理器后,对于由此容器创建的每一个bean实例在初始化方法调用前,后置处理器都会从容器中获取一个回调。

另外注意的一点就是BeanFactory和ApplicationContext对待bean后置处理器有点不同。ApplicationContext会自动检测在配置文件中实现了BeanPostProcessor接口的所有bean,并把它注册为后置处理器,然后在容器创建好bean之后调用它。而使用BeanFactory的时候,bean后置处理器必须通过下面的代码显式的去注册。

例如:ConfigurableBeanFactory factory = new XmlBeanFactory("");

     MyBeanPostProcessor postProcessor = new MyBeanPostProcessor();

    factory.addBeanPostProcessor(postProcessor);

因为显式注册不是很方便,这也是为什么在各种Spring应用中首选ApplicationContext的一个原因。

下面通过一个例子来了解一下BeanPostProcessor的应用。

首先是一个实现了BeanPostProcessor接口的类,InstantiationTracingBeanPostProcessor

代码如下:

public class InstantiationTracingBeanPostProcessor implements BeanPostProcessor{

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
      
        return bean;
    }

    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
      
        if(bean instanceof MessageSource)
        {
            System.out.println("MessageSource before Initialization Start!!!");
            System.out.println("Bean '" + beanName + "' created:" + bean.toString());
            System.out.println("MessageSource before Initialization End!!!");
        }
//        else if(bean instanceof PostProcess1)
//        {
//            System.out.println("PostProcess1 before Initialization Start!!!");
//            System.out.println("Bean '" + beanName + "' created:" + bean.toString());
//            System.out.println("PostProcess1 before Initialization End!!!");
//        }
      
        return bean;
    }
}

下面是一个配置文件,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:lang="http://www.springframework.org/schema.lang"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd">

<bean id="messageSource"
  class="org.springframework.context.support.ResourceBundleMessageSource">
  <property name="basenames" value="test-messages"></property>
</bean>

<bean id="postProcess1"
  class="co.jp.beanPostProcess.PostProcess1">
  <property name="name" value="xiaohailin"></property>
</bean>

<bean
  class="co.jp.beanPostProcess.InstantiationTracingBeanPostProcessor" />

</beans>

测试的主类的代码如下:

public class BeanPostProcessorDemo {

    public static void main(String[] args) {

        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "beanPostProcess.xml");

         ResourceBundleMessageSource hello = (ResourceBundleMessageSource) ctx
                .getBean("messageSource");
        System.out.println(hello);

        PostProcess1 hello2 = (PostProcess1) ctx.getBean("postProcess1");
        System.out.println(hello2);

    }
}
运行结果:

[2009/08/08 17:40:40][INFO][AbstractApplicationContext] Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1ac3c08: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1ac3c08]; startup date [Mon Aug 09 17:40:40 CST 2008]; root of context hierarchy
[2009/08/08 17:40:40][INFO][XmlBeanDefinitionReader] Loading XML bean definitions from class path resource [beanPostProcess.xml]
[2009088 17:40:41][INFO][AbstractApplicationContext] Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1ac3c08]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1507fb2
[2009/08/08 17:40:41][INFO][AbstractApplicationContext$BeanPostProcessorChecker] Bean 'co.jp.beanPostProcess.InstantiationTracingBeanPostProcessor' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
MessageSource before Initialization Start!!!
Bean 'messageSource' created:org.springframework.context.support.ResourceBundleMessageSource: basenames=[test-messages]
MessageSource before Initialization End!!!
[2009/08/08  17:40:41][INFO][DefaultListableBeanFactory] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1507fb2: defining beans [messageSource,postProcess1,co.jp.beanPostProcess.InstantiationTracingBeanPostProcessor]; root of factory hierarchy
org.springframework.context.support.ResourceBundleMessageSource: basenames=[test-messages]
co.jp.beanPostProcess.PostProcess1@1995d80
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics