Spring之BeanPostProcessor接口

这里写图片描述
BeanPostProcessor接口

public interface BeanPostProcessor {

    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;

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

}

官方解释:

Factory hook that allows for custom modification of new bean instances, e.g. checking for marker interfaces or wrapping them with proxies.
ApplicationContexts can autodetect BeanPostProcessor beans in their bean definitions and apply them to any beans subsequently created. Plain bean factories allow for programmatic registration of post-processors, applying to all beans created through this factory.
Typically, post-processors that populate beans via marker interfaces or the like will implement postProcessBeforeInitialization(java.lang.Object, java.lang.String), while post-processors that wrap beans with proxies will normally implementpostProcessAfterInitialization(java.lang.Object, java.lang.String).

如果这个接口的某个实现类被注册到某个容器,那么该容器的每个受管Bean在调用初始化方法之前,都会获得该接口实现类的一个回调。容器调用接口定义的方法时会将该受管Bean的实例和名字通过参数传入方法,进过处理后通过方法的返回值返回给容器。

public class ExampleBeanPostProcessor implements InitializingBean{
    public void initMethod(){
        System.out.println("ExampleBeanPostProcessor init-method initMethod");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("ExampleBeanPostProcessor InitializingBean afterPropertiesSet");
    }
}
public class BeanPostProcessorImpl implements BeanPostProcessor{

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("Bean "+beanName+" init 后 预处理");
        return bean;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("Bean "+ beanName+" init 前 预处理");
        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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
    >
    <bean class="com.xiang.bean.BeanPostProcessorImpl"></bean>
    <bean id="exampleBeanPostProcessor" class="com.xiang.bean.ExampleBeanPostProcessor" init-method="initMethod"></bean>
</beans>
Bean exampleBeanPostProcessor init 前 预处理
ExampleBeanPostProcessor InitializingBean afterPropertiesSet
ExampleBeanPostProcessor init-method initMethod
Bean exampleBeanPostProcessor init 后 预处理

BeanPostProcessor的作用域是容器级的,它只和所在容器有关。如果你在容器中定义了BeanPostProcessor,它仅仅对此容器中的bean进行后置。它不会对定义在另一个容器中的bean进行任何处理。BeanPostProcessor是在spring容器加载了bean的定义文件并且实例化bean之后执行的。

BeanFactory和ApplicationContext容器的注册方式不大一样:若使用BeanFactory,则必须要显示的调用其addBeanPostProcessor()方法进行注册,参数为BeanPostProcessor实现类的实例;如果是使用ApplicationContext,那么容器会在配置文件在中自动寻找实现了BeanPostProcessor接口的Bean,然后自动注册,我们要做的只是配置一个BeanPostProcessor实现类的Bean就可以了。

BeanPostPrcessorImpl beanPostProcessor = new BeanPostPrcessorImpl();
Resource resource = new FileSystemResource("applicationContext.xml");
ConfigurableBeanFactory factory = new XmlBeanFactory(resource);
factory.addBeanPostProcessor(beanPostProcessor);

假如我们使用了多个的BeanPostProcessor的实现类,那么如何确定处理顺序呢?其实只要实现Ordered接口,设置order属性就可以很轻松的确定不同实现类的处理顺序了。

public class BeanPostProcessorImplFirst implements BeanPostProcessor,Ordered{

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("Bean "+beanName+" init 前 预处理 First");
        return bean;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("Bean "+beanName+" init 后 预处理 First");
        return bean;
    }

    @Override
    public int getOrder() {
        return 0;
    }

}
public class BeanPostProcessorImplSecond implements BeanPostProcessor,Ordered{

    @Override
    public int getOrder() {
        return 1;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("Bean "+beanName+" init 前 预处理 Second");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("Bean "+beanName+" init 后 预处理 Second");
        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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
    >
    <bean class="com.xiang.bean.BeanPostProcessorImpl"></bean>
    <bean class="com.xiang.bean.BeanPostProcessorImplFirst"></bean>
    <bean class="com.xiang.bean.BeanPostProcessorImplSecond"></bean>
    <bean id="exampleBeanPostProcessor" class="com.xiang.bean.ExampleBeanPostProcessor" init-method="initMethod"></bean>
</beans>
Bean exampleBeanPostProcessor init 后 预处理 First
Bean exampleBeanPostProcessor init 前 预处理 Second
Bean exampleBeanPostProcessor init 前 预处理
ExampleBeanPostProcessor InitializingBean afterPropertiesSet
ExampleBeanPostProcessor init-method initMethod
Bean exampleBeanPostProcessor init 前 预处理 First
Bean exampleBeanPostProcessor init 后 预处理 Second
Bean exampleBeanPostProcessor init 后 预处理

优先级高的先执行,有优先级的比没有优先级的先执行

BeanPostProcessor的前置和后置处理的方法中都要返回该bean,不能是null

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值