【SpringBoot】Re 02 Import与自定义装配实现
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与自定义装配实现的更多相关文章
- java框架之SpringBoot(10)-启动流程及自定义starter
启动流程 直接从 SpringBoot 程序入口的 run 方法看起: public static ConfigurableApplicationContext run(Object source, ...
- SpringBoot入门(五)——自定义配置
本文来自网易云社区 大部分比萨店也提供某种形式的自动配置.你可以点荤比萨.素比萨.香辣意大利比萨,或者是自动配置比萨中的极品--至尊比萨.在下单时,你并没有指定具体的辅料,你所点的比萨种类决定了所用的 ...
- springboot - 返回xml error 从自定义的 ErrorController
1.概览 2.在<springboot - 返回JSON error 从自定义的 ErrorController>基础上,做如下调整: 1).新增Attribute类和Error类 pac ...
- springboot + 拦截器 + 注解 实现自定义权限验证
springboot + 拦截器 + 注解 实现自定义权限验证最近用到一种前端模板技术:jtwig,在权限控制上没有用springSecurity.因此用拦截器和注解结合实现了权限控制. 1.1 定义 ...
- @Import导入自定义选择器
@Import导入自定义选择器 之前一篇博文:Spring中的@Import注解已经详细介绍了@Import注解,不赘述. 需求描述 通过@import注解自定义组件选择器,将满足我们自定义的规则的b ...
- springBoot进阶02
SpringBoot进阶02 1. 日志的使用 1.1 基本使用 /** * 获取日志记录器 */ Logger logger = LoggerFactory.getLogger(this.getCl ...
- SpringBoot自定义装配的多种实现方法
Spring手动装配实现 对于需要加载的类文件,使用@Configuration/@Component/@Service/@Repository修饰 @Configuration public cla ...
- springboot系列十四、自定义实现starter
一.starter的作用 当我们实现了一个组建,希望尽可能降低它的介入成本,一般的组建写好了,只要添加spring扫描路径加载spring就能发挥作用.有个更简单的方式扫描路径都不用加,直接引入jar ...
- thymeleaf教程-springboot项目中实现thymeleaf自定义标签
转载: http://www.9191boke.com/466119140.html 91博客网 开始: 在使用thymeleaf的过程中有时候需要公共部分渲染页面,这个时候使用自定义标签实现自 ...
- SpringBoot核心特性之组件自动装配
写在前面 spring boot能够根据依赖的jar包自动配置spring boot的应用,例如: 如果类路径中存在DispatcherServlet类,就会自动配置springMvc相关的Bean. ...
随机推荐
- 采用Dapr 的IoT 案例
CNCF 发布了一篇Dapr 的IoT 案例:Tempestive uses Dapr and K8s to track IoT messages | CNCF.Tempestive 是一家物联网解决 ...
- jquery的绑定和删除
// 基本语法形式 $().on( 事件类型 , 事件处理函数 ) // 删除事件处理函数 // 必须绑定的是 函数名称 才能删除 绑定的事件处理函数 ...
- IceRPC之依赖注入>快乐的RPC
作者引言 很高兴啊,我们来到了IceRPC之依赖注入>快乐的RPC,基础引导,打好基础,才能让自已不在迷茫,快乐的畅游世界. 依赖注入和IceRPC 了解 IceRPC (C#) 如何为依赖注入 ...
- Java实际工作里用到的几种加密方式
1.Base64加密 最简单的加密方式,甚至可以说不是加密,只是一种用64个字符表示任意二进制数据的方法.Base64编码原理是将输入字符串按字节切分,取得每个字节对应的二进制值(若不足8比特则高位补 ...
- 实验一:Wireshark工具的使用
1.0 [实验目的] 了解Wireshark.TCP协议的概念,掌握Wireshark抓包工具的使用.FTP的搭建和登录,学会对Wireshark抓包结果的分析. 2.0[知识点] Wireshark ...
- opencv在MAC下的安装
版本信息 MAC版本:10.10.5 Xcode版本:7.2 openCV版本:2.4.13 安装步骤: 联网 安装brew,在终端输入指令 /usr/bin/ruby -e "$(curl ...
- 深度解读昇腾CANN多流并行技术,提高硬件资源利用率
本文分享自华为云社区<深度解读昇腾CANN多流并行技术,提高硬件资源利用率>,作者:昇腾CANN. 随着人工智能应用日益成熟,文本.图片.音频.视频等非结构化数据的处理需求呈指数级增长,数 ...
- 3.8折年终钜惠,RK3568J国产工业评估板
3.8折年终钜惠,RK3568J国产工业评估板活动火热进行中,错过等一年! -核心板国产化率100%,提供报告-瑞芯微四核ARM Cortex-A55@1.8GHz-4K视频解码.1080P视频编码. ...
- ARM+DSP异构多核——全志T113-i+玄铁HiFi4核心板规格书
核心板简介 创龙科技SOM-TLT113是一款基于全志科技T113-i双核ARM Cortex-A7 + 玄铁C906 RISC-V + HiFi4 DSP异构多核处理器设计的全国产工业核心板,ARM ...
- k8s实战 ---- pod 基础
如果你对k8s还不了解,可以看下前文 k8s 实战 1 ---- 初识 (https://www.cnblogs.com/jilodream/p/18245222) 什么是pod,pod在 ...