整理一些spring常见的扩展点

2023-10-31

一、各种后处理器

1.1、BeanDefinition与BeanFactory扩展

1.1.1、BeanDefinitionRegistryPostProcessor接口

/**
 * Extension to the standard {@link BeanFactoryPostProcessor} SPI, allowing for
 * the registration of further bean definitions <i>before</i> regular
 * BeanFactoryPostProcessor detection kicks in. In particular,
 * BeanDefinitionRegistryPostProcessor may register further bean definitions
 * which in turn define BeanFactoryPostProcessor instances.
 *
 * @author Juergen Hoeller
 * @since 3.0.1
 * @see org.springframework.context.annotation.ConfigurationClassPostProcessor
 */
public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {

    /**
     * Modify the application context's internal bean definition registry after its
     * standard initialization. All regular bean definitions will have been loaded,
     * but no beans will have been instantiated yet. This allows for adding further
     * bean definitions before the next post-processing phase kicks in.
     * @param registry the bean definition registry used by the application context
     * @throws org.springframework.beans.BeansException in case of errors
     */
    void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;

}

这个接口扩展了标准的BeanFactoryPostProcessor 接口,允许在普通的BeanFactoryPostProcessor接口实现类执行之前注册更多的BeanDefinition。特别地是,BeanDefinitionRegistryPostProcessor可以注册BeanFactoryPostProcessor的BeanDefinition。

postProcessBeanDefinitionRegistry方法可以修改在BeanDefinitionRegistry接口实现类中注册的任意BeanDefinition,也可以增加和删除BeanDefinition。原因是这个方法执行前所有常规的BeanDefinition已经被加载到BeanDefinitionRegistry接口实现类中,但还没有bean被实例化。

1.1.2、BeanFactoryPostProcessor接口

BeanFactory生成后,如果想对BeanFactory进行一些处理,该怎么办呢?BeanFactoryPostProcessor接口就是用来处理BeanFactory的。

/**
 * Allows for custom modification of an application context's bean definitions,
 * adapting the bean property values of the context's underlying bean factory.
 *
 * <p>Application contexts can auto-detect BeanFactoryPostProcessor beans in
 * their bean definitions and apply them before any other beans get created.
 *
 * <p>Useful for custom config files targeted at system administrators that
 * override bean properties configured in the application context.
 *
 * <p>See PropertyResourceConfigurer and its concrete implementations
 * for out-of-the-box solutions that address such configuration needs.
 *
 * <p>A BeanFactoryPostProcessor may interact with and modify bean
 * definitions, but never bean instances. Doing so may cause premature bean
 * instantiation, violating the container and causing unintended side-effects.
 * If bean instance interaction is required, consider implementing
 * {@link BeanPostProcessor} instead.
 *
 * @author Juergen Hoeller
 * @since 06.07.2003
 * @see BeanPostProcessor
 * @see PropertyResourceConfigurer
 */
public interface BeanFactoryPostProcessor {

    /**
     * Modify the application context's internal bean factory after its standard
     * initialization. All bean definitions will have been loaded, but no beans
     * will have been instantiated yet. This allows for overriding or adding
     * properties even to eager-initializing beans.
     * @param beanFactory the bean factory used by the application context
     * @throws org.springframework.beans.BeansException in case of errors
     */
    void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;

}

这个接口允许自定义修改应用程序上下文的BeanDefinition,调整上下文的BeanFactory的bean属性值。应用程序上下文可以在BeanFactory的BeanDefinition中自动检测BeanFactoryPostProcessor bean,并在创建任何其他bean之前应用它们。对于定位于系统管理员的自定义配置文件非常有用,它们将覆盖应用程序上下文中配置的bean属性。请参阅PropertyResourceConfigurer及其具体实现,了解解决此类配置需求的开箱即用解决方案。BeanFactoryPostProcessor可能与bean定义交互并修改,但永远不应该将bean实例化。 这样做可能会导致过早的bean实例化,违反容器执行顺序并导致意想不到的副作用。如果需要bean实例交互,请考虑实现BeanPostProcessor接口。

postProcessBeanFactory方法在BeanFactory初始化后,所有的bean定义都被加载,但是没有bean会被实例化时,允许重写或添加属性。

1.2、Bean实例化中的扩展

1.2.1、BeanPostProcessor接口

/**
 * Factory hook that allows for custom modification of new bean instances,
 * e.g. checking for marker interfaces or wrapping them with proxies.
 *
 * <p>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.
 *
 * <p>Typically, post-processors that populate beans via marker interfaces
 * or the like will implement {@link #postProcessBeforeInitialization},
 * while post-processors that wrap beans with proxies will normally
 * implement {@link #postProcessAfterInitialization}.
 *
 * @author Juergen Hoeller
 * @since 10.10.2003
 * @see InstantiationAwareBeanPostProcessor
 * @see DestructionAwareBeanPostProcessor
 * @see ConfigurableBeanFactory#addBeanPostProcessor
 * @see BeanFactoryPostProcessor
 */
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.
     * @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.
     * @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;

}

这个接口,允许自定义修改新的bean实例,例如检查标记接口或用代理包装,注意,如果有相互依赖的bean,这里可能无法使用代理。

postProcessBeforeInitialization方法,在任何bean初始化回调(如InitializingBean的afterPropertiesSet或自定义init方法)之前,将此BeanPostProcessor应用于给定的新的bean实例。 这个bean已经被填充了属性值。 返回的bean实例可能是原始的包装器。

postProcessAfterInitialization方法,在Bean初始化回调(如InitializingBean的afterPropertiesSet或自定义init方法)之后,将此BeanPostProcessor应用于给定的新bean实例。 这个bean已经被填充了属性值。 返回的bean实例可能是原始的包装器。这个方法也会在InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation方法生成对象后再次不让他生成对象(具体可以参考Spring生成bean的过程)。

二、用来感知IOC容器的各种Aware扩展

2.1、BeanNameAware

实现该接口并重写void setBeanName(String var1)方法;获取该bean在BeanFactory配置中的名字

2.2、ApplicationContextAware

实现该接口,并重写setApplicationContext(ApplicationContext applicationContext)方法,获取spring 上下文环境的对象,然后通过该上下文对象获取spring容器中的bean对象

2.3、BeanFactoryAware

实现该接口,并重写void setBeanFactory(BeanFactory beanFactory) 方法,Bean获取配置他们的BeanFactory的引用

2.4、ServletContextAware

实现该接口,并重写void setServletContext(ServletContext servletContext)方法;获取servletContext容器。

2.5、ResourceLoaderAware

实现该接口,并重写void setServletContext(ServletContext servletContext)方法;获取ResourceLoader对象,便能够通过它获得各种资源。

参考

http://blog.gavinzh.com/2017/11/20/spring-develop-summary/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

整理一些spring常见的扩展点 的相关文章

随机推荐

  • 投屏为什么显示无法连接服务器,乐播投屏为什么连不上? 乐播投屏无法连接如何解决?...

    随着乐播投屏吸引的新用户逐渐增多 有些朋友发现自己的手机与电视无法成功连接到一起 更别说进行投屏了 而下面小编就为大家介绍了乐播投屏无法连接电视的原因 希望对你有所帮助 乐播投屏无法连接的处理教程 面对无法连接电视的情况 我们需要先保证自己
  • shell编程100例

    1 编写hello world脚本 bin bash 编写hello world脚本 echo Hello World 2 通过位置变量创建 Linux 系统账户及密码 bin bash 通过位置变量创建 Linux 系统账户及密码 1 是
  • 一分钟学会对合并单元格填充数据(Excel)

    问题描述 大家有没有发现 我们在使用Excel时有很多情况下需要对某列几行的内容进行合并 但是其他列中需要填写的内容却又完全相同 本文用两种方法解决这一问题 重点是方法二哦 一分钟可搞定数百条数据 如下图 需要按照专业进行单元格合并 但是他
  • vue高德地图初体验地图初始化(一)

    vue高德地图初体验地图初始化 安装依赖 引用依赖 地图初始化 AMap Map参数说明 安装依赖 npm i amap amap jsapi loader save 引用依赖 import AMapLoader from amap ama
  • A level数学真题解析及运用

    在A level考试9709数学科目中pure mathematics 3考卷考察范围内有一章节名为complex number 即复数章节 这部分知识点虽然理解难度不大 但是在我国普通高中的数学学习中涉及的较少 考生在接受上有比较大的难度
  • java设计模式-单例模式

    Java中单例 Singleton 模式是一种广泛使用的设计模式 单例模式的主要作用是保证在Java程序中 某个类只有一个实例存在 一些管理器和控制器常被设计成单例模式 单例模式有很多好处 它能够避免实例对象的重复创建 不仅可以减少每次创建
  • C++ operator == 的一些思考

    最近写代码的时候 需要重载 以前也没有进行过太多的思考 都是顺手就写了 在这里就总结一下 下面的代码是 应该是重载 用的最多的写法了 class Demo public Demo int a a a Demo bool operator c
  • java poi读取pdf word excel文档,读取pdf文字图片

    文章目录 依赖 读取pdf文本和图片 简单读取word docx doc文字 读取word doc表格内容 读取word doc图片 读取excel 输出到excel office转pdf 依赖
  • springCloud Eureka 报错解决方案

    在根据大神的文章安装eureka过程遇到些报错 这里记录下比较好的解决方案 史上最简单的 SpringCloud 教程 终章 方志朋的博客 CSDN博客 springcloud 启动Eureka server 直接启动报错 EMERGENC
  • html5图片并列排版,小编,图片与文字并排怎么排版呢?

    图文排版 H5秀 手机图文 小伙伴 小米 我想左边放图片 右边是文字 但是图片插入之后 再编辑文字只能在下一行 我就直接调整段前距 但有时候预览它会错位 想问图片与文字并排如何排版出来呢 这是一个日经题 设置段前距或许是一个方法 但设定的数
  • Windows下PyTorch1.5的下载安装

    在安装PyTorch之前要先安装好CUDA cudNN 以及anaconda 还有就是编译器pycharm 然后你才能开始安装PyTorch 目录 1 创建虚拟环境 2 去PyTorch官网 3 发现警告 4 解决办法 5 pip list
  • 解析波士顿动力Handle机器人背后的技术(附PPT+视频)

    转 http www leiphone com news 201703 URrR8CG2tmtghNDl html 导语 Boston Dynamics 在机器人动力方面堪称翘楚 其由双足或多足机器人组成的机器人天团总是时不时能给我们带来惊
  • Python3 pip

    Python3 pip pip 是 Python 包管理工具 该工具提供了对 Python 包的查找 下载 安装 卸载的功能 软件包也可以在 https pypi org 中找到 目前最新的 Python 版本已经预装了 pip 注意 Py
  • Nginx添加SSL模块

    目录 一 SSL 概述 SSL证书 HTTPS SSL工作原理 二 创建SSL证书 安装openssl 生成证书 三 nginx配置 nginx打补丁添加模块 nginx conf配置 四 访问 一 SSL 概述 SSL Security
  • 向日葵权限mac

    问题 权限打开后自动关上 解决 mac上几乎所有远程软件都会出现这种权限设置问题 换了腾讯会议或其他也没用 方法一 试试先打开系统的安全性设置 将向日葵软件从隐私框里移出来 点击 号移除 再重新添加进去 方法二 将权限的勾选去掉 再添加 然
  • EFCore 数据模型 和 值转换

    操作中经常要涉及到模型和值转换的问题 这里记录一下 实际使用过程中遇到过的问题 而非功能的全部 模型 EFCore中支持字段 参考地址 https docs microsoft com zh cn ef core modeling back
  • SpringBoot框架详解,实战入门教程

    SpringBoot作为当下Java开发最常用的技术框架 相信你也一定听过很多次了 那么到底什么是SpringBoot SpringBoot又有什么用呢 跟着动力节点的视频快速入门springboot 视频观看资源 https www bi
  • CIKM 2023|TASTE:通过文本匹配缓解序列化推荐中流行偏差问题

    序列化推荐系统旨在根据用户的浏览历史动态地为用户推荐下一个商品 这在Yelp TikTok Amazon等众多Web应用程序中发挥着至关重要的作用 这些推荐系统通过使用不同的神经网络架构来学习用户 商品交互中商品之间的依赖关系 从而对用户行
  • Qt:文管打开方式:选择并设置默认程序

    默认启动APP配置文件 local share applications mimeapps list config mimeapps list etc gnome defaults list 全局 QAction action choose
  • 整理一些spring常见的扩展点

    一 各种后处理器 1 1 BeanDefinition与BeanFactory扩展 1 1 1 BeanDefinitionRegistryPostProcessor接口 Extension to the standard link Bea