@Import注解中使用ImportSelector接口导入bean

1.ImportSelector接口概述

ImportSelector接口是Spring中导入外部配置的核心接口,在Spring Boot的自动化配置和@EnableXXX(功能性注解)都有它的存在。

public interface ImportSelector {

	String[] selectImports(AnnotationMetadata importingClassMetadata);

	@Nullable
	default Predicate<String> getExclusionFilter() {
		return null;
	}
}

其主要作用是收集需要导入的配置类,selectImports()方法的返回值就是我们向Spring容器中导入的类的全类名。如果该接口的实现类同时实现EnvironmentAware,BeanFactoryAware,BeanClassLoaderAware或者ResourceLoaderAware,那么在调用其selectImports()方法之前先调用上述接口中对应的方法,如果需要在所有的@Configuration处理完再导入时,那么可以实现DeferredImportSelector接口。

在ImportSelector接口的selectImports()方法中,存在一个AnnotationMetadata类型的参数,这个参数能够获取到当前标注@Import注解的类的所有注解信息,也就是说不仅能获取到@Import注解里面的信息,还能获取到其他注解的信息。

2.ImportSelector接口实例

创建一个MyImportSelector类实现ImportSelector接口,如下所示

package com.tianxia.springannotation.config.configImport;

import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;

/**
 * 自定义逻辑,返回需要导入的组件
 * @author liqb
 * @date 2023-04-23 17:43
 **/
public class MyImportSelector implements ImportSelector {

    /**
     * 导入组件
     * @author liqb
     * @date 2023-04-23 17:45
     * @param annotationMetadata 当前标注@Import注解的类的所有注解信息,也就是说不仅能获取到@Import注解里面的信息,还能获取到其他注解的信息
     * @return 返回值:就是要导入到容器中的组件的全类名
     */
    @Override
    public String[] selectImports(AnnotationMetadata annotationMetadata) {

        // 方法不要返回null值,否则会报空指针异常
        // 可以返回一个空数组
        // return new String[]{};
        return new String[]{"com.tianxia.springannotation.entity.Bule", "com.tianxia.springannotation.entity.Yellow"};
    }
}

使用MyImportSelector类要导入哪些bean,就需要你在MyImportSelector类的selectImports()方法中进行设置了,只须在MyImportSelector类的selectImports()方法中返回要导入的类的全类名(包名+类名)。

创建实体类

package com.tianxia.springannotation.entity;

/**
 * Blue类
 * @author liqb
 * @date 2023-04-23 17:47
 **/
public class Bule {
}
package com.tianxia.springannotation.entity;

/**
 * Yellow 类
 * @author liqb
 * @date 2023-04-23 17:48
 **/
public class Yellow {
}

修改配置类,在@Import注解中,导入MyImportSelector类

package com.tianxia.springannotation.config;

import com.tianxia.springannotation.config.configEntity.CustomerScope;
import com.tianxia.springannotation.config.configImport.MyImportSelector;
import com.tianxia.springannotation.entity.Color;
import com.tianxia.springannotation.entity.Person;
import com.tianxia.springannotation.entity.Red;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Scope;

/**
 * 配置类
 * @author liqb
 * @date 2023-04-23 10:45
 **/
@Configuration
// @Import快速地导入组件,id默认是组件的全类名
@Import({Color.class, Red.class, MyImportSelector.class})
public class MainConfig03 {

    /**
     * 创建person实例
     * @author liqb
     * @date 2023-04-23 09:46
     * @return
     */
    @Bean("person03")
    @Scope(CustomerScope.CUSTOMER_SCOPE)
    public Person person() {
        System.out.println("给容器中添加咱们这个Person对象...");
        return new Person("liqb", 24);
    }
}

运行测试方法

/**
 * 测试Import
 * @author liqb
 * @date 2023-04-23 15:38
 */
@Test
public void testImport() {
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig03.class);
    String[] definitionNames = applicationContext.getBeanDefinitionNames();
    for (String name : definitionNames) {
        System.out.println(name);
    }
}

输出的结果信息如下所示:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值