【Spring Boot】Spring Boot之使用ImportBeanDefinitionRegistrar类实现动态注册Bean
一、ImportBeanDefinitionRegistrar类介绍
ImportBeanDefinitionRegistrar类通过其他@Configuration类通过@Import的方式来加载,通常是启动类或配置类。
然后会调用该接口方法,将其中要注册的类注册成bean。
通过实现该接口的类拥有注册bean的能力。
可以实现遗下Aware接口
EnvironmentAware
BeanFactoryAware
BeanClassLoaderAware
ResourceLoaderAware
二、实现注册指定类型的Bean
1)定义一个需要用来注册的service类
/**
* @author zhangboqing
* @date 2019/12/13
*/
@Slf4j
public class HelloService {
public void sayHello() {
log.info("Hello!!!");
}
}
2)定义HelloImportBeanDefinitionRegistrar类实现ImportBeanDefinitionRegistrar接口
/**
* @author zhangboqing
* @date 2019/12/13
*/
public class HelloImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { @Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
// 扫描注解
Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(ComponentScan.class.getName());
String[] basePackages = (String[]) annotationAttributes.get("basePackages"); // 扫描类
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry, false);
// 指定类
TypeFilter helloServiceFilter = new AssignableTypeFilter(HelloService.class);
scanner.addIncludeFilter(helloServiceFilter);
scanner.scan(basePackages);
}
}
3)定义HelloConfiguration配置类,引入ImportBeanDefinitionRegistrar
/**
* @author zhangboqing
* @date 2019/12/13
*/
@Configuration
@ComponentScan("com.zbq.springbootdemo.speciality.dynamicRegisterBean.ImportBeanDefinitionRegistrar")
@Import(HelloImportBeanDefinitionRegistrar.class)
public class HelloConfiguration {
}
4)定义测试类可验证HelloService类是否注册成功
/**
* @author zhangboqing
* @date 2019/12/13
*/
@SpringBootTest(classes = {HelloConfiguration.class})
// 指定配置类
//@ContextConfiguration(classes = {HelloConfiguration.class})
public class HelloImportBeanDefinitionRegistrarTest { @Autowired
private HelloService helloService; @Test
public void testHelloService() {
helloService.sayHello();
}
}
三、实现注册指定注解的Bean
1)自定义注解HelloAnnotation
/**
* @author zhangboqing
* @date 2019/12/13
*/
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface HelloAnnotation {
}
2)将注解加入到HelloService类上
/**
* @author zhangboqing
* @date 2019/12/13
*/
@Slf4j
@HelloAnnotation
public class HelloService {
public void sayHello() {
log.info("Hello!!!");
}
}
3)修改HelloImportBeanDefinitionRegistrar类,将类型过滤器从指定类型变为指定注解
/**
* @author zhangboqing
* @date 2019/12/13
*/
public class HelloImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar { @Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
// 扫描注解
Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(ComponentScan.class.getName());
String[] basePackages = (String[]) annotationAttributes.get("basePackages"); // 扫描类
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(registry, false);
// 指定类
// TypeFilter helloServiceFilter = new AssignableTypeFilter(HelloService.class);
// 指定注解
TypeFilter helloServiceFilter = new AnnotationTypeFilter(HelloAnnotation.class);
scanner.addIncludeFilter(helloServiceFilter);
scanner.scan(basePackages);
}
}
4)用上面的测试类再次验证,同样可以
【Spring Boot】Spring Boot之使用ImportBeanDefinitionRegistrar类实现动态注册Bean的更多相关文章
- 【Spring Boot】Spring Boot之使用ImportSelector类实现动态注册Bean
一.ImportSelector类介绍 可以通过指定的选择条件来决定哪些类被注册到Spring中.与ImportBeanDefinitionRegistrar类功能相似,通过@Import的方 ...
- SpringBoot27 JDK动态代理详解、获取指定的类类型、动态注册Bean、接口调用框架
1 JDK动态代理详解 静态代理.JDK动态代理.Cglib动态代理的简单实现方式和区别请参见我的另外一篇博文. 1.1 JDK代理的基本步骤 >通过实现InvocationHandler接口来 ...
- 【Spring注解驱动开发】在@Import注解中使用ImportBeanDefinitionRegistrar向容器中注册bean
写在前面 在前面的文章中,我们学习了如何使用@Import注解向Spring容器中导入bean,可以使用@Import注解快速向容器中导入bean,小伙伴们可以参见<[Spring注解驱动开发] ...
- 基于ImportBeanDefinitionRegistrar和FactoryBean动态注入Bean到Spring容器中
基于ImportBeanDefinitionRegistrar和FactoryBean动态注入Bean到Spring容器中 一.背景 二.实现方案 1.基于@ComponentScan注解实现 2.基 ...
- Spring动态注册bean实现动态多数据源
Spring动态注册bean实现动态多数据源 http://blog.csdn.net/littlechang/article/details/8071882
- spring boot 动态注入bean
方法一 SpringContextUtil public class SpringContextUtil { private static ApplicationContext application ...
- 使用spring配置类代替xml配置文件注册bean类
spring配置类,即在类上加@Configuration注解,使用这种配置类来注册bean,效果与xml文件是完全一样的,只是创建springIOC容器的方式不同: //通过xml文件创建sprin ...
- Spring之2:Spring Bean动态注册、删除
IoC容器的初始化包括BeanDefinition的Resource定位.载入和注册这三个基本的过程. 一.Resource定位.BeanDefinition的资源定位有resourceLoader通 ...
- 【String注解驱动开发】面试官让我说说:如何使用FactoryBean向Spring容器中注册bean?
写在前面 在前面的文章中,我们知道可以通过多种方式向Spring容器中注册bean.可以使用@Configuration结合@Bean向Spring容器中注册bean:可以按照条件向Spring容器中 ...
随机推荐
- MATLAB粒子群优化算法(PSO)
MATLAB粒子群优化算法(PSO) 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 一.介绍 粒子群优化算法(Particle Swarm Optim ...
- 新版本的node,全局配置
配置环境变量: 1.在你安装node环境目录下 即是node_modules同级目录中. 创建两个文件夹[node_global]及[node_cache] node_cache:缓存目录 node_ ...
- luoguP3071 [USACO13JAN]座位Seating
https://www.luogu.org/problem/P3071 AC代码: https://www.luogu.org/blog/user33426/solution-p3071 莫名其妙RE ...
- 有用的link
资料 了解oi 刘汝佳代码仓库(紫书 c++参考手册 2018年洛谷日报索引 2019年洛谷日报索引 (其他oj: luogu 虚拟判官(名校oj都有 离线bzoj题库 (有时候进不去请点:rxz大爷 ...
- DOM的重绘和回流及代码性能优化
1.DOM的重绘和回流Repaint&Reflow 1.1重绘:元素样式的改变(但宽高.大小.位置等不变) 如outline.visibility.color.background-color ...
- bootstrap多级下拉菜单
只需为下拉菜单的任意 <li> 元素添加 .dropdown-submenu 的类,并在该 <li> 元素下添加 .dropdown-menu 类的列表,就可以为该菜单项添加一 ...
- golang 服务端和客户端(二)
1.golang服务端 package main import( "net/http" ) func main(){ //注册处理函数,用户连接,自动调用指定的处理函数 http. ...
- jquery和bootstrap:
事件:绑定bind和解绑unbind: $('选择器').bind('事件',function(){ // 操作 }) $('选择器').unbind('事件') $('选择器').c ...
- MySQL实战45讲学习笔记:第三十五讲
一.本节概述 在上一篇文章中,我和你介绍了 join 语句的两种算法,分别是 Index Nested-LoopJoin(NLJ) 和 Block Nested-Loop Join(BNL). 我们发 ...
- UOS系统 - 国产统一操作系统UOS的基本知识
一.UOS操作系统含义及现状 UOS操作系统与windows不同的是,UOS统一操作系统支持龙芯.申威.华为鲲鹏等一票国产处理器芯片.它的诞生是多家国内科技公司联合孕育的结果,包括中国电子集团.武汉深 ...