Spring点滴五:Spring中的后置处理器BeanPostProcessor讲解

2024-09-08 00:48

本文主要是介绍Spring点滴五:Spring中的后置处理器BeanPostProcessor讲解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

https://www.cnblogs.com/sishang/p/6576665.html

BeanPostProcessor接口作用:

     如果我们想在Spring容器中完成bean实例化、配置以及其他初始化方法前后要添加一些自己逻辑处理。我们需要定义一个或多个BeanPostProcessor接口实现类,然后注册到Spring IoC容器中。

 

复制代码

package com.test.spring;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/*** bean后置处理器* @author zss**/
public class PostProcessor implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean,String beanName) throws BeansException {if ("narCodeService".equals(beanName)) {//过滤掉bean实例ID为narCodeServicereturn bean;}System.out.println("后置处理器处理bean=【"+beanName+"】开始");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean,String beanName) throws BeansException {if ("narCodeService".equals(beanName)) {return bean;}System.out.println("后置处理器处理bean=【"+beanName+"】完毕!");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}return bean;}}注意:接口中两个方法不能返回null,如果返回null那么在后续初始化方法将报空指针异常或者通过getBean()方法获取不到bena实例对象因为后置处理器从Spring IoC容器中取出bean实例对象没有再次放回IoC容器中

复制代码

将Spring的后置处理器PostProcessor配置到Spring配置文件中

复制代码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 定义一个bean --><bean id="narCodeService" class="com.test.service.impl.NarCodeServiceImpl"></bean><bean id="beanLifecycle" class="com.test.spring.BeanLifecycle" init-method="init" destroy-method="close"><property name="name" value="张三"></property><property name="sex" value="男"></property></bean><!-- Spring后置处理器 --><bean id="postProcessor" class="com.test.spring.PostProcessor"/>
</beans>

复制代码

BeanPostProcessor API:

复制代码

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.    */  //实例化、依赖注入完毕,在调用显示的初始化之前完成一些定制的初始化任务  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.       */  //实例化、依赖注入、初始化完毕时执行  Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;  }

复制代码

由API可以看出:
1:后置处理器的postProcessorBeforeInitailization方法是在bean实例化,依赖注入之后及自定义初始化方法(例如:配置文件中bean标签添加init-method属性指定Java类中初始化方法、
@PostConstruct注解指定初始化方法,Java类实现InitailztingBean接口)之前调用
2:后置处理器的postProcessorAfterInitailization方法是在bean实例化、依赖注入及自定义初始化方法之后调用注意:1.BeanFactory和ApplicationContext两个容器对待bean的后置处理器稍微有些不同。ApplicationContext容器会自动检测Spring配置文件中那些bean所对应的Java类实现了BeanPostProcessor
接口,并自动把它们注册为后置处理器。在创建bean过程中调用它们,所以部署一个后置处理器跟普通的bean没有什么太大区别。

      2.BeanFactory容器注册bean后置处理器时必须通过代码显示的注册,在IoC容器继承体系中的ConfigurableBeanFactory接口中定义了注册方法

复制代码

    /**  * Add a new BeanPostProcessor that will get applied to beans created  * by this factory. To be invoked during factory configuration.  * <p>Note: Post-processors submitted here will be applied in the order of  * registration; any ordering semantics expressed through implementing the  * {@link org.springframework.core.Ordered} interface will be ignored. Note  * that autodetected post-processors (e.g. as beans in an ApplicationContext)  * will always be applied after programmatically registered ones.  * @param beanPostProcessor the post-processor to register  */    void addBeanPostProcessor(BeanPostProcessor beanPostProcessor);   

复制代码

 Spring如何调用多个BeanPostProcessor实现类:

    我们可以在Spring配置文件中添加多个BeanPostProcessor(后置处理器)接口实现类,在默认情况下Spring容器会根据后置处理器的定义顺序来依次调用。

Spring配置文件:

复制代码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!-- bean定义 -->    <bean id="narCodeService" class="com.test.service.impl.NarCodeServiceImpl"></bean><bean id="postProcessor" class="com.test.spring.PostProcessor"/><bean id="postProcessorB" class="com.test.spring.PostProcessorB"/>
</beans>

复制代码

BeanPostProcessor实现类:

复制代码

package com.test.spring;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/*** bean后置处理器* @author zss**/
public class PostProcessor implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean,String beanName) throws BeansException {System.out.println("后置处理器处理bean=【"+beanName+"】开始");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean,String beanName) throws BeansException {System.out.println("后置处理器处理bean=【"+beanName+"】完毕!");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}return bean;}
}
----------------------------------------------------------------------------------------------------------------------------------------
package com.test.spring;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;public class PostProcessorB implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean,String beanName) throws BeansException {System.out.println("后置处理器开始调用了");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean,String beanName) throws BeansException {System.out.println("后置处理器调用结束了");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}return bean;}
}

复制代码

测试:

复制代码

package com.test.spring;import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class T {AbstractApplicationContext applicationcontext=null;@Beforepublic void before() {System.out.println("》》》Spring ApplicationContext容器开始初始化了......");applicationcontext= new ClassPathXmlApplicationContext(new String[]{"test1-service.xml"});System.out.println("》》》Spring ApplicationContext容器初始化完毕了......");}@Testpublic void  test() {applicationcontext.registerShutdownHook();   }
}

复制代码

测试结果:

复制代码

》》》Spring ApplicationContext容器开始初始化了......
2017-03-19 10:50:29  INFO:ClassPathXmlApplicationContext-Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@18c92ff9: startup date [Sun Mar 19 10:50:29 CST 2017]; root of context hierarchy
2017-03-19 10:50:29  INFO:XmlBeanDefinitionReader-Loading XML bean definitions from class path resource [test1-service.xml]
后置处理器处理bean=【narCodeService】开始
后置处理器开始调用了
后置处理器处理bean=【narCodeService】完毕!
后置处理器调用结束了
》》》Spring ApplicationContext容器初始化完毕了......
2017-03-19 10:50:34  INFO:ClassPathXmlApplicationContext-Closing org.springframework.context.support.ClassPathXmlApplicationContext@18c92ff9: startup date [Sun Mar 19 10:50:29 CST 2017]; root of context hierarchy

复制代码

   在Spring机制中可以指定后置处理器调用顺序,通过让BeanPostProcessor接口实现类实现Ordered接口getOrder方法,该方法返回一整数,默认值为 0,优先级最高,值越大优先级越低

例如:

复制代码

package com.test.spring;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;
/*** bean后置处理器* @author zss**/
public class PostProcessor implements BeanPostProcessor,Ordered{@Overridepublic Object postProcessBeforeInitialization(Object bean,String beanName) throws BeansException {System.out.println("后置处理器处理bean=【"+beanName+"】开始");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean,String beanName) throws BeansException {System.out.println("后置处理器处理bean=【"+beanName+"】完毕!");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}return bean;}@Overridepublic int getOrder() {return 1;}
}
----------------------------------------------------------------------------
package com.test.spring;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;public class PostProcessorB implements BeanPostProcessor,Ordered {@Overridepublic Object postProcessBeforeInitialization(Object bean,String beanName) throws BeansException {System.out.println("后置处理器开始调用了");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean,String beanName) throws BeansException {System.out.println("后置处理器调用结束了");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}return bean;}@Overridepublic int getOrder() {return 0;}
}

复制代码

测试结果:

》》》Spring ApplicationContext容器开始初始化了......
2017-03-19 11:04:10  INFO:ClassPathXmlApplicationContext-Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@18c92ff9: startup date [Sun Mar 19 11:04:10 CST 2017]; root of context hierarchy
2017-03-19 11:04:10  INFO:XmlBeanDefinitionReader-Loading XML bean definitions from class path resource [test1-service.xml]
后置处理器开始调用了
后置处理器处理bean=【narCodeService】开始
后置处理器调用结束了
后置处理器处理bean=【narCodeService】完毕!
》》》Spring ApplicationContext容器初始化完毕了......
2017-03-19 11:04:14  INFO:ClassPathXmlApplicationContext-Closing org.springframework.context.support.ClassPathXmlApplicationContext@18c92ff9: startup date [Sun Mar 19 11:04:10 CST 2017]; root of context hierarchy

这篇关于Spring点滴五:Spring中的后置处理器BeanPostProcessor讲解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1146646

相关文章

SpringBoot中六种批量更新Mysql的方式效率对比分析

《SpringBoot中六种批量更新Mysql的方式效率对比分析》文章比较了MySQL大数据量批量更新的多种方法,指出REPLACEINTO和ONDUPLICATEKEY效率最高但存在数据风险,MyB... 目录效率比较测试结构数据库初始化测试数据批量修改方案第一种 for第二种 case when第三种

Java docx4j高效处理Word文档的实战指南

《Javadocx4j高效处理Word文档的实战指南》对于需要在Java应用程序中生成、修改或处理Word文档的开发者来说,docx4j是一个强大而专业的选择,下面我们就来看看docx4j的具体使用... 目录引言一、环境准备与基础配置1.1 Maven依赖配置1.2 初始化测试类二、增强版文档操作示例2.

一文详解如何使用Java获取PDF页面信息

《一文详解如何使用Java获取PDF页面信息》了解PDF页面属性是我们在处理文档、内容提取、打印设置或页面重组等任务时不可或缺的一环,下面我们就来看看如何使用Java语言获取这些信息吧... 目录引言一、安装和引入PDF处理库引入依赖二、获取 PDF 页数三、获取页面尺寸(宽高)四、获取页面旋转角度五、判断

Spring Boot中的路径变量示例详解

《SpringBoot中的路径变量示例详解》SpringBoot中PathVariable通过@PathVariable注解实现URL参数与方法参数绑定,支持多参数接收、类型转换、可选参数、默认值及... 目录一. 基本用法与参数映射1.路径定义2.参数绑定&nhttp://www.chinasem.cnbs

JAVA中安装多个JDK的方法

《JAVA中安装多个JDK的方法》文章介绍了在Windows系统上安装多个JDK版本的方法,包括下载、安装路径修改、环境变量配置(JAVA_HOME和Path),并说明如何通过调整JAVA_HOME在... 首先去oracle官网下载好两个版本不同的jdk(需要登录Oracle账号,没有可以免费注册)下载完

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

Java中Integer128陷阱

《Java中Integer128陷阱》本文主要介绍了Java中Integer与int的区别及装箱拆箱机制,重点指出-128至127范围内的Integer值会复用缓存对象,导致==比较结果为true,下... 目录一、Integer和int的联系1.1 Integer和int的区别1.2 Integer和in

SpringSecurity整合redission序列化问题小结(最新整理)

《SpringSecurity整合redission序列化问题小结(最新整理)》文章详解SpringSecurity整合Redisson时的序列化问题,指出需排除官方Jackson依赖,通过自定义反序... 目录1. 前言2. Redission配置2.1 RedissonProperties2.2 Red

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.