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

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介:

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

写在前面
我们可以将一些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();
AI 代码解读

}
从源码里可以看出@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);
AI 代码解读

}
运行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

原文地址https://www.cnblogs.com/binghe001/p/13089993.html

目录
打赏
0
0
0
0
2
分享
相关文章
Spring IoC容器的设计与实现
Spring 是一个功能强大且模块化的 Java 开发框架,其核心架构围绕 IoC 容器、AOP、数据访问与集成、Web 层支持等展开。其中,`BeanFactory` 和 `ApplicationContext` 是 Spring 容器的核心组件,分别定位为基础容器和高级容器,前者提供轻量级的 Bean 管理,后者扩展了事件发布、国际化等功能。
保姆级Spring AI 注解式开发教程,你肯定想不到还能这么玩!
这是一份详尽的 Spring AI 注解式开发教程,涵盖从环境配置到高级功能的全流程。Spring AI 是 Spring 框架中的一个模块,支持 NLP、CV 等 AI 任务。通过注解(如自定义 `@AiPrompt`)与 AOP 切面技术,简化了 AI 服务集成,实现业务逻辑与 AI 基础设施解耦。教程包含创建项目、配置文件、流式响应处理、缓存优化及多任务并行执行等内容,助你快速构建高效、可维护的 AI 应用。
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于注解的整合
本文介绍了Spring Boot集成MyBatis的两种方式:基于XML和注解的形式。重点讲解了注解方式,包括@Select、@Insert、@Update、@Delete等常用注解的使用方法,以及多参数时@Param注解的应用。同时,针对字段映射不一致的问题,提供了@Results和@ResultMap的解决方案。文章还提到实际项目中常结合XML与注解的优点,灵活使用两者以提高开发效率,并附带课程源码供下载学习。
98 0
Spring Boot的核心注解是哪个?他由哪几个注解组成的?
Spring Boot的核心注解是@SpringBootApplication , 他由几个注解组成 : ● @SpringBootConfiguration: 组合了- @Configuration注解,实现配置文件的功能; ● @EnableAutoConfiguration:打开自动配置的功能,也可以关闭某个自动配置的选项 ● @ComponentScan:Spring组件扫描
Spring MVC常用的注解
@RequestMapping:用于处理请求 url 映射的注解,可用于类或方法上。用于类上,则表示类中 的所有响应请求的方法都是以该地址作为父路径。 @RequestBody:注解实现接收http请求的json数据,将json转换为java对象。 @ResponseBody:注解实现将conreoller方法返回对象转化为json对象响应给客户。 @Controller:控制器的注解,表示是表现层,不能用用别的注解代替 @RestController : 组合注解 @Conntroller + @ResponseBody @GetMapping , @PostMapping , @Put
在Docker容器中部署GitLab服务器的步骤(面向Ubuntu 16.04)
现在,你已经成功地在Docker上部署了GitLab。这就是我们在星际中的壮举,轻松如同土豆一样简单!星际旅行结束,靠岸,打开舱门,迎接全新的代码时代。Prepare to code, astronaut!
89 12
Docker网关冲突导致容器启动网络异常解决方案
当执行`docker-compose up`命令时,服务器网络可能因Docker创建新网桥导致IP段冲突而中断。原因是Docker默认的docker0网卡(172.17.0.1/16)与宿主机网络地址段重叠,引发路由异常。解决方法为修改docker0地址段,通过配置`/etc/docker/daemon.json`调整为非冲突段(如192.168.200.1/24),并重启服务。同时,在`docker-compose.yml`中指定网络模式为`bridge`,最后通过检查docker0地址、网络接口列表及测试容器启动验证修复效果。
容器技术实践:在Ubuntu上使用Docker安装MySQL的步骤。
通过以上的操作,你已经步入了Docker和MySQL的世界,享受了容器技术给你带来的便利。这个旅程中你可能会遇到各种挑战,但是只要你沿着我们划定的路线行进,你就一定可以达到目的地。这就是Ubuntu、Docker和MySQL的灵魂所在,它们为你开辟了一条通往新探索的道路,带你亲身感受到了技术的力量。欢迎在Ubuntu的广阔大海中探索,用Docker技术引领你的航行,随时准备感受新技术带来的震撼和乐趣。
192 16
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等

登录插画

登录以查看您的控制台资源

管理云资源
状态一览
快捷访问