9、组件注册-@Import-使用ImportSelector

9.1 @Import 源码:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import { /**
* {@link Configuration}, {@link ImportSelector}, {@link ImportBeanDefinitionRegistrar}
* or regular component classes to import.
*/
Class<?>[] value(); }

可以使用ImportSelector来注册组建

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); }

9.2 自定义逻辑返回需要注入的组建

新建 MyImportSelector implements ImportSelector 重写selectImports 方法。

package com.hw.springannotation.conditional;

import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata; /**
* @Description 自定义逻辑返回需要注入的组建
* @Author hw
* @Date 2018/11/28 15:52
* @Version 1.0
*/
public class MyImportSelector implements ImportSelector {
/**
* 返回值就是需要导入的组建 全类名
*
* @param importingClassMetadata 当前标注@Import注解类的所有注解信息
*/
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
// 不能返回null
return new String[]{"com.hw.springannotation.beans.Blue"};
}
}

9.3 引入@Import

@Import({Color.class, Red.class, MyImportSelector.class})    // 快速导入组建,ID 默认是全路径包名

9.4 运行测试类

    /**
* @Import注解
*/
@Test
public void testImport() {
printBeans();
// System.exit(0);
} private void printBeans(){
String[] names = applicationContext.getBeanDefinitionNames();
for (String name : names) {
System.out.println(name);
}
}

结果如图:

9、组件注册-@Import-使用ImportSelector的更多相关文章

  1. Spring Framework 组件注册 之 @Import

    Spring Framework 组件注册 之 @Import 写在前面 向spring中注册组件或者叫javaBean是使用spring的功能的前提条件.而且spring也提供了很多种方式,让我们可 ...

  2. spring注解扫描组件注册

    最近对单点系统进行微服务拆分,被各个springboot的组件注册搞得云里雾里的.(有的是通过springboot的自动配置进IOC容器的,有的是自己添加构造方法添加进IOC容器.)决定抽时间将spr ...

  3. Spring注解开发-全面解析常用注解使用方法之组件注册

    目录 1. @Configuration 2. @ComponentScan excludeFilters includeFilters 使用自定义TypeFilter 3. @Bean @Scope ...

  4. spring组件注册

    基于注解和类的组件注册 @Conditional 作用:按照一定的条件进行判断,如果满足条件的话就给spring容器中注册bean ​ 该注解既可以标注到方法上面,也可以标注到类上面(只有满足条件时, ...

  5. Spring注解开发系列Ⅱ --- 组件注册(下)

    1.@Import注册组件 @Import主要功能是通过导入的方式实现把实例加入springIOC容器中, /** * 给容器注册组件 * 1.包扫描+组件标注注解(@Controller,@Serv ...

  6. 【建议收藏】阿里P7总结的Spring注解笔记,把组件注册讲的明明白白

    环境搭建 注解的方式是通过配置类的方式来注入组件,注解注入要比XML注入的方式简单,注解注入也需要在前者的基础上,添加一个spring-context的包,也是实际开发中常用的方式. 准备所需Jar包 ...

  7. 【spring 注解驱动开发】spring组件注册

    尚学堂spring 注解驱动开发学习笔记之 - 组件注册 组件注册 1.@Configuration&@Bean给容器中注册组件 2.@ComponentScan-自动扫描组件&指定扫 ...

  8. spring注解-组件注册

    一.@Configuration+@Bean @Configuration:配置类==配置文件 @Bean:给容器中注册一个Bean:类型为返回值的类型,默认是用方法名作为id @Bean(" ...

  9. Vue组件深入了解(组件注册和Prop)

    一.组件名 自定义组件的名称强烈推荐遵循W3C规范中的方式:字母全小写且必须包含一个连字符. 二.全局注册和局部注册的区别 全局注册 Vue.component进行注册.全局注册的可以在任何创建的实例 ...

随机推荐

  1. K8S从入门到放弃系列-(12)Kubernetes集群Coredns部署

    摘要: 集群其他组件全部完成后我们应当部署集群 DNS 使 service 等能够正常解析,1.11版本coredns已经取代kube-dns成为集群默认dns. 1)下载yaml配置清单 [root ...

  2. Word 查找替换高级玩法系列之 -- 将换行符替换成回车符

    我们从网上Copy过来的很多Word文档,里面使用的都是换行符,也就是我们常说的软回车,它显示为一个向下的箭头.这些符号不仅碍眼,而且会影响我们后期的排版,尤其是对段落的排版,因为Word里面的段落只 ...

  3. Python 【类与对象】

    类与对象 把类的个例就叫做实例 (instance),可理解为“实际的例子”类是某个特定的群体,实例是群体中某个具体的个体 Python中的对象等于类和实例的集合:即类可以看作是对象,实例也可以看作是 ...

  4. S03_CH07_AXI_VDMA_OV5640摄像头采集系统

    S03_CH07_AXI_VDMA_OV5640摄像头采集系统 7.1概述 本章内容和<S03_CH06_AXI_VDMA_OV7725摄像头采集系统>只是摄像头采用的分辨率不同,其他原理 ...

  5. Python中操作mysql的pymysql模块详解(转载)

    https://www.cnblogs.com/wt11/p/6141225.html

  6. (三十)JSP标签之自定义标签

    创建一个类,引入外部jsp-api.jar包(在tomcat 下lib包里有),这个类继承SimpleTagSupport 重写doTag()方法. jspprojec包下的helloTag类: 1 ...

  7. Abp 领域事件简单实践 <二>

    上一篇说的是仓储增删改 的时候会自动触发领域事件. 其实也可以随时触发. 现在在应用层触发. 应用层依赖注入 EventBus public void Trigger() { var e = new ...

  8. umi model 注册

    model 分两类,一是全局 model,二是页面 model.全局 model 存于 /src/models/ 目录,所有页面都可引用:页面 model 不能被其他页面所引用. 规则如下: src/ ...

  9. python采集websocket实时数据

    之前大部分的数据采集基本都是http的,也一直对如何采集websocket的实时数据有疑问,不知道入从哪里入手,一筹莫展,今天在知乎上看到了一篇采集websocket的文章,讲的很透彻 终于把这个疑问 ...

  10. Endless looping of packets in TCP/IP networks (Routing Loops)

    How endless looping of packets in a TCP/IP network might occur? Router is a device used to interconn ...