写在前面

在上一篇关于Spring的@Import注解的文章《【Spring注解驱动开发】使用@Import注解给容器中快速导入一个组件》中,我们简单介绍了如何使用@Import注解给容器中快速导入一个组件,而我们知道,@Import注解总共包含三种使用方法,分别为:直接填class数组方式;ImportSelector方法(重点);ImportBeanDefinitionRegistrar方式。那么,今天,我们就一起来学习关于@Import注解非常重要的第二种方式:ImportSelector方式。

项目工程源码已经提交到GitHub:https://github.com/sunshinelyz/spring-annotation

ImportSelector接口概述

ImportSelector接口是至spring中导入外部配置的核心接口,在SpringBoot的自动化配置和@EnableXXX(功能性注解)都有它的存在。我们先来看一下ImportSelector接口的源码,如下所示。

package org.springframework.context.annotation;

import java.util.function.Predicate;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.lang.Nullable; public interface ImportSelector {
String[] selectImports(AnnotationMetadata importingClassMetadata);
@Nullable
default Predicate<String> getExclusionFilter() {
return null;
}
}

该接口文档上说的明明白白,其主要作用是收集需要导入的配置类,selectImports()方法的返回值就是我们向Spring容器中导入的类的全类名。如果该接口的实现类同时实现EnvironmentAware, BeanFactoryAware ,BeanClassLoaderAware或者ResourceLoaderAware,那么在调用其selectImports方法之前先调用上述接口中对应的方法,如果需要在所有的@Configuration处理完在导入时可以实现DeferredImportSelector接口。

在ImportSelector接口的selectImports()方法中,存在一个AnnotationMetadata类型的参数,这个参数能够获取到当前标注@Import注解的类的所有注解信息。

注意:如果ImportSelector接口展开讲的话,可以单独写一篇文章,那我就放在下一篇文章中讲吧,这里就不赘述了,嘿嘿。

ImportSelector接口实例

首先,我们创建一个MyImportSelector类实现ImportSelector接口,如下所示。

package io.mykit.spring.plugins.register.selector;

import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
/**
* @author binghe
* @version 1.0.0
* @description 测试@Import注解中使用ImportSelector
* 自定义逻辑,返回需要导入的组件
*/
public class MyImportSelector implements ImportSelector {
/**
* 返回值为需要导入到容器中的bean的全类名数组
* AnnotationMetadata:当前标注@Import注解的类的所有注解信息
*/
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[0];
}
}

接下来,我们在PersonConfig2类的@Import注解中,导入MyImportSelector类,如下所示。

@Configuration
@Import({Department.class, Employee.class, MyImportSelector.class})
public class PersonConfig2 {

至于使用MyImportSelector导入哪些bean,就需要在MyImportSelector类的selectImports()方法中进行设置了,只要在MyImportSelector类的selectImports()方法中返回要导入的类的全类名(包名+类名)即可。

我们继承创建两个Java bean对象,分别为User和Role,如下所示。

  • User类
package io.mykit.spring.plugins.register.bean;
/**
* @author binghe
* @version 1.0.0
* @description 测试ImportSelector
*/
public class User {
}
  • Role类
package io.mykit.spring.plugins.register.bean;
/**
* @author binghe
* @version 1.0.0
* @description 测试ImportSelector
*/
public class Role {
}

接下来,我们将User类和Role类的全类名返回到MyImportSelector类的selectImports()方法中,此时,MyImportSelector类的selectImports()方法如下所示。

/**
* 返回值为需要导入到容器中的bean的全类名数组
* AnnotationMetadata:当前标注@Import注解的类的所有注解信息
*/
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{
User.class.getName(),
Role.class.getName()
};
}

接下来,我们运行SpringBeanTest类的testAnnotationConfig7()方法,输出的结果信息如下所示。

org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
personConfig2
io.mykit.spring.plugins.register.bean.Department
io.mykit.spring.plugins.register.bean.Employee
io.mykit.spring.plugins.register.bean.User
io.mykit.spring.plugins.register.bean.Role
person
binghe001

可以看到,输出结果中多出了io.mykit.spring.plugins.register.bean.User和io.mykit.spring.plugins.register.bean.Role。

说明使用ImportSelector已经成功将User类和Role类导入到了Spring容器中。

好了,咱们今天就聊到这儿吧!别忘了给个在看和转发,让更多的人看到,一起学习一起进步!!

项目工程源码已经提交到GitHub:https://github.com/sunshinelyz/spring-annotation

写在最后

如果觉得文章对你有点帮助,请微信搜索并关注「 冰河技术 」微信公众号,跟冰河学习Spring注解驱动开发。公众号回复“spring注解”关键字,领取Spring注解驱动开发核心知识图,让Spring注解驱动开发不再迷茫。

【Spring注解驱动开发】在@Import注解中使用ImportSelector接口导入bean的更多相关文章

  1. Spring注解驱动开发04(给容器中注册组件的方式)

    给容器中注册组件的方式 1. 组件注解标注 + 包扫描(适用于自己写的类) //控制层组件 @Controller public class PersonController { } //业务逻辑层组 ...

  2. 【Spring注解驱动开发】在@Import注解中使用ImportBeanDefinitionRegistrar向容器中注册bean

    写在前面 在前面的文章中,我们学习了如何使用@Import注解向Spring容器中导入bean,可以使用@Import注解快速向容器中导入bean,小伙伴们可以参见<[Spring注解驱动开发] ...

  3. 【Spring注解驱动开发】使用@Import注解给容器中快速导入一个组件

    写在前面 我们可以将一些bean组件交由Spring管理,并且Spring支持单实例bean和多实例bean.我们自己写的类,可以通过包扫描+标注注解(@Controller.@Servcie.@Re ...

  4. 【String注解驱动开发】如何按照条件向Spring容器中注册bean?这次我懂了!!

    写在前面 当bean是单实例,并且没有设置懒加载时,Spring容器启动时,就会实例化bean,并将bean注册到IOC容器中,以后每次从IOC容器中获取bean时,直接返回IOC容器中的bean,不 ...

  5. 【Spring注解驱动开发】面试官:如何将Service注入到Servlet中?朋友又栽了!!

    写在前面 最近,一位读者出去面试前准备了很久,信心满满的去面试.没想到面试官的一个问题把他难住了.面试官的问题是这样的:如何使用Spring将Service注入到Servlet中呢?这位读者平时也是很 ...

  6. 【String注解驱动开发】面试官让我说说:如何使用FactoryBean向Spring容器中注册bean?

    写在前面 在前面的文章中,我们知道可以通过多种方式向Spring容器中注册bean.可以使用@Configuration结合@Bean向Spring容器中注册bean:可以按照条件向Spring容器中 ...

  7. 0、Spring 注解驱动开发

    0.Spring注解驱动开发 0.1 简介 <Spring注解驱动开发>是一套帮助我们深入了解Spring原理机制的教程: 现今SpringBoot.SpringCloud技术非常火热,作 ...

  8. 【Spring注解驱动开发】组件注册-@ComponentScan-自动扫描组件&指定扫描规则

    写在前面 在实际项目中,我们更多的是使用Spring的包扫描功能对项目中的包进行扫描,凡是在指定的包或子包中的类上标注了@Repository.@Service.@Controller.@Compon ...

  9. 【Spring注解驱动开发】自定义TypeFilter指定@ComponentScan注解的过滤规则

    写在前面 Spring的强大之处不仅仅是提供了IOC容器,能够通过过滤规则指定排除和只包含哪些组件,它还能够通过自定义TypeFilter来指定过滤规则.如果Spring内置的过滤规则不能够满足我们的 ...

随机推荐

  1. xv6 操作系统的环境搭建

    xv6 是 MIT 设计的一个教学型操纵系统.xv6 可在 Intel X86 框架上运行,为了方便,建议将 xv6 运行在 QEMU 虚拟机器上,本人的实验环境是 ubuntu 18.04 . 1. ...

  2. hibernate 异常分析:java.lang.NoClassDefFoundError: org/hibernate/Session

    原因: NoClassDefFoundError的含义就是说编译器找不到org/hibernate/Session这个类的定义 解决方法: 1.检查java中是否导入hibernate 包 impor ...

  3. 【python 爬虫】fake-useragent Maximum amount of retries reached解决方案

    前言 在用fake-useragent的时候发生报错,fake_useragent.errors.FakeUserAgentError: Maximum amount of retries reach ...

  4. MySql 语言分类

    (1)数据定义语言,即SQL DDL,用于定义SQL模式.基本表.视图.索引等结构.(2)数据操纵语言,即SQL DML.数据操纵分成数据查询和数据更新两类.(3)数据查询语言,即SQL DQL.(4 ...

  5. C#判断网址的可访问性

    /// <summary> /// 判断网址是否可以访问 /// </summary> /// <param name="Url"></p ...

  6. Asp.net Identity身份与权限体系设计

    1 Identity 介绍 2 授权系统 图1 体系结构 3 自定义 Attribute 自定义 Attribute 继承于 AuthorizeAttribute,AuthorizeAttribute ...

  7. vue-cli3区分开发和生产环境

    vue-cli3出来很久了,之前一直使用vue-cli2的配置,并且区分了生产和开发环境,自己的理解,环境分两大类,开发环境 和生产环境,最近升级到了vue-cli4当然改动并不大. 升级的主要原因嘛 ...

  8. Redis详解(十)------ 从零开始搭建集群

    在上一篇博客我们介绍了------Redis哨兵(Sentinel)模式,哨兵模式主要是解决高可用问题,在master节点宕机时,slave节点能够自动切换成为master节点 本篇博客我们来介绍Re ...

  9. GNS3内网配置虚拟机测试

    一.背景:考虑在GNS3内网配置一台虚拟机系统,用于大流量测试. 二.环境: 1.win10系统已安装:GNS3和配套GNS3VM虚拟机(版本号:2.2.7):vmware虚拟机winxp(名称为:W ...

  10. Beta冲刺 —— 5.28

    这个作业属于哪个课程 软件工程 这个作业要求在哪里 Beta冲刺 这个作业的目标 Beta冲刺 作业正文 正文 github链接 项目地址 其他参考文献 无 一.会议内容 1.组员一起学习Git分支管 ...