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

相关文章

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

深入浅出SpringBoot WebSocket构建实时应用全面指南

《深入浅出SpringBootWebSocket构建实时应用全面指南》WebSocket是一种在单个TCP连接上进行全双工通信的协议,这篇文章主要为大家详细介绍了SpringBoot如何集成WebS... 目录前言为什么需要 WebSocketWebSocket 是什么Spring Boot 如何简化 We

SpringBoot监控API请求耗时的6中解决解决方案

《SpringBoot监控API请求耗时的6中解决解决方案》本文介绍SpringBoot中记录API请求耗时的6种方案,包括手动埋点、AOP切面、拦截器、Filter、事件监听、Micrometer+... 目录1. 简介2.实战案例2.1 手动记录2.2 自定义AOP记录2.3 拦截器技术2.4 使用Fi