Import的注册形式:

1、使用@Import导入一个或者多个类字节对象

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {
Class<?>[] value();
}

使用时一般在配置类上注解,表示该注解类导入了其他配置

@Configuration
@Import({
MyBeanFactoryPostProcessor.
class,
ClassA.class,
ClassB.class,
ClassC.class
})
public class TestConfiguration {
}

2、使用导入Bean定义登记者

public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(ClassD.class);
registry.registerBeanDefinition("classD", rootBeanDefinition);
}
}

然后配置导入:

@Configuration
@Import(MyImportBeanDefinitionRegistrar.class)
public class TestConfiguration {
}

测试运行:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestConfiguration.class)
public class DefinitionTest {
@Autowired
private ClassD classD; @Test
public void testSample() {
System.out.println(classD);
}
}
cn.dzz.bean.ClassD@6771beb3

Process finished with exit code 0

3、使用【导入选择器】

原始版本是直接声明类完整路径名

public class MyImportSelector implements ImportSelector {

    public String[] selectImports(AnnotationMetadata annotationMetadata) {
return new String[] {"cn.dzz.bean.ClassD"};
}
}

导入配置:

@Configuration
@Import(MyImportSelector.class)
public class TestConfiguration {
}

测试结果:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestConfiguration.class)
public class DefinitionTest {
@Autowired
private ClassD classD; @Test
public void testSample() {
System.out.println(classD);
}
}
cn.dzz.bean.ClassD@682b2fa

Process finished with exit code 0

第二版本,类的限定名固定编写在源码文件中不可灵活改变:

我们可以通过外部的配置文件来实现:

1、创建配置读取类

public class Tc51AutoConfigReader {

    public static Properties readerProperties(String resource){
Properties properties = new Properties();
InputStream it = Tc51AutoConfigReader.class.getResourceAsStream(resource); try {
properties.load(it); } catch (IOException e) {
e.printStackTrace();
}
return properties;
}
}

2、获取字符串,但是这个方法写的非常简单,只读取了一个类

我们的一个Map是允许一个键存储多个值的,也就是读取多个类

public String[] selectImports(AnnotationMetadata annotationMetadata) {
  Properties properties = Tc51AutoConfigReader.readerProperties("/Tc51autoconfig.properties");
  String property = properties.getProperty(Tc51EnableAutoConfig.class.getName());
  return new String[]{property};
}

第三版本,调用Spring写好的方法和注解方式实现自动装配
首先仿照SpringBoot创建这样的配置文件:

内部配置信息:

cn.dzz.annotation.MyEnableAutoConfiguration = \
cn.dzz.config.RedisConfiguration

声明一个自定义开启自动配置注解:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(MyImportSelector.class)
public @interface MyEnableAutoConfiguration {
}

然后实现导入选择器接口的方法变成这样:

public class MyImportSelector implements ImportSelector {

    private Class<?> getMyFactoryLoaderClass(){
return MyEnableAutoConfiguration.class;
} private List<String> getConfiguration(){
return SpringFactoriesLoader.loadFactoryNames(
getMyFactoryLoaderClass(),MyImportSelector.class.getClassLoader()
);
} public String[] selectImports(AnnotationMetadata annotationMetadata) {
return StringUtils.toStringArray(getConfiguration());
}
}

首先是得到注解类的字节对象,它被下面的方法所需要,

获取配置方法可以从配置文件中读取信息返回一个List集合,里面装载了那些类限定名(配置信息)

需要的参数是上面写的注解类字节对象和一个类加载器

再返回给选择器方法,集合转换一下数组即可

【SpringBoot】Re 02 Import与自定义装配实现的更多相关文章

  1. java框架之SpringBoot(10)-启动流程及自定义starter

    启动流程 直接从 SpringBoot 程序入口的 run 方法看起: public static ConfigurableApplicationContext run(Object source, ...

  2. SpringBoot入门(五)——自定义配置

    本文来自网易云社区 大部分比萨店也提供某种形式的自动配置.你可以点荤比萨.素比萨.香辣意大利比萨,或者是自动配置比萨中的极品--至尊比萨.在下单时,你并没有指定具体的辅料,你所点的比萨种类决定了所用的 ...

  3. springboot - 返回xml error 从自定义的 ErrorController

    1.概览 2.在<springboot - 返回JSON error 从自定义的 ErrorController>基础上,做如下调整: 1).新增Attribute类和Error类 pac ...

  4. springboot + 拦截器 + 注解 实现自定义权限验证

    springboot + 拦截器 + 注解 实现自定义权限验证最近用到一种前端模板技术:jtwig,在权限控制上没有用springSecurity.因此用拦截器和注解结合实现了权限控制. 1.1 定义 ...

  5. @Import导入自定义选择器

    @Import导入自定义选择器 之前一篇博文:Spring中的@Import注解已经详细介绍了@Import注解,不赘述. 需求描述 通过@import注解自定义组件选择器,将满足我们自定义的规则的b ...

  6. springBoot进阶02

    SpringBoot进阶02 1. 日志的使用 1.1 基本使用 /** * 获取日志记录器 */ Logger logger = LoggerFactory.getLogger(this.getCl ...

  7. SpringBoot自定义装配的多种实现方法

    Spring手动装配实现 对于需要加载的类文件,使用@Configuration/@Component/@Service/@Repository修饰 @Configuration public cla ...

  8. springboot系列十四、自定义实现starter

    一.starter的作用 当我们实现了一个组建,希望尽可能降低它的介入成本,一般的组建写好了,只要添加spring扫描路径加载spring就能发挥作用.有个更简单的方式扫描路径都不用加,直接引入jar ...

  9. thymeleaf教程-springboot项目中实现thymeleaf自定义标签

    转载: http://www.9191boke.com/466119140.html    91博客网 开始: 在使用thymeleaf的过程中有时候需要公共部分渲染页面,这个时候使用自定义标签实现自 ...

  10. SpringBoot核心特性之组件自动装配

    写在前面 spring boot能够根据依赖的jar包自动配置spring boot的应用,例如: 如果类路径中存在DispatcherServlet类,就会自动配置springMvc相关的Bean. ...

随机推荐

  1. 在webpack中运行vue

    网址:https://vue-loader.vuejs.org/zh/ Vue Loader 是一个 webpack 的 loader,它允许你以一种名为单文件组件的格式撰写 Vue 组件 安装loa ...

  2. SRE Google 运维解密读书笔记一:SRE 方法论概述

    SRE Google 运维解密,是 SRE 领域的启蒙之作,讲述了 Google 的 SRE 实践,SRE 就是从 Google 流传出来的.本文是读书笔记,第一篇,概述 SRE 方法论.帮大家把书读 ...

  3. javascript高级编程笔记第五章

    chapter 5 5.5 Function类型 未完待续 函数实际上是对象,每个函数都是Function类型的实例,因此与其他引用类型一样具有属性和方法 因此函数名实际上就是函数对象的指针,不会与某 ...

  4. Apollo启动配置排查,超时时间的配置

    Apollo启动配置排查 1.排查下来是 本地的服务 apollo 配置fake发布到线上去了.2.或者是引用的apollo jar包中指向的apollo服务器地址是否正确. 3.超时时间的配置 ## ...

  5. springboot3使用validation进行参数验证

    前言   今天学习了使用validation整合springboot进行字段的校验,体验下来感觉很不错,有了validation可以省下一大堆控制器里面的数据校验,例如前端发送了一个请求到我们后端,请 ...

  6. 前端Uncaught (in promise) 的解决方法及原因

    问题:在Vue项目中使用axios调用一个第三方的接口时,前端无法获取到接口返回值,检查控制台Network发现接口请求已经正常发出并且有数据返回,但是控制台Console报了这么一个错误 上图可以看 ...

  7. 关于 Jupyter Nbconvert 自定义 LaTeX 模板,中文兼容与格式设置,从 Notebook 构建 LaTeX PDF 文档

    目录 为什么会有这篇随笔的内容? 简述一下我遇到的问题 Nbconvert 转换 .ipynb 文件的基本方法 Jupyter Nbconvert 构建中文 \(\LaTeX\) 文档的痛点 Jupy ...

  8. 使用64位Office2016处理万级数据的过程

    先放下载和安装教程https://mp.weixin.qq.com/s/5ym9950_NZROlN0s2ZmLTg 由于同事电脑在使用Mysql for Excel插件处理十万级数据,如下图: 爆出 ...

  9. SpringBoot集成日志框架

    springboot默认日志是打印在console中,不会保存在文件中.我们项目上线肯定要保存日志用于分析问题的. 使用xml配置日志保存 并不需要pom配置slf4j依赖,starter里面已经配置 ...

  10. v-model 的原理?

    我们在 vue 项目中主要使用 v-model 指令在表单 input.textarea.select 等元素上创建双向数据绑定,我们知道 v-model 本质上不过是语法糖,v-model 在内部为 ...