Spring(32)——ImportSelector介绍

本文介绍了Spring中的ImportSelector接口,它允许动态地引入配置类。通过示例展示了如何实现ImportSelector,以及如何结合自定义注解进行更灵活的bean定义扫描。在不使用ImportSelector时,可以通过直接定义bean或import达到类似效果,但ImportSelector适用于需要根据特定条件动态注册bean的场景。
摘要由CSDN通过智能技术生成

ImportSelector介绍

@Configuration标注的Class上可以使用@Import引入其它的配置类,其实它还可以引入org.springframework.context.annotation.ImportSelector实现类。ImportSelector接口只定义了一个selectImports(),用于指定需要注册为bean的Class名称。当在@Configuration标注的Class上使用@Import引入了一个ImportSelector实现类后,会把实现类中返回的Class名称都定义为bean。来看一个简单的示例,假设现在有一个接口HelloService,需要把所有它的实现类都定义为bean,而且它的实现类上是没有加Spring默认会扫描的注解的,比如@Component@Service等。

public interface HelloService {
   

    void doSomething();
    
}

public class HelloServiceA implements HelloService {
   

    @Override
    public void doSomething() {
   
        System.out.println("Hello A");
    }

}

public class HelloServiceB implements HelloService {
   

    @Override
    public void doSomething() {
   
        System.out.println("Hello B");
    }

}

现定义了一个ImportSelector实现类HelloImportSelector,直接指定了需要把HelloService接口的实现类HelloServiceA和HelloServiceB定义为bean。

public class HelloImportSelector implements ImportSelector {
   

    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
   
        return new String[] {
   HelloServiceA.class.getName(), HelloServiceB.class.getName()};
    }

}

然后定义了@Configuration配置类HelloConfiguration,指定了@Import的是HelloImportSelector。

@Configuration
@Import(HelloImportSelector.class)
public class HelloConfiguration {
   

}

这样当加载配置类HelloConfiguration的时候会一并把HelloServiceA和HelloServiceB注册为Spring bean。可以进行如下简单测试:

@ContextConfiguration(classes=HelloConfiguration.class)
@RunWith(SpringRunner.class)
public class HelloImportSelectorTest {
   

    @Autowired
    private List<HelloService> helloServices;
    
    @Test
    public void test() {
   
        this.helloServices.forEach(HelloService::doSomething);
    }
    
}

看到这里可能你会觉得其实它也没什么用,因为整一个ImportSelector实现类那么麻烦,还不如直接在HelloConfiguration中定义bean或者import。在不引入ImportSelector的情况下,下面的两种方式都可以达到相同的效果。

@Configuration
@Import({
   HelloServiceA.class, HelloServiceB.class})
public class HelloConfiguration {
   
    
}
@Configuration
public class HelloConfiguration {
   

    @Bean
    public HelloServiceA 
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值