您的位置:首页 > 编程语言 > Java开发

Spring的发布处理器(BeanPostProcessor)

2016-07-10 10:27 543 查看

       BeanFactoryPostProcessor和BeanPostProcessor都是spring初始化bean的扩展点,两个接口非常相似。

        一、BeanFactoryPostProcessor可以对bean的定义(配置元数据)进行处理。也就是说,Spring IoC容器允许BeanFactoryPostProcessor在容器实际实例化任何其它的bean之前读取配置元数据,并有可能修改它。通过beanFactory可以获取bean的示例或定义等,同时可以修改bean的属性,这是和BeanPostProcessor最大的区别。

BeanDefinition bd = beanFactory.getBeanDefinition("xxBean");
MutablePropertyValues mpv = bd.getPropertyValues();
if(pv.contains("xxName")) {
pv.addPropertyValue("xxName", "icoder");
}
        如果你愿意,你可以配置多个BeanFactoryPostProcessor。通过设置'order'属性来控制BeanFactoryPostProcessor的执行次序。

       注册BeanFactoryPostProcessor的实例,需要重写void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException方法;

        二、注册BeanPostProcessor的实例,需要重载

Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException方法;



Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException方法;

示例:

bean:

package com.yourcompany.spring;

public class HelloBean{
private String message;

public void setMessage(String message){
this.message  = message;
}

public void getMessage(){
System.out.println("Your Message : " + message);
}

public void init(){
System.out.println("Bean is going through init.");
}
}

processor:

package com.yourcompany.spring;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class Processor implements BeanPostProcessor{

public Object postProcessAfterInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("After Initialization : " + arg0);
return arg0;
}

public Object postProcessBeforeInitialization(Object arg0, String arg1)
throws BeansException {
System.out.println("Before Initialization : " + arg0);
return arg0;
}
}

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> 
<bean id="processor" class="com.yourcompany.spring.Processor"></bean>
<bean id="helloBean" class="com.yourcompany.spring.HelloBean" init-method="init">
<property name="message" value="Hello,World!"/>
</bean>
</beans>

程序主入口:

package com.yourcompany.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Hello{
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
HelloBean hello=(HelloBean)context.getBean("helloBean");
hello.getMessage();
}
}<span style="font-size:18px;">
</span>


        三、还有一点区别就是BeanFactoryPostProcessor的回调比BeanPostProcessor要早。

参考:http://blog.csdn.net/mn11201117/article/details/24986325
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息