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. LeetCode 688. Knight Probability in Chessboard “马”在棋盘上的概率 (C++/Java)

    题目: On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exact ...

  2. JavaScript中如何终止forEach循环&跳出for(双层)循环?

    在JavaScript中,forEach方法是用于遍历数组的,通常没有直接终止循环的机制.然而,我们可以使用一些技巧来模拟终止forEach循环.以下是几种常见的方法 1.使用return语句:在fo ...

  3. Blender练习——SciFi枪械.md

    Blender练习--SciFi枪械 一.基本操作 常用快捷键 E 挤出 B 倒角,中途可通过滚轮或S来调整细分 Alt+点选 循环选择 Ctrl Alt+点选 并排选择 F 补面,比如一个碗口,将碗 ...

  4. NetMvc通过亚马逊方式服务器端和客户端上传MinIO顺利解决

    前言: 1.由于项目是.NET Framework 4.7 MVC LayUI,所以需要找一个资源站点存放项目中静态资源文件: 2.需要支持服务端和客户端都支持上传文件方式: 3.调用简单,涉及库越少 ...

  5. 信奥一本通1164:digit函数

    1164:digit函数 时间限制: 1000 ms 内存限制: 65536 KB 提交数:41504 通过数: 26475 [题目描述] 在程序中定义一函数digit(n,k) ,它能分离出整数n ...

  6. B码对时方案,基于TI AM62x异构多核工业处理器实现!

    什么是IRIG-B码对时 IRIG-B(inter-range instrumentationgroup-B)码是一种时间同步标准,通常用于精确的时间测量和数据同步,广泛应用于电力.通信.航空等领域. ...

  7. 数据结构—包(Bag)

    数据结构中的包,其实是对现实中的包的一种抽象. 想像一下现实中的包,比如书包,它能做什么?有哪些功能?首先它用来装东西,里面的东西可以随便放,没有规律,没有顺序,当然,可以放多个相同的东西.其次,东西 ...

  8. flutter 一直卡在Running Gradle task 'assembleDebug'...运行不起来

    大概率只有一个原因:gradle下载不完整! 要想办法让他下载完整! 解决方法: 方法一:修改远程maven仓库地址(2024.7.9下列地址可用) repositories{ maven{ url' ...

  9. 基于vsftpd搭建项目文件服务器

    vsftpd 是"very secure FTP daemon"的缩写,安全性是它的一个最大的特点.vsftpd 是一个 UNIX 类操作系统上运行的服务器的名字,它可以运行在诸如 ...

  10. 12 二次打开pdf失败

    h5 安卓 iOS均出现pdf二次打开失败