深入浅出org.springframework.context.annotation.Import、ImportSelector、ImportBeanDefinitionRegistrar

Import

https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans

源码注释

Indicates one or more component classes to import — typically @Configuration classes.

标记要导入的一个或者多个组件类,尤其是被 @Configuration 标记的类。


Provides functionality equivalent to the element in Spring XML. Allows for importing @Configuration classes, ImportSelector and ImportBeanDefinitionRegistrar implementations, as well as regular component classes (as of 4.2; analogous to AnnotationConfigApplicationContext.register).

和 Spring XML中的 import 标签相同的功能。允许导入@Configuration 类,ImportSelector 和ImportBeanDefinitionRegistrar 的实现类,以及常规组件类()

AnnotationConfigApplicationContext.register源码

	@Override
	public void register(Class<?>... componentClasses) {
   
		Assert.notEmpty(componentClasses, "At least one component class must be specified");
		StartupStep registerComponentClass = this.getApplicationStartup().start("spring.context.component-classes.register")
				.tag("classes", () -> Arrays.toString(componentClasses));
		this.reader.register(componentClasses);
		registerComponentClass.end();
	}

If XML or other non-@Configuration bean definition resources need to be imported, use the @ImportResource annotation instead.

如果XML或者其他非@Configuration bean definition 资源需要导入,使用@ImportResource注解替代。

源码

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {
   	
	Class<?>[] value();
}

只有一个参数接收Class数组。

常见用法

DataSourceAutoConfiguration

	@Import({
    DataSourcePoolMetadataProvidersConfiguration.class,
	 DataSourceInitializationConfiguration.class })

	@Import(EmbeddedDataSourceConfiguration.class)
	
	@Import({
    DataSourceConfiguration.Hikari.class, DataSourceConfiguration.Tomcat.class,
			DataSourceConfiguration.Dbcp2.class, DataSourceConfiguration.OracleUcp.class,
			DataSourceConfiguration.Generic.class, DataSourceJmxConfiguration.class })

问题一:Import数组的顺序性

在DefaultListableBeanFactory类中preInstantiateSingletons方法中的beanNames列表中

### Spring Configuration 类的依赖项 对于在 Maven 项目中使用 Spring Framework 并希望利用 `@Configuration` 注解来定义配置类的情况,需要确保 POM 文件中有合适的依赖项。以下是针对 Spring Boot 和纯 Spring Framework 的两种情况。 #### 使用 Spring Boot Starter Parent 当基于 Spring Boot 构建应用程序时,推荐的方式是从父级继承自 `spring-boot-starter-parent` 或者导入 BOM (Bill of Materials),这可以简化管理版本号的过程并提供一组预设的最佳实践设置: ```xml <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.2</version> <!-- 版本应根据实际需求调整 --> <relativePath/> <!-- lookup parent from repository --> </parent> <!-- 添加 spring boot starter 配置支持 --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> </dependencies> ``` 上述代码片段展示了如何通过继承 `spring-boot-starter-parent` 来获取默认的构建约定以及一些常用的启动器依赖[^1]。 #### 单独引入 Spring Context Module 如果不打算采用整个 Spring Boot 生态系统,则可以直接添加来自 Spring Framework 的特定模块作为依赖。为了使 JavaConfig (`@Configuration`) 功能可用,至少要包含 `spring-context` 模块: ```xml <properties> <spring.version>6.0.9</spring.version> <!-- 设置所需的Spring框架版本 --> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> </dependencies> ``` 这段 XML 定义了一个属性 `${spring.version}` 来控制所使用的 Spring Framework 的具体版本,并显式指定了对 `spring-context` 组件的需求。 #### 处理可能遇到的问题 如果遇到依赖解析错误或长时间无法完成下载等问题,建议检查网络连接状况,确认本地仓库路径配置无误,并考虑切换至更稳定可靠的镜像源服务器以加速资源获取过程[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值