BeanPostProcessor学习

简介: BeanPostProcessor是什么?在看bean初始化时,看到了很多的BeanPostProcessor,具体作用还真搞不清楚。前人说这是spring的扩展点,遵循“开-闭原则”的一个扩展。可以进行自定义的实例化、初始化、依赖装配、依赖检查等流程,即可以覆盖默认的实例化,也可以增强初始化、依赖注入、依赖检查等流程 但我还是一脸蒙逼,到底是什么鬼?网上找几个实例来个大体认知一下

BeanPostProcessor是什么?在看bean初始化时,看到了很多的BeanPostProcessor,具体作用还真搞不清楚。

前人说这是spring的扩展点,遵循“开-闭原则”的一个扩展。可以进行自定义的实例化、初始化、依赖装配、依赖检查等流程,即可以覆盖默认的实例化,也可以增强初始化、依赖注入、依赖检查等流程 但我还是一脸蒙逼,到底是什么鬼?

网上找几个实例来个大体认知一下

对所有bean做个代理:http://guoliangqi.iteye.com/blog/635826

对bean做一些改动:http://blog.csdn.net/partner4java/article/details/6973782

改变Bean的属性值:http://winneryj.iteye.com/blog/307736

做预加载:http://292528867.iteye.com/blog/2159499 感觉这个可以使用init-method来实现

这些实例都是对bean的属性和行为都可以修改

BeanPostProcessor定义

/**
 * Factory hook that allows for custom modification of new bean instances,
 * e.g. checking for marker interfaces or wrapping them with proxies.
 * 大体意思是可以检查相应的标识接口完成一些自定义功能实现,如包装目标对象到代理对象。
 */public interface BeanPostProcessor {
    /**
     * Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean
     * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
     * or a custom init-method). The bean will already be populated with property values.
     * The returned bean instance may be a wrapper around the original.
     * <p>The default implementation returns the given {@code bean} as-is.
     * @param bean the new bean instance     * @param beanName the name of the bean
     * @return the bean instance to use, either the original or a wrapped one;
     * if {@code null}, no subsequent BeanPostProcessors will be invoked
     * @throws org.springframework.beans.BeansException in case of errors
     * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
     */
    Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
    /**
   * Apply this BeanPostProcessor to the given new bean instance <i>after</i> any bean
   * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
   * or a custom init-method). The bean will already be populated with property values.
   * The returned bean instance may be a wrapper around the original.
   * <p>In case of a FactoryBean, this callback will be invoked for both the FactoryBean
   * instance and the objects created by the FactoryBean (as of Spring 2.0). The
   * post-processor can decide whether to apply to either the FactoryBean or created
   * objects or both through corresponding {@code bean instanceof FactoryBean} checks.
   * <p>This callback will also be invoked after a short-circuiting triggered by a
   * {@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method,
   * in contrast to all other BeanPostProcessor callbacks.
   * <p>The default implementation returns the given {@code bean} as-is.
   * @param bean the new bean instance   * @param beanName the name of the bean
   * @return the bean instance to use, either the original or a wrapped one;
   * if {@code null}, no subsequent BeanPostProcessors will be invoked
   * @throws org.springframework.beans.BeansException in case of errors
   * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
   * @see org.springframework.beans.factory.FactoryBean
   */
    Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
}

接口结构很简单,两个方法

  1. postProcessBeforeInitialization:在Spring调用任何bean的初始化钩子(例如InitializingBean.afterPropertiesSet或者init方法)之前被调用。
  2. postProcessAfterInitialization:Spring在成功完成嵌入初始化以后调用他。


目录
相关文章
|
1月前
|
设计模式 Java Spring
BeanFactory与FactoryBean的区别
BeanFactory与FactoryBean的区别
|
1月前
|
XML 数据格式
Spring5源码(10)-BeanPostProcessor和BeanFactoryPostProcessor的区别
Spring5源码(10)-BeanPostProcessor和BeanFactoryPostProcessor的区别
29 0
|
9月前
|
XML 开发框架 Java
聊聊Spring扩展点BeanPostProcessor和BeanFactoryPostProcessor
今天聊一聊spring中很重要的两个扩展点BeanPostProcessor和BeanFactoryPostProcessor,spring之所以如次强大,是因为它提供了丰富的功能给我们使用,但是我觉得最强大的是它扩展点,因为有了各种扩展点,我们才能去开发一些自己的需求,一个框架的强大之处也在于它能否灵活的配置,能够支持很好的扩展。
42 0
|
10月前
|
Java Spring 容器
深入理解Spring源码之bean的生命周期控制器BeanPostProcessor
深入理解Spring源码之bean的生命周期控制器BeanPostProcessor
|
11月前
|
Java 容器 Spring
七.Spring源码剖析-Bean的实例化-属性注入
喜欢我的文章的话就给个好评吧,你的肯定是我坚持写作最大的动力,来吧兄弟们,给我一点动力 这一章节我们来讨论创建Bean过程中的属性注入,在Spring的IOC容器启动过程中,会把定义的Bean封装成BeanDefinition注册到一个ConcurrentHashMap中,Bean注册完成后,就会对单利的且lazy-init=false 的Bean进行实例化。创建Bean的代码在 AbstractAutowireCapableBeanFactory#doCreateBean 中,当Bean创建成功之后,会调用AbstractAutowireCapableBeanFactory#populat
|
XML Java 数据库连接
深入理解Spring IOC之扩展篇(二)、BeanFactoryPostProcessor和BeanPostProcessor
深入理解Spring IOC之扩展篇(二)、BeanFactoryPostProcessor和BeanPostProcessor
75 1
|
11月前
|
XML JSON Java
介绍 Spring 体系、Bean 生命周期剖析以及核心接口 BeanFactory、BeanFactoryPostProcessor、BeanPostProcessor
介绍 Spring 体系、Bean 生命周期剖析以及核心接口 BeanFactory、BeanFactoryPostProcessor、BeanPostProcessor
272 0
BeanFactoryPostProcessor-spring源码详解(三)
BeanFactoryPostProcessor-spring源码详解(三)
BeanFactoryPostProcessor-spring源码详解(三)
|
XML Java 数据格式
Spring之BeanFactoryPostProcessor和BeanPostProcessor
Spring之BeanFactoryPostProcessor和BeanPostProcessor
230 0
Spring之BeanFactoryPostProcessor和BeanPostProcessor
|
Java Spring 容器