Spring5应用之BeanPostProcessor

作者简介 :☕️大家好,我是Aomsir,一个爱折腾的开发者!
个人主页Aomsir_Spring5应用专栏,Netty应用专栏,RPC应用专栏
当前专栏Spring5应用专栏_Aomsir的博客

前言

BeanPostProcessor在中文中可以翻译为"Bean后置处理器 ",它用于对Spring框架创建的对象进行后续处理或加工

BeanPostProcessor是Spring框架中的一个高级特性 ,它在Spring的底层实现中起到了关键作用。很多高级功能的封装都离不开这项技术。当我们后续深入探讨**AOP(面向切面编程)**时,会对其进行更深入的分析

在Spring中,Bean的创建包括调用构造方法属性注入执行BeanPostProcessor的前置处理调用InitializingBean和自定义初始化方法,再经过BeanPostProcessor的后置处理后得到最终Bean实例

在Spring中,BeanPostProcessor的before/after方法接收bean实例和beanName作为参数,并在加工后返回该bean,最终交由Spring容器进行管理

在实战中,我们很少通过InitializingBean或属性设置进行初始化,而更多地是通过BeanPostProcessor来完成。因此,很多时候我们并不需要明确区分其before和after方法

开发步骤

  • 创建Bean对应的类
  • 实现 BeanPostProcessor接口
  • Spring的配置文件中进行配置
  • 编写测试方法
  • 测试结果如下
    • name属性的值由Aomsir变为了Aomsir1
java 复制代码
@Data
public class Category {
    private Integer id;
    private String name;
}
java 复制代码
public class MyBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        // 不处理,但是需要返回
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

        if (bean instanceof Category) {
            Category category = (Category) bean;
            category.setName("Aomsir1");
        }
        return bean;
    }
}
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" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="category" class="com.aomsir.basic.beanpost.Category">
        <property name="id" value="1" />
        <property name="name" value="Aomsir" />
    </bean>

    <bean id="myBeanPostProcessor" class="com.aomsir.basic.beanpost.MyBeanPostProcessor" />
</beans>
java 复制代码
public class TestSpring {
    @Test
    public void test13() {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        Category category = ctx.getBean("category", Category.class);
        System.out.println("category = " + category);
    }
}

注意

BeanPostProcessor会对Spring工厂中的所有Bean进行处理。因此,在实现BeanPostProcessor接口的类中,我们需要对不同的Bean进行区分处理,以避免潜在的问题

参考文献

相关推荐
0xDevNull23 分钟前
Linux 中 Nginx 代理 Redis 的详细教程
redis·后端
GetcharZp39 分钟前
告别 Nginx 手动配置!这款 Go 语言开发的云原生网关,才是容器化时代的真香神器!
后端
RuoyiOffice1 小时前
SpringBoot+Vue3 企业考勤如何处理法定假期?节假日方案、调休补班与工作日判断链路拆解
spring boot·后端·vue·anti-design-vue·ruoyioffice·假期·人力
xmjd msup1 小时前
spring security 超详细使用教程(接入springboot、前后端分离)
java·spring boot·spring
Vane11 小时前
从零开发一个AI插件,经历了什么?
人工智能·后端
952362 小时前
SpringBoot统一功能处理
java·spring boot·后端
rleS IONS2 小时前
SpringBoot中自定义Starter
java·spring boot·后端
DevilSeagull2 小时前
MySQL(2) 客户端工具和建库
开发语言·数据库·后端·mysql·服务
TeDi TIVE3 小时前
springboot和springframework版本依赖关系
java·spring boot·后端
雨辰AI3 小时前
SpringBoot3 + 人大金仓 V9 微服务监控实战|Prometheus+Grafana+SkyWalking 全链路监控
数据库·后端·微服务·grafana·prometheus·skywalking