spring笔记-DeferredImportSelector

1.概念

有2个特点

  1. 继承该接口的ImportSelector会在最后执行
  2. 如果定义了1一个以上的DeferredImportSelector则使用Order接口来进行排序
/**
 * A variation of {@link ImportSelector} that runs after all {@code @Configuration} beans
 * have been processed. This type of selector can be particularly useful when the selected
 * imports are {@code @Conditional}.
 *
 * <p>Implementations can also extend the {@link org.springframework.core.Ordered}
 * interface or use the {@link org.springframework.core.annotation.Order} annotation to
 * indicate a precedence against other {@link DeferredImportSelector}s.
 *
 * @author Phillip Webb
 * @since 4.0
 */
public interface DeferredImportSelector extends ImportSelector {

}

2.测试代码

    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Import({DeferredImportSelector1.class, DeferredImportSelector2.class, ImportSelector1.class, ImportSelector2.class})
    public static @interface Sample {
    }


    public static class ImportSelector1 implements ImportSelector {

        @Override
        public String[] selectImports(AnnotationMetadata importingClassMetadata) {
            ImportSelectorTests.importFrom.put(getClass(), importingClassMetadata.getClassName());
            return new String[] { ImportedSelector1.class.getName() };
        }
    }


    public static class ImportSelector2 implements ImportSelector {

        @Override
        public String[] selectImports(AnnotationMetadata importingClassMetadata) {
            ImportSelectorTests.importFrom.put(getClass(), importingClassMetadata.getClassName());
            return new String[] { ImportedSelector2.class.getName() };
        }
    }


    public static class DeferredImportSelector1 implements DeferredImportSelector, Ordered {

        @Override
        public String[] selectImports(AnnotationMetadata importingClassMetadata) {
            ImportSelectorTests.importFrom.put(getClass(), importingClassMetadata.getClassName());
            return new String[] { DeferredImportedSelector1.class.getName() };
        }

        @Override
        public int getOrder() {
            return Ordered.LOWEST_PRECEDENCE;
        }
    }


    @Order(Ordered.HIGHEST_PRECEDENCE)
    public static class DeferredImportSelector2 implements DeferredImportSelector {

        @Override
        public String[] selectImports(AnnotationMetadata importingClassMetadata) {
            ImportSelectorTests.importFrom.put(getClass(), importingClassMetadata.getClassName());
            return new String[] { DeferredImportedSelector2.class.getName() };
        }
    }

执行的先后顺序为
ImportSelector1-> ImportSelector2-> DeferredImportSelector2-> DeferredImportSelector1

参考:
https://www.jianshu.com/p/5f99d0809eaa

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,288评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,826评论 25 709
  • 再不疯狂,我们就都老了!35岁是个坎,少了一点年少轻狂,多了一份责任。少了一点冲动,多了一丝理智。在这个暖冬的阳光...
    谢谢传媒阅读 342评论 0 0
  • 早上有心看了下自己的关注的公众号的数量,数下来有76个那么多,好多公众号可能就结缘于某一篇文章,看完后自此再没有打...
    启航2020年阅读 487评论 1 2
  • 萨特一度是二十世纪欧洲最著名的存在主义大师。他著名的论断模式是“不……毋宁死”。 法国曾被德国纳粹奴役,很多人被关...
    如一书阅读 242评论 0 2