Spring系列——@Import、ImportSelector接口

本文介绍了Spring中使用@Import注解和ImportSelector接口注册组件的方法。通过@Import可以将类直接导入Spring容器,而ImportSelector可以通过自定义逻辑选择需要导入的组件。文章提供了详细的源码解析和实战示例,展示了如何使用这两个特性来注册并管理Bean。
摘要由CSDN通过智能技术生成

Spring注解之@Import

前言

大家使用的最多的给Spring注册组件的方式,大概是以下两种把。

  • 包扫描+注解的方式(@Controller、@Service、@Reposity、@Comment)
  • 使用@Configuration+@Bean的方式

这两种使用的区别:

  • 第一种只能注册我们自己写的组件
  • 而第二种可以注册第三方jar包里面的组件

接下来我来介绍第三种方式使用@Import来给Spring注册组件

@Import源码

/**
 * Indicates one or more <em>component classes</em> to import &mdash; typically
 * {@link Configuration @Configuration} classes.
 *
 * <p>Provides functionality equivalent to the {@code <import/>} element in Spring XML.
 * Allows for importing {@code @Configuration} classes, {@link ImportSelector} and
 * {@link ImportBeanDefinitionRegistrar} implementations, as well as regular component
 * classes (as of 4.2; analogous to {@link AnnotationConfigApplicationContext#register}).
 *
 * <p>{@code @Bean} definitions declared in imported {@code @Configuration} classes should be
 * accessed by using {@link org.springframework.beans.factory.annotation.Autowired @Autowired}
 * injection. Either the bean itself can be autowired, or the configuration class instance
 * declaring the bean can be autowired. The latter approach allows for explicit, IDE-friendly
 * navigation between {@code @Configuration} class methods.
 *
 * <p>May be declared at the class level or as a meta-annotation.
 *
 * <p>If XML or other non-{@code @Configuration} bean definition resources need to be
 * imported, use the {@link ImportResource @ImportResource} annotation instead.
 *
 * @author Chris Beams
 * @author Juergen Hoeller
 * @since 3.0
 * @see Configuration
 * @see ImportSelector
 * @see ImportBeanDefinitionRegistrar
 * @see ImportResource
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {
   

	/**
	 * {@link Configuration @Configuration}, {@link ImportSelector},
	 * {@link ImportBeanDefinitionRegistrar}, or regular component classes to import.
	 */
	Class<?>[] value();

}

从源码中我们得知,这个注解是作用在类上面的并且注解能保留在运行期间。value属性的类型是Class
从注释中我们可以得知,@Import是为了提供与Xml配置中的import标签一样的功能,通过value属性注入组件,允许通过它引入 @Configuration 注解的类,引入ImportSelector接口,或者 ImportBeanDefinitionRegistrar进行注册组件。

@Improt实战

  • Bean
package org.example.imports;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dog {
   
    private String name;
}
  • 配置类
package org.example.imports;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Import(
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot中,@Import注解用于导入其他配置类或者配置文件。它的作用是将其他配置类或者配置文件中的bean导入到当前的配置类中。通常情况下,我们可以使用@Import注解来导入以下类型的配置类或者配置文件: 1.其他@Configuration注解标注的配置类 2.@Enable*注解标注的配置类 3.@ImportSelector接口的实现类 4.@ImportBeanDefinitionRegistrar接口的实现类 使用@Import注解的语法如下: ``` @Configuration @Import({OtherConfig.class, AnotherConfig.class}) public class AppConfig { // ... } ``` 在上面的例子中,我们使用@Import注解来导入了两个其他的配置类:OtherConfig和AnotherConfig。这些配置类中定义的bean将会被自动注册到当前的配置类中。 除此之外,@Import注解还可以用来动态装配bean,例如: ``` @Configuration public class AppConfig { @Bean public MyBean myBean() { return new MyBean(); } @Import(MyImportSelector.class) public static class MyConfig {} public static class MyImportSelector implements ImportSelector { @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { return new String[] { "com.example.AnotherBean" }; } } } ``` 在上面的例子中,我们定义了一个MyBean的bean,并且使用@Import注解来导入了一个实现了ImportSelector接口的MyImportSelector类。这个类会动态地返回一个字符串数组,包含了要导入的bean的类名。在这个例子中,我们导入了另一个名为AnotherBean的bean。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值