当前位置:网站首页>Importbeandefinitionregistrar registers beans with the container
Importbeandefinitionregistrar registers beans with the container
2022-06-22 04:38:00 【cristianoxm】
One 、ImportBeanDefinitionRegistrar
Let's take a look first ImportBeanDefinitionRegistrar What the hell is that? , Click to enter ImportBeanDefinitionRegistrar Source code , As shown below .
public interface ImportBeanDefinitionRegistrar {
default void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry,
BeanNameGenerator importBeanNameGenerator) {
registerBeanDefinitions(importingClassMetadata, registry);
}
default void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
}
}
You can see from the source code ,ImportBeanDefinitionRegistrar It's essentially an interface . stay ImportBeanDefinitionRegistrar Interface , There is one registerBeanDefinitions() Method , adopt registerBeanDefinitions() Method , We can approach Spring Register in container bean example .Spring The official is registering dynamically bean when , Most routines actually use ImportBeanDefinitionRegistrar Interface . all Classes that implement this interface will be ConfigurationClassPostProcessor Handle ,ConfigurationClassPostProcessor Realized BeanFactoryPostProcessor Interface , therefore ImportBeanDefinitionRegistrar Dynamic registration in bean Is prior to relying on it bean The initialization of the , Can also be aop、validator And so on .
Two 、 Usage method
ImportBeanDefinitionRegistrar Need to cooperate with @Import annotation ,@Import Annotation import implements ImportBeanDefinitionRegistrar The class of the interface .
- ImportBeanDefinitionRegistrar example
since ImportBeanDefinitionRegistrar It's an interface , Let's create a MyImportBeanDefinitionRegistrar class , Realization ImportBeanDefinitionRegistrar Interface , As shown below .
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar{
/** * AnnotationMetadata: Annotation information for the current class * BeanDefinitionRegistry:BeanDefinition Registration class * By calling BeanDefinitionRegistry Interface registerBeanDefinition() Method , You can add all that you need to add to the container bean Pour into the container . */
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
boolean dao1= registry.containsBeanDefinition("com.myImportBeanDefinitionRegistrar.MyDao1");
boolean dao2= registry.containsBeanDefinition("com.myImportBeanDefinitionRegistrar.MyDao2");
if (dao2 && dao1){
BeanDefinition beanDefinition = new RootBeanDefinition(MyDao3.class);
registry.registerBeanDefinition("myDao3", beanDefinition);
}
}
}
public class MyDao(1/2/3) {
public void query(){
System.out.println("query MyDao for MyImportSelect");
}
}
@Import({
MyDao1.class,MyDao2.class})
@EnableMyImportBeanDefinitionRegistrar
public class AppConfig {
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(MyImportBeanDefinitionRegistrar.class)
public @interface EnableMyImportBeanDefinitionRegistrar {
}
public class MyTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
Arrays.stream(ctx.getBeanDefinitionNames()).toList().forEach(System.out::println);
}
}
adopt EnableMyImportBeanDefinitionRegistrar annotation , We judge according to the conditions , Dynamic introduction MyDao3.class
3、 ... and 、 The same goes for most frameworks
- openFeign
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(FeignClientsRegistrar.class)
public @interface EnableFeignClients {
}
FeignClientsRegistrar Realized ImportBeanDefinitionRegistrar Interface
- mybatis
@Retention(RetentionPolicy.RUNTIME)
@Target({
ElementType.TYPE})
@Documented
@Import({
MapperScannerRegistrar.class})
@Repeatable(MapperScans.class)
public @interface MapperScan {
}
MapperScannerRegistrar Realized ImportBeanDefinitionRegistrar Interface
- There are also implementations importSelector Interface , Injected by returning an array containing the class name
边栏推荐
- IDEA藍屏的解决方案
- Write the first C application -- Hello, C
- The beta version of move protocol is stable, and it is temporarily decided to expand the scale of the prize pool
- kubernetes集群中工作节点Noready故障处理
- Lua exports as an external link library and uses
- Code example of map and entity class Interoperation
- Customized plug-ins in Cordova project -- plug-in creation process
- With these websites, do you still worry about job hopping without raising your salary?
- NFT mall building digital collection mall building digital collection market digital collection development company
- Take you to develop an efficiency enhancing tool -- vscode plug-in
猜你喜欢

爬梯子&&卖卖股份的最佳时期(跑路人笔记)

使用Echart绘制3D饼环图、仪表盘、电池图

active RM机子断电后,RM HA切换正常。但是YarnUI上查看不到集群资源,application也一直处于ACCEPTED状态。

New chief maintenance personnel for QT project

PCM data format

Golang為什麼不推薦使用this/self/me/that/_this

The best time to climb a ladder & sell shares (notes of the runner)

DFS of graph

Odoo 开发手册(一)接触 odoo 的第二天...

PCM数据格式
随机推荐
Insert sort
Es cannot work, circuitbreakingexception
System V IPC and POSIX IPC
Lightweight CNN design skills
网页设计与制作期末大作业报告——小众音乐网站
Storage structure of tree
IDEA蓝屏的解决方案
Golang为什么不推荐使用this/self/me/that/_this
New chief maintenance personnel for QT project
Cloud native enthusiast weekly: Chaos mesh upgraded to CNCF incubation project
中闽在线:以“积分”为纽带 共享线上渠道资源
mongo模糊查詢,帶有特殊字符需要轉義,再去查詢
What is the value of the FC2 new domain name? How to resolve to a website?
Researcher of Shangtang intelligent medical team interprets organ image processing under intelligent medical treatment
【sdx12】使用QCMAP_CLI启动WiFi操作说明
Case driven: a detailed guide from getting started to mastering shell programming
It is easy to analyze and improve R & D efficiency by understanding these five figures
Low power radar sensing module, application of smart lock radar sensing scheme, smart radar sensor technology
Thinkphp 的sesssion在同一个控制器不同的方法无法获取session的原因和对策
Calculation of audio frame size