Spring扩展点之ImportBeanDefinitionRegistrar

开发 架构
根据给定的注释元数据,根据需要注册Bean定义......Spring会遍历所有的BeanDefinition,逐个创建对应的Bean。

一、是什么?

查看接口注释:根据给定的注释元数据,根据需要注册bean定义......spring会遍历所有的beanDefinition,逐个创建对应的bean。

public interface ImportBeanDefinitionRegistrar {
	/**
	 * Register bean definitions as necessary based on the given annotation metadata of
	 * the importing {@code @Configuration} class.......(截取部分注释)
	 */
	default void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry,
			BeanNameGenerator importBeanNameGenerator) {
		registerBeanDefinitions(importingClassMetadata, registry);
	}
	default void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
	}
}

PS:创建bean还有哪些方式?

1、@Component、@Bean等等
2、@Import(导入类)
3、实现ImportSelector接口,重写selectImports方法,返回需要导入的全类名
4、实现ImportBeanDefinitionRegistrar接口,注册bean定义

二、demo案例

StudentBean:学生类。

public class StudentBean {
    private String stuName;
    public String getStuName() {
        return stuName;
    }
    public void setStuName(String stuName) {
        this.stuName = stuName;
    }
}

StudentImportBeanDefinitionRegistrar:实现ImportBeanDefinitionRegistrar接口。

@Component
public class StudentImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        // 创建beanDefinitionBuilder
        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(StudentBean.class);
        beanDefinitionBuilder.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
        // 获取beanDefinition
        AbstractBeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();
        BeanDefinitionHolder beanDefinitionHolder = new BeanDefinitionHolder(beanDefinition, StudentBean.class.getSimpleName());
        // 注册beanDefinition
        BeanDefinitionReaderUtils.registerBeanDefinition(beanDefinitionHolder, registry);
    }
}

启动类:使用@Import导入StudentImportBeanDefinitionRegistrar类。

@Import(StudentImportBeanDefinitionRegistrar.class)
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(DemoApplication.class, args);
        StudentBean studentBean = applicationContext.getBean(StudentBean.class);
        System.out.println("studentBean: " + studentBean);
    }
}

我们大都习惯使用@Enable***方式引入某个组件,此处可稍作改造。

新增注解:EnableStudentBean。

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(StudentImportBeanDefinitionRegistrar.class)
public @interface EnableStudentBean {
}

启动类修改:

@EnableStudentBean
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(DemoApplication.class, args);
        StudentBean studentBean = applicationContext.getBean(StudentBean.class);
        System.out.println("studentBean: " + studentBean);
    }
}

三、ImportBeanDefinitionRegistrar在其他开源项目中的使用

mybatis

在使用mybatis时,我们需要指定mapper的扫描路径:

@MapperScan(basePackages = "com.test.demo.dao.mapper")

查看@MapperScan源码,发现导入了MapperScannerRegistrar类,该类实现了ImportBeanDefinitionRegistrar接口。

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(MapperScannerRegistrar.class)
@Repeatable(MapperScans.class)
public @interface MapperScan

MapperScannerRegistrar类(截取部分代码):

public class MapperScannerRegistrar implements ImportBeanDefinitionRegistrar {
  @Override
  public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
    // 获取MapperScan注解
    AnnotationAttributes mapperScanAttrs = AnnotationAttributes
        .fromMap(importingClassMetadata.getAnnotationAttributes(MapperScan.class.getName()));
    if (mapperScanAttrs != null) {
      // 扫描对应的mapper接口,并注册BeanDefinition(bean定义)
      registerBeanDefinitions(importingClassMetadata, mapperScanAttrs, registry,
          generateBaseBeanName(importingClassMetadata, 0));
    }
  }
}

Openfeign

在使用OpenFeign时,我们需要指定feign接口扫描路径:

@EnableFeignClients(basePackages = ""com.test.demo")

查看@EnableFeignClients源码,发现导入了FeignClientsRegistrar类,该类实现了ImportBeanDefinitionRegistrar接口。

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(FeignClientsRegistrar.class)
public @interface EnableFeignClients

FeignClientsRegistrar类(截取部分代码):

class FeignClientsRegistrar	implements ImportBeanDefinitionRegistrar {
	@Override
	public void registerBeanDefinitions(AnnotationMetadata metadata,BeanDefinitionRegistry registry) {
		registerDefaultConfiguration(metadata, registry);
    registerFeignClients(metadata, registry);
	}
}

扫描所有加了@FeignClient注解的接口,接着注册FeignClientFactoryBean类型的BeanDefinition到容器中,需要使用时生成具体的接口代理实现服务调用。

责任编辑:姜华 来源: 今日头条
相关推荐

2021-04-12 06:09:38

Spring扩展点应用

2023-11-28 08:01:25

2023-12-01 07:28:40

SpringbootBean

2022-05-30 09:32:07

Spring容器

2023-11-24 08:00:00

2017-05-09 10:34:21

Spring BootDubbo Activ扩展

2017-04-28 08:32:40

Spring BootDubbo Activ使用

2023-11-27 07:26:42

Springboot容器

2023-12-05 07:48:23

SpringBoot

2022-09-16 11:41:17

Spring代码

2024-02-01 08:28:28

2009-08-13 18:00:48

Eclipse重构功能扩展点

2011-12-05 13:44:34

JavaSpringMVC

2023-01-30 11:35:14

SpringJava

2009-08-28 13:57:29

virtual ove扩展点

2022-06-07 07:58:45

SpringSpring AOP

2010-09-13 15:06:36

2016-05-30 17:31:34

Spring框架

2009-12-24 17:10:42

WPF动画类

2011-12-08 09:01:59

JavaSpring
点赞
收藏

51CTO技术栈公众号