专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

Spring Boot之ImportSelector

如何实现动态批量装载Bean?

ImportSelector介绍

ImportSelector这个接口不是有了springboot之后才有的,它是在org.springframework.context.annotation这个包下,随着spring-context包3.1版本发布的时候出现的。

用法和用途

其用途比较简单,可以根据启动的相关环境配置来决定让哪些类能够被Spring容器初始化。

举个例子:

一个是事务配置相关的TransactionManagementConfigurationSelector,其实现如下:

@Override
protected String[] selectImports(AdviceMode adviceMode) {
   switch (adviceMode) {
      case PROXY:
         return new String[] {AutoProxyRegistrar.class.getName(),
               ProxyTransactionManagementConfiguration.class.getName()};
      case ASPECTJ:
         return new String[] {determineTransactionAspectClass()};
      default:
         return null;
   }
}

总结下来的用法应该是这样:

1、 定义一个Annotation, Annotation中定义一些属性,到时候会根据这些属性的不同返回不同的class数组。
2、 在selectImports方法中,获取对应的Annotation的配置,根据不同的配置来初始化不同的class。
3、 实现ImportSelector接口的对象应该是在Annotation中由@Import Annotation来引入。这也就意味着,一旦启动了注解,那么就会实例化这个对象。

实践

比如我有一个需求,要根据当前环境是测试还是生产来决定我们的日志是输出到本地还是阿里云。

假设我定义两个logger :LoggerALoggerB ,两个logger实现 Logger接口。我们再定义一个Annotation。

Annotation(@EnableMyLogger)的定义:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import({LoggerSelector.class})
public @interface EnableMyLogger {
}

LoggerSelector的定义:

public class LoggerSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        List<String> classNames = new ArrayList<>();
        if (testEnv()) {
            classNames.add(LoggerA.class.getName());
        } else {
            classNames.add(LoggerB.class.getName());
        }
        return classNames.toArray(new String[0]);
    }

    private boolean testEnv() {
        return false;
    }
}

配置类定义:

@Configuration
@EnableMyLogger
public class SelectorConfig {
}

测试类定义:

public class MainTest {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SelectorConfig.class);
        System.out.println(applicationContext.getBean(Logger.class));
    }
}

测试结果:

74_1.png

事实证明,testEnv()方法返回值为false,确实LoggerA没有注入到容器,LoggerB注入到容器。

文章永久链接:https://tech.souyunku.com/26390

未经允许不得转载:搜云库技术团队 » Spring Boot之ImportSelector

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们