Spring的发布处理器(BeanPostProcessor)

55 篇文章 0 订阅
33 篇文章 0 订阅


       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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值