首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >BeanPostProcessor学习

BeanPostProcessor学习

作者头像
码农戏码
发布2021-03-23 10:19:48
2280
发布2021-03-23 10:19:48
举报
文章被收录于专栏:DDDDDD

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在成功完成嵌入初始化以后调用他。
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-07-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码农戏码 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • BeanPostProcessor定义
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档