欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

Spring.ImportSelector接口

发布时间:2025/3/15 32 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Spring.ImportSelector接口 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

一、关于ImportSelector接口

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

二、ImportSelector接口源码

package org.springframework.context.annotation;import org.springframework.core.type.AnnotationMetadata;/*** Interface to be implemented by types that determine which @{@link Configuration}* class(es) should be imported based on a given selection criteria, usually one or more* annotation attributes.** <p>An {@link ImportSelector} may implement any of the following* {@link org.springframework.beans.factory.Aware Aware} interfaces, and their respective* methods will be called prior to {@link #selectImports}:* <ul>* <li>{@link org.springframework.context.EnvironmentAware EnvironmentAware}</li>* <li>{@link org.springframework.beans.factory.BeanFactoryAware BeanFactoryAware}</li>* <li>{@link org.springframework.beans.factory.BeanClassLoaderAware BeanClassLoaderAware}</li>* <li>{@link org.springframework.context.ResourceLoaderAware ResourceLoaderAware}</li>* </ul>** <p>ImportSelectors are usually processed in the same way as regular {@code @Import}* annotations, however, it is also possible to defer selection of imports until all* {@code @Configuration} classes have been processed (see {@link DeferredImportSelector}* for details).** @author Chris Beams* @since 3.1* @see DeferredImportSelector* @see Import* @see ImportBeanDefinitionRegistrar* @see Configuration*/ public interface ImportSelector {/*** Select and return the names of which class(es) should be imported based on* the {@link AnnotationMetadata} of the importing @{@link Configuration} class.*/String[] selectImports(AnnotationMetadata importingClassMetadata);}

该接口文档上说的明明白白,其主要作用是收集需要导入的配置类,如果该接口的实现类同时实现EnvironmentAware, BeanFactoryAware ,BeanClassLoaderAware或者ResourceLoaderAware,那么在调用其selectImports方法之前先调用上述接口中对应的方法,如果需要在所有的@Configuration处理完再导入时可以实现DeferredImportSelector接口。

三、例子

@Import(MyImportSelector.class) public class AppConfig { } public class MyDao {public void query(){System.out.println("query MyDao for MyImportSelect");} } public class MyImportSelector implements ImportSelector {@Overridepublic String[] selectImports(AnnotationMetadata importingClassMetadata) {return new String[]{MyDao.class.getName()};} } public class TestImpo {public static void main(String[] args) {AnnotationConfigApplicationContext ctx=new AnnotationConfigApplicationContext(AppConfig.class);MyDao myDao = ctx.getBean(MyDao.class);myDao.query();} } 运行打印 query MyDao for MyImportSelect

从上面的例子看,尽管程序上没有把MyImportSelect类放到Spring容器中,也没有把MyDao放到Spring容器中,但是在测试上就可以把MyDao从容器中拿出来,并且正常执行。不知道大家看到这里有什么感觉,到这里我其实是有疑问的。我这么做有个卵用噻。我是有病吧,直接把类上加个@Component注册进去不香吗?所以这个ImportSelector把程序搞这么复杂是有毛病吧,把简单的功能搞这么复杂。

四、ImportSelector真正的作用

其实Spring既然这么设计,那肯定是有用的。那么有什么用呢?设想这样一个场景,如果有些功能我们并不需要Spring在一开始就加载进去,而是需要Spring帮助我们把这些功能动态加载进去,这时候这个ImportSelector的作用就来了。我们完全可以把实现这个接口的类做成一个开关,用来开启或者关闭某一个或者某些功能类。比如说我们上面的例子MyDao,假设这个MyDao实现的功能是一个扩展功能,在正式的生产上不一定用的到。如果说一个包下有100个类,那么使用扫描去屏蔽这个类就很麻烦,但是屏蔽这个类用ImportSelector去做就很容易了。下次如果需要用到了,我再放开这个开关,直接可以使用MyDao的功能了,这样就做到了一个灵活的功能掌控。

  • 改造例子
@Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) @Import(MyImportSelector.class) public @interface EnableMySelector { }@EnableMySelector //该注解引入MyImportSelector public class AppConfig { }public class TestImpo {public static void main(String[] args) {AnnotationConfigApplicationContext ctx=new AnnotationConfigApplicationContext(AppConfig.class);MyDao myDao = null;//根据bean中是否有MyDao类型进行控制try {myDao = ctx.getBean(MyDao.class);myDao.query();} catch (BeansException e) {e.printStackTrace();}} }

其实Spring中那么多的EnableXXXX的注解底层就是这样的原理

五、ImportSelector源码分析

我们可以来看一下ConfigurationClassParser这个类的processImports方法

private void processImports(ConfigurationClass configClass, SourceClass currentSourceClass,Collection<SourceClass> importCandidates, boolean checkForCircularImports) {if (importCandidates.isEmpty()) {return;}if (checkForCircularImports && isChainedImportOnStack(configClass)) {this.problemReporter.error(new CircularImportProblem(configClass, this.importStack));}else {this.importStack.push(configClass);try {for (SourceClass candidate : importCandidates) {            //对ImportSelector的处理if (candidate.isAssignable(ImportSelector.class)) {// Candidate class is an ImportSelector -> delegate to it to determine importsClass<?> candidateClass = candidate.loadClass();ImportSelector selector = BeanUtils.instantiateClass(candidateClass, ImportSelector.class);ParserStrategyUtils.invokeAwareMethods(selector, this.environment, this.resourceLoader, this.registry);if (this.deferredImportSelectors != null && selector instanceof DeferredImportSelector) {                //如果为延迟导入处理则加入集合当中this.deferredImportSelectors.add(new DeferredImportSelectorHolder(configClass, (DeferredImportSelector) selector));}else {                //根据ImportSelector方法的返回值来进行递归操作String[] importClassNames = selector.selectImports(currentSourceClass.getMetadata());Collection<SourceClass> importSourceClasses = asSourceClasses(importClassNames);processImports(configClass, currentSourceClass, importSourceClasses, false);}}else if (candidate.isAssignable(ImportBeanDefinitionRegistrar.class)) {// Candidate class is an ImportBeanDefinitionRegistrar ->// delegate to it to register additional bean definitionsClass<?> candidateClass = candidate.loadClass();ImportBeanDefinitionRegistrar registrar =BeanUtils.instantiateClass(candidateClass, ImportBeanDefinitionRegistrar.class);ParserStrategyUtils.invokeAwareMethods(registrar, this.environment, this.resourceLoader, this.registry);configClass.addImportBeanDefinitionRegistrar(registrar, currentSourceClass.getMetadata());}else {              // 如果当前的类既不是ImportSelector也不是ImportBeanDefinitionRegistar就进行@Configuration的解析处理// Candidate class not an ImportSelector or ImportBeanDefinitionRegistrar ->// process it as an @Configuration classthis.importStack.registerImport(currentSourceClass.getMetadata(), candidate.getMetadata().getClassName());processConfigurationClass(candidate.asConfigClass(configClass));}}}catch (BeanDefinitionStoreException ex) {throw ex;}catch (Throwable ex) {throw new BeanDefinitionStoreException("Failed to process import candidates for configuration class [" +configClass.getMetadata().getClassName() + "]", ex);}finally {this.importStack.pop();}}}

在这里我们可以看到ImportSelector接口的返回值会递归进行解析,把解析到的类全名按照@Configuration进行处理

六、连带说一下DeferredImportSelector

这个是看Spring源码的时候发现的,直接翻译就是延时加载ImportSelector,实现这个接口的类,将会在@Configuration后面被加载,用法什么的和ImportSelector功能基本一样。因为用的比较稀有就不多做解释了,仅仅作为一个只是扩展点介绍下。在ConfigurationClassParser中会有一个判断,是不是这个接口,如果是就会放到后面解析。以下摘自源码:

org.springframework.context.annotation.ConfigurationClassParser#processImports //这里拦截了DeferredImportSelector然后使用handle() if (selector instanceof DeferredImportSelector) {this.deferredImportSelectorHandler.handle(configClass, (DeferredImportSelector) selector); }

进入handle()方法,发现和这个接口相关的都被加入了一个deferredImportSelectors的list中。

public void handle(ConfigurationClass configClass, DeferredImportSelector importSelector) {DeferredImportSelectorHolder holder = new DeferredImportSelectorHolder(configClass, importSelector);if (this.deferredImportSelectors == null) {DeferredImportSelectorGroupingHandler handler = new DeferredImportSelectorGroupingHandler();handler.register(holder);handler.processGroupImports();}else {//加入到了一个ArrayList中this.deferredImportSelectors.add(holder);} }

最终这个ArrayList在parse()方法的最后被处理了

org.springframework.context.annotation.ConfigurationClassParser#parse(java.util.Set<org.springframework.beans.factory.config.BeanDefinitionHolder>)public void parse(Set<BeanDefinitionHolder> configCandidates) {//根据BeanDefinition的类型做不同的处理,一般都会调用ConfigurationClassParser.parse()进行解析for (BeanDefinitionHolder holder : configCandidates) {BeanDefinition bd = holder.getBeanDefinition(); //拿出BeanDefinitiontry {if (bd instanceof AnnotatedBeanDefinition) { // 判断是不是加了注解的// 解析注解对象,并且把解析出来的bd方法map中,但是这里的bd指的的普通的// 普通和不普通的怎么区分。比如@Bean和各种beanFactoryPostProcessor得到的bean//如果被加了注解,又调用了一个parse()方法parse(((AnnotatedBeanDefinition) bd).getMetadata(), holder.getBeanName());}else if (bd instanceof AbstractBeanDefinition && ((AbstractBeanDefinition) bd).hasBeanClass()) {parse(((AbstractBeanDefinition) bd).getBeanClass(), holder.getBeanName());}else {parse(bd.getBeanClassName(), holder.getBeanName());}}catch (BeanDefinitionStoreException ex) {throw ex;}catch (Throwable ex) {throw new BeanDefinitionStoreException("Failed to parse configuration class [" + bd.getBeanClassName() + "]", ex);}}//处理,而此时上面其他的Import已经处理完了this.deferredImportSelectorHandler.process(); } 与50位技术专家面对面20年技术见证,附赠技术全景图

总结

以上是生活随笔为你收集整理的Spring.ImportSelector接口的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。