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在成功完成嵌入初始化以后调用他。


目录
相关文章
|
4月前
|
XML JavaScript Java
BeanFactory 和 FactoryBean的区别
本文介绍了Spring框架中的`BeanFactory`和`FactoryBean`。`BeanFactory`是Spring的核心接口,用于管理Bean的创建、配置及依赖注入。其实现包括`DefaultListableBeanFactory`和已废弃的`XmlBeanFactory`。`FactoryBean`则用于动态创建Bean实例,支持懒加载及AOP代理创建。文章还通过示例展示了如何实现一个`FactoryBean`,并通过测试验证其功能。最后附上了作者信息及版权声明。
BeanFactory 和 FactoryBean的区别
|
9月前
|
设计模式 Java Spring
BeanFactory与FactoryBean的区别
BeanFactory与FactoryBean的区别
|
Java Spring 容器
【Spring源码】 BeanFactory和FactoryBean是什么?
面试官:“看过Spring源码吧,简单说说Spring中BeanFactory和FactoryBean的区别是什么?”
17481 6
【Spring源码】 BeanFactory和FactoryBean是什么?
|
Java 容器 Spring
七.Spring源码剖析-Bean的实例化-属性注入
喜欢我的文章的话就给个好评吧,你的肯定是我坚持写作最大的动力,来吧兄弟们,给我一点动力 这一章节我们来讨论创建Bean过程中的属性注入,在Spring的IOC容器启动过程中,会把定义的Bean封装成BeanDefinition注册到一个ConcurrentHashMap中,Bean注册完成后,就会对单利的且lazy-init=false 的Bean进行实例化。创建Bean的代码在 AbstractAutowireCapableBeanFactory#doCreateBean 中,当Bean创建成功之后,会调用AbstractAutowireCapableBeanFactory#populat
|
存储 缓存 Java
六.Spring源码剖析-单利Bean的实例化
前面系列章节我们分析了Spring的IOC的启动流程,包括:容器创建,配置加载,配置解析,Bean注册等几个阶段,所以Bean注册其实就是把Bean的相关属性,依赖关系等封装成BeanDeafinition对象,然后注册到一个ConcurrentHashMap中。要注意的是这个BeanDeafinition只是对Bean的一个定义封装而已,并不是真正的Bean的实例,那Bean的实例是在什么时候创建的?有三种情况 如果是单利Bean,且lazy-init=false 急切初始(即时,立即,迫切,饥饿都是一个意思)的情况,在IOC容器启动之后就会根据BeanDeafinition对Bean进行
BeanFactoryPostProcessor-spring源码详解(三)
BeanFactoryPostProcessor-spring源码详解(三)
BeanFactoryPostProcessor-spring源码详解(三)
|
XML Java 数据格式
Spring之BeanFactoryPostProcessor和BeanPostProcessor
Spring之BeanFactoryPostProcessor和BeanPostProcessor
312 0
Spring之BeanFactoryPostProcessor和BeanPostProcessor
|
XML 缓存 Java
Spring源码(三)-Bean工厂-BeanFactory
DefaultListableBeanFactory 上篇就使用到了 DefaultListableBeanFactory类中的registerBeanDefinition方法将 `BeanDefinition` 缓存在了 `beanDefinitionMap` Map中。`DefaultListableBeanFactory` 是Spring中最为核心的 `BeanFactory`。
320 0
|
存储 缓存 Java
Spring源码(四)-FactoryBean
上一篇讲到了BeanFactory,那就不得不提一下FactoryBean,先看一下它的定义吧! 看到里面的三个方法名,我们可以见名知意了。Bean实例、Bean类型、bean是否单例。
431 1
|
存储 XML Java
Spring源码(一)-Bean的定义-BeanDefinition
在 Spring 容器中,我们广泛使用的是一个一个的 Bean,那在Spring 中,我们可以如何去定义一个Bean?
962 1

热门文章

最新文章