Spring BeanPostProcessor 接口

2023-10-06 11:05

本文主要是介绍Spring BeanPostProcessor 接口,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Spring BeanPostProcessor 接口

目录

Spring BeanPostProcessor 接口

Spring bean定义继承


BeanPostProcessor 的接口定义,可以实现提供自己的实例化逻辑,依赖解析逻辑等,也可以以后在Spring容器实例化完毕,配置和初始化一个bean通过插入一个或多个的BeanPostProcessor实现一些自定义逻辑回调方法实现。

可以配置多个的BeanPostProcessor接口,控制这些的BeanPostProcessor接口,通过设置属性顺序执行顺序提供的BeanPostProcessor实现了Ordered接口。

BeanPostProcessor可以对bean(或对象)操作实例,这意味着Spring IoC容器实例化一个bean实例,然后BeanPostProcessor的接口做好自己的工作。

ApplicationContext会自动检测已定义实现的BeanPostProcessor接口和注册这些bean类为后置处理器,可然后通过在容器创建bean,在适当时候调用任何bean。

示例:

下面的示例显示如何编写,注册和使用BeanPostProcessor 可以在一个ApplicationContext 的上下文。

使用Eclipse IDE,然后按照下面的步骤来创建一个Spring应用程序:

步骤描述
1Create a project with a name SpringExample and create a package com.manongjc under the src folder in the created project.
2Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter.
3Create Java classes HelloWorldInitHelloWorld and MainApp under the com.manongjcpackage.
4Create Beans configuration file Beans.xml under the src folder.
5The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.

这里是 HelloWorld.java 文件的内容:

package com.manongjc;public class HelloWorld {private String message;public void setMessage(String message){this.message  = message;}public void getMessage(){System.out.println("Your Message : " + message);}public void init(){System.out.println("Bean is going through init.");}public void destroy(){System.out.println("Bean will destroy now.");}
}

这是实现BeanPostProcessor,之前和之后的任何bean的初始化它打印一个bean的名字非常简单的例子。可以因为有两个后处理器的方法对内部bean对象访问之前和实例化一个bean后执行更复杂的逻辑。

这里是InitHelloWorld.java文件的内容:

package com.manongjc;import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.BeansException;public class InitHelloWorld implements BeanPostProcessor {public Object postProcessBeforeInitialization(Object bean,String beanName) throws BeansException {System.out.println("BeforeInitialization : " + beanName);return bean;  // you can return any other object as well}public Object postProcessAfterInitialization(Object bean,String beanName) throws BeansException {System.out.println("AfterInitialization : " + beanName);return bean;  // you can return any other object as well}}

以下是MainApp.java 文件的内容。在这里,需要注册一个关闭挂钩registerShutdownHook() 是在AbstractApplicationContext类中声明的方法。这将确保正常关闭,并调用相关的destroy方法。

package com.manongjc;import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp {public static void main(String[] args) {AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");HelloWorld obj = (HelloWorld) context.getBean("helloWorld");obj.getMessage();context.registerShutdownHook();}
}

下面是init和destroy方法需要的配置文件beans.xml文件:

<?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-3.0.xsd"><bean id="helloWorld" class="com.manongjc.HelloWorld"init-method="init" destroy-method="destroy"><property name="message" value="Hello World!"/></bean><bean class="com.manongjc.InitHelloWorld" /></beans>

创建源代码和bean配置文件完成后,让我们运行应用程序。如果一切顺利,这将打印以下信息:

​BeforeInitialization : helloWorld
Bean is going through init.
AfterInitialization : helloWorld
Your Message : Hello World!
Bean will destroy now.​

Spring bean定义继承

一个bean定义包含了大量的配置信息,包含了构造器参数,属性值,和容器指定的信息,比如初始化方法,静态工厂方法名,等等。子bean从父bean那里继承配置数据。子定义可以重写一些值,或者按需添加别的值。使用子和父beans定义可以节约大量的输入。这就是使用模板的高效。

如果你使用ApplicationContext接口程序化编程,子bean定义由ChildBeanDefinition类表示。大多数使用者不会在这个级别使用它们,而是配置bean定义声明,比如ClassPathXmlApplicationContext。当你使用基于XML配置元数据时,你使用parent属性指定一个子bean定义,指定父bean作为这个属性的值。

<bean id="inheritedTestBean" abstract="true"class="org.springframework.beans.TestBean"><property name="name" value="parent"/><property name="age" value="1"/>
</bean><bean id="inheritsWithDifferentClass"class="org.springframework.beans.DerivedTestBean"parent="inheritedTestBean" init-method="initialize"><property name="name" value="override"/><!-- the age property value of 1 will be inherited from  parent --></bean>

一个子bean定义使用了来自父bean定义的bean类。如果没有指定,但也可以重写。在后面的案例中,子bean类必须与父bean类是兼容的,即,其必须接受父类的属性值。

一个子bean定义从其父bean中继承了构造器参数值,属性值,和方法重写,其还可以添加新的值。燃和初始化方法,销毁方法,并且/或者静态工厂方法设置将重写对应的父bean的设置。

剩下的设置总是从子定义中获取:depends on,autowire mode,依赖检查,singleton,scope,延迟加载。

前面的例子明确指定了父bean为abstract类型,其通过属性abstrat指定。如果父定义没有指定一个类,明确指定其为abstract是必须的。

<bean id="inheritedTestBeanWithoutClass" abstract="true"><property name="name" value="parent"/><property name="age" value="1"/>
</bean><bean id="inheritsWithClass" class="org.springframework.beans.DerivedTestBean"parent="inheritedTestBeanWithoutClass" init-method="initialize"><property name="name" value="override"/><!-- age will inherit the value of 1 from the parent bean definition-->
</bean>

父bean不能自己实例化,因为它完成不了,并且它也标识为abstract。当一个定义是abstract,其仅仅是一个纯净的模板bean定义,专门为子定义服务。使用这个abstract bean定义,通过作为另一个bean的ref属性关联它或者调用getBean()还有父bean的id关联它,将返回错误。相似地,容器内部的preInstantiateSingletons()方法忽视了bean定义(其如abstract定义一样)。

注意:ApplicationContext 默认的预处理实例化所有的singletons。因此这是很重要的(至少对于singleton beans),如果你想使用一个父bean作为一个模板,并且这个定义指定了一个类,你必须确保abstract属性值为true,否则应用上下文将真的会预处理实例化这个abstract bean。

这篇关于Spring BeanPostProcessor 接口的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

springboot集成easypoi导出word换行处理过程

《springboot集成easypoi导出word换行处理过程》SpringBoot集成Easypoi导出Word时,换行符n失效显示为空格,解决方法包括生成段落或替换模板中n为回车,同时需确... 目录项目场景问题描述解决方案第一种:生成段落的方式第二种:替换模板的情况,换行符替换成回车总结项目场景s

SpringBoot集成redisson实现延时队列教程

《SpringBoot集成redisson实现延时队列教程》文章介绍了使用Redisson实现延迟队列的完整步骤,包括依赖导入、Redis配置、工具类封装、业务枚举定义、执行器实现、Bean创建、消费... 目录1、先给项目导入Redisson依赖2、配置redis3、创建 RedissonConfig 配

SpringBoot中@Value注入静态变量方式

《SpringBoot中@Value注入静态变量方式》SpringBoot中静态变量无法直接用@Value注入,需通过setter方法,@Value(${})从属性文件获取值,@Value(#{})用... 目录项目场景解决方案注解说明1、@Value("${}")使用示例2、@Value("#{}"php

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。

SpringBoot全局域名替换的实现

《SpringBoot全局域名替换的实现》本文主要介绍了SpringBoot全局域名替换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录 项目结构⚙️ 配置文件application.yml️ 配置类AppProperties.Ja

SpringBoot实现不同接口指定上传文件大小的具体步骤

《SpringBoot实现不同接口指定上传文件大小的具体步骤》:本文主要介绍在SpringBoot中通过自定义注解、AOP拦截和配置文件实现不同接口上传文件大小限制的方法,强调需设置全局阈值远大于... 目录一  springboot实现不同接口指定文件大小1.1 思路说明1.2 工程启动说明二 具体实施2