当前位置: 首页 > news >正文

Spring之【Bean工厂后置处理器】

目录

BeanFactoryPostProcessor

BeanDefinitionRegistryPostProcessor

使用一下Bean工厂后置处理器

定义包扫描范围

定义一个组件Bean

定义一个普通的类

自定义一个组件类实现Bean工厂后处理器

测试类


BeanFactoryPostProcessor

  • 该接口是Spring提供的扩展点之一
  • 是一个函数式接口,只有一个抽象方法
  • Spring会在合适的时机执行方法回调,并将bean工厂作为参数传递进去

既然把ConfigurableListableBeanFactory类型的bean工厂传递过来了,那么该类型中的方法我是不是就可以为所欲为地为自己所用了啊

package org.springframework.beans.factory.config;import org.springframework.beans.BeansException;// 函数式接口
@FunctionalInterface
public interface BeanFactoryPostProcessor {// 参数为bean工厂void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;}

BeanDefinitionRegistryPostProcessor

  • 是BeanFactoryPostProcessor的子接口
  • 自身提供了一个将BeanDefinitionRegistry作为参数传递进来的方法

把BeanDefinitionRegistry传递过来了,BeanDefinitionRegistry是用来管理BeanDefinition的,提供了一系列操作BeanDefinition的方法,那么我就可以操作Spring容器中的BeanDefinition了

package org.springframework.beans.factory.support;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;// 继承了BeanFactoryPostProcessor
public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {// 将BeanDefinitionRegistry作为参数void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;}

使用一下Bean工厂后置处理器

定义包扫描范围

package spring.demo;import org.springframework.context.annotation.ComponentScan;// 扫描spring.demo及其子包
@ComponentScan("spring.demo")
public class SpringConfig {
}

定义一个组件Bean

package spring.demo;import org.springframework.stereotype.Component;// 这里添加了@Component注解
// Spring容器启动过程中通过ConfigurationClassPostProcessor解析@Component注解,将Cat类的BeanDefinitoon放入容器中
@Component
public class Cat {
}

定义一个普通的类

package spring.demo;// Dog类只是一个普通的Java类,没加任何注解
public class Dog {
}

自定义一个组件类实现Bean工厂后处理器

package spring.demo;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.stereotype.Component;@Component
public class CustomBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {@Overridepublic void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {// Dog类只是一个普通的类// 在这里我通过Spring的回调拿到BeanDefinitionRegistry// 通过BeanDefinitionRegistry将Dog类的定义信息放入Spring容器中GenericBeanDefinition beanDefinition = new GenericBeanDefinition();beanDefinition.setBeanClass(Dog.class);registry.registerBeanDefinition("dog", beanDefinition);}@Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {// Spring回调将beanFactory传递过来// 这里通过beanName从beanFactory中获取到对应的BeanDefinition信息BeanDefinition catBeanDefinition = beanFactory.getBeanDefinition("cat");String beanClassName = catBeanDefinition.getBeanClassName();// 这里打印出:spring.demo.CatSystem.out.println(beanClassName);}
}

测试类

package spring.demo;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class SpringMain {public static void main(String[] args) {// 启动Spring容器AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);// 从容器中获取beanName为cat的beanObject catBean = applicationContext.getBean("cat");// 这里打印出:spring.demo.Cat@4310d43System.out.println(catBean);// 从容器中获取beanName为cat的beanObject dogBean = applicationContext.getBean("dog");// 这里打印出:spring.demo.Dog@54a7079eSystem.out.println(dogBean);}
}

http://www.dtcms.com/a/292676.html

相关文章:

  • PHP 8.0 超维意识编程终极指南(终篇)终极展望:PHP与宇宙意识融合跨维度架构模式超弦控制器增强版(1)
  • 最新植物大战僵尸杂交版最新版本2.5.1版,内置触屏+加速+全屏,附PC+安卓+iOS最全安装教程!
  • 阶段1--Linux中的文件服务器(FTP、NAS、SSH)
  • 前端_Javascript复习
  • 【C++】第十八节—一文万字详解 | map和set的使用
  • 网络安全第三次作业
  • Java学习第六十六部分——分布式系统架构
  • days32 :零基础学嵌入式之网络2.0
  • Coze智能体工作流:3分钟批量生成连贯人物一致的治愈图文
  • 远程调用图形浏览器(X11 转发)在 CentOS 7 上的安装操作
  • CentOS 7 安装nginx
  • 【LINUX】Centos 9使用nmcli更改IP
  • SpringBoot6-10(黑马)
  • linux-计划任务
  • 如何排查服务器 CPU 飙高
  • 本地大模型VRAM需求计算器:原理与实现详解
  • Spring Boot音乐服务器项目-上传音乐模块
  • [vue3] 自定义组件的v-model
  • Android ViewModel 深度解析:原理、使用与最佳实践
  • Android 中 实现日期选择功能(DatePickerDialog/MaterialDatePicker)
  • “鱼书”深度学习入门 笔记(2)第五章
  • MoonBit Meetup 杭州站丨 探讨AI基础软件的精彩回顾
  • API是什么,如何保障API安全?
  • 解决flex布局的元素高度超出父元素高度
  • AI网关是什么?为何而生?企业为什么需要AI网关?
  • 使用Kiro开发项目
  • SQL基础入门③ | 排序篇
  • 基于mysql云数据库创建和美化表格,对比分析Power BI和Quick BI的功能优劣
  • PACKET_HOST等宏定义介绍
  • 草稿!Linux网络系统总结!