写在前面

我们可以将一些bean组件交由Spring管理,并且Spring支持单实例bean和多实例bean。我们自己写的类,可以通过包扫描+标注注解(@Controller、@Servcie、@Repository、@Component)的形式将其注册到IOC容器中,如果不是我们自己写的类,比如,我们在项目中引入了一些第三方的类库,此时,我们需要将这些第三方类库中的类注册到Spring容器中,该怎么办呢?此时,我们就可以使用@Bean和@Import注解将这些类快速的导入Spring容器中。接下来,我们来一起探讨下如何使用@Import注解给容器中快速导入一个组件。

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

注册bean的方式

向Spring容器中注册bean通常有以下几种方式:

  • 包扫描+标注注解(@Controller、@Servcie、@Repository、@Component),通常用于自己写的类。
  • @Bean注解,通常用于导入第三方包中的组件。
  • @Import注解,快速向Spring容器中导入组件。

@Import注解概述

Spring 3.0之前,创建Bean可以通过xml配置文件与扫描特定包下面的类来将类注入到Spring IOC容器内。而在Spring 3.0之后提供了JavaConfig的方式,也就是将IOC容器里Bean的元信息以java代码的方式进行描述。我们可以通过@Configuration与@Bean这两个注解配合使用来将原来配置在xml文件里的bean通过java代码的方式进行描述

@Import注解提供了@Bean注解的功能,同时还有xml配置文件里标签组织多个分散的xml文件的功能,当然在这里是组织多个分散的@Configuration

先看一下@Import注解的源码:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {
/**
* {@link Configuration}, {@link ImportSelector}, {@link ImportBeanDefinitionRegistrar}
* or regular component classes to import.
*/
Class<?>[] value();
}

从源码里可以看出@Import可以配合 Configuration , ImportSelector, ImportBeanDefinitionRegistrar 来使用,下面的or表示也可以把Import当成普通的Bean使用。

@Import只允许放到类上面,不能放到方法上。下面我们来看具体的使用方式。

@Import注解的使用方式

@Import注解的三种用法主要包括:

  • 直接填class数组方式
  • ImportSelector方式【重点】
  • ImportBeanDefinitionRegistrar方式

注意:我们先来看第一种方法:直接填class数组的方式,其他的两种方式我们后面继续讲。

@Import导入组件的简单示例

没有使用@Import注解的效果

首先,我们创建一个Department类,这个类是一个空类,没有成员变量和方法,如下所示。

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

/**
* @author binghe
* @version 1.0.0
* @description 测试@Import注解的bean
*/
public class Department {
}

接下来,我们先在SpringBeanTest类中创建testAnnotationConfig7()方法,输出Spring容器中所有的bean,来查看是否存在Department类对应的bean实例,以此来判断Spring容器中是否注册有Department类对应的bean实例。

@Test
public void testAnnotationConfig7(){
ApplicationContext context = new AnnotationConfigApplicationContext(PersonConfig2.class);
String[] names = context.getBeanDefinitionNames();
Arrays.stream(names).forEach(System.out::println);
}

运行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
person
binghe001

可以看到Spring容器中并没有Department类对应的bean实例。

使用@Import注解的效果

我们在PersonConfig2类上添加@Import注解,并将Department类标注到注解中,如下所示。

@Configuration
@Import(Department.class)
public class PersonConfig2 {

此时,我们再次运行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
person
binghe001

可以看到,输出结果中打印了io.mykit.spring.plugins.register.bean.Department,说明使用@Import导入bean时,id默认是组件的全类名。

@Import注解支持同时导入多个类,例如,我们再次创建一个Employee类,如下所示。

package io.mykit.spring.plugins.register.bean;
/**
* @author binghe
* @version 1.0.0
* @description 测试@Import注解的bean
*/
public class Employee {
}

接下来,我们也将Employee类添加到@Import注解中,如下所示。

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

此时,我们再次运行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
person
binghe001

可以看到,结果信息中同时输出了io.mykit.spring.plugins.register.bean.Department和io.mykit.spring.plugins.register.bean.Employee,说明Department类的bean实例和Employee类的bean实例都导入到Spring容器中了。

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

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

写在最后

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

【Spring注解驱动开发】使用@Import注解给容器中快速导入一个组件的更多相关文章

  1. 【Spring注解驱动开发】在@Import注解中使用ImportSelector接口导入bean

    写在前面 在上一篇关于Spring的@Import注解的文章<[Spring注解驱动开发]使用@Import注解给容器中快速导入一个组件>中,我们简单介绍了如何使用@Import注解给容器 ...

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

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

  3. 0、Spring 注解驱动开发

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

  4. 【spring 注解驱动开发】spring组件注册

    尚学堂spring 注解驱动开发学习笔记之 - 组件注册 组件注册 1.@Configuration&@Bean给容器中注册组件 2.@ComponentScan-自动扫描组件&指定扫 ...

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

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

  6. 【Spring注解驱动开发】二狗子让我给他讲讲@EnableAspectJAutoProxy注解

    写在前面 最近,二狗子入职了新公司,新入职的那几天确实有点飘.不过慢慢的,他发现他身边的人各个身怀绝技啊,有Spring源码的贡献者,有Dubbo源码的贡献者,有MyBatis源码的贡献者,还有研究A ...

  7. Spring注解驱动开发(一)-----组件注册

    注册bean xml方式 1.beans.xml-----很简单,里面注册了一个person bean <?xml version="1.0" encoding=" ...

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

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

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

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

随机推荐

  1. Scrapy 框架 入门教程

    Scrapy入门教程 在本篇教程中,我已经安装好Scrapy 本篇教程中将带您完成下列任务: 创建一个Scrapy项目 定义提取的Item 编写爬取网站的 spider 并提取 Item 编写 Ite ...

  2. 异步http接口调用库:httpx

    谈到http接口调用,Requests大家并不陌生,例如,robotframework-requests.HttpRunner等HTTP接口测试库/框架都是基于它开发.这里将介绍另一款http接口测试 ...

  3. python3.x 基础三:set集合

    集合,set(),记住: 1个特点:去重,把列表变成集合,达到自动去重操作,无序 5个关系:测试两个列表的交差并子反向差集 方法: |  add(...) 常用,已存在元素去重不生效 |      A ...

  4. SQL SERVER sa无法登陆的问题

    安装的时候选择了 Windows 身份验证模式,只能windows内置账户登录解决方法:先登录(SQL Server Management Studio ),点服务器,右键->属性->安全 ...

  5. Robot Framework(2)- 快速安装

    如果你还想从头学起Robot Framework,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1770899.html 安装RF cmd ...

  6. JS中的bind 、call 、apply

    # 一 .bind 特点: ### 1.返回原函数的拷贝,我们称这个拷贝的函数为绑定函数 ### 2.将函数中的this固定为调用bind方法时的第一个参数,所以称之为绑定函数.注意是名词而非动词. ...

  7. 对于近似有序序列(即除掉少数K个元素后是有序序列且K<<n),试分析直接插入排序,冒牌排序和简单选择排序的时间复杂度

    学弟问的一道数据结构的题,关于一些排序算法的时间复杂度. 针对近似有序序列, ①当使用直接插入排序时,其基本操作为数组中元素的移动.最好情况下,待排序列有序,无需移动,此时时间复杂度为O(n), 当为 ...

  8. php实用正则

    1 Email地址:^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$ 2 域名:[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z ...

  9. Kubernetes Dashborad 搭建

    需求 基于网页查看Kubernetes 用户管理界面 安装步骤 在控制面板节点部署dashborad kubectl apply -f https://raw.githubusercontent.co ...

  10. 【Elasticsearch学习】DSL搜索大全(持续更新中)

    1.复合查询 复合查询能够组合其他复合查询或者查询子句,同时也可以组合各个查询的查询结果及得分,也可以从Query查询转换为Filter过滤器查询. 首先介绍一下Query Context和 Filt ...