当前位置: 首页 > news >正文

SpringBoot复习:(21)自定义ImportBeanDefinitionRegistrar

要达到的目的:将某个包下使用了某个自定义注解(比如@MyClassMapper)的类注册到Spring 容器。
一、自定义注解:

package com.example.demo.service;import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;@Retention(RetentionPolicy.RUNTIME)
public @interface MyClassMapper {
}

二、使用了自定义注解的组件类:

package com.example.demo.service;@MyClassMapper
public class HelloService {public HelloService(){System.out.println("this is hello......");}
}

三、自定义一个ClasspathBeanDefinitionScanner:

package com.example.demo.component;import com.example.demo.service.MyClassMapper;
import com.example.demo.service.MybatisMapper;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.stereotype.Component;import java.util.Set;public class MyClasspathBeanDefinitionScanner extends ClassPathBeanDefinitionScanner {public MyClasspathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters) {super(registry, useDefaultFilters);}protected void registerFilters() {addIncludeFilter(new AnnotationTypeFilter(MyClassMapper.class));}@Overrideprotected Set<BeanDefinitionHolder> doScan(String... basePackages) {return super.doScan(basePackages);}
}

四、自定义ImportBeanDefinitionRegistrar

package com.example.demo.component;import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.stereotype.Component;@Component
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {public static String BASE_PACKAGE = "com.example.demo.service";@Overridepublic void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {MyClasspathBeanDefinitionScanner scanner = new MyClasspathBeanDefinitionScanner(registry, false);scanner.registerFilters();scanner.doScan(BASE_PACKAGE);}
}

其中调用MyClasspathBeanDefinitionScanner进行组件扫描。

五、将自定义ImportBeanDefinitionRegistrar配置到容器

package com.example.demo.config;import com.example.demo.component.MyClasspathBeanDefinitionScanner;
import com.example.demo.component.MyImportBeanDefinitionRegistrar;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;@Configuration
@Import(MyImportBeanDefinitionRegistrar.class)
public class MyConfig {
}

六、测试代码

package com.example.demo;import com.example.demo.service.HelloService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.PropertySource;@SpringBootApplication
@PropertySource("classpath:my.properties")
public class DemoApplication {public static void main(String[] args) {ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);System.out.println("abc");HelloService helloService = (HelloService) context.getBean("helloService");System.out.println(helloService);}}
http://www.lryc.cn/news/110374.html

相关文章:

  • 小黑子—JavaWeb:第五章 - JSP与会话跟踪技术
  • Python - 【socket】 客户端client重连处理简单示例Demo(一)
  • Redis 基础
  • 【0805作业】Linux中 AB终端通过两根有名管道进行通信聊天(半双工)
  • ruby - ckeditor 设置编辑器高度
  • WMS仓库管理系统研发规划说明
  • JavaScript |(六)DOM事件 | 尚硅谷JavaScript基础实战
  • 实验心得,包括代码复现工作的体会
  • RabbitMQ(二)
  • Linux软件实操
  • kagNet:对常识推理的知识感知图网络 8.4+8.5
  • Jmeter 压测工具使用手册[详细]
  • matlab智能算法程序包89套最新高清录制!matlab专题系列!
  • caj文件怎么转换成pdf?了解一下这种方法
  • windows 同时安装 Mysql 5.7 和8.0
  • 数字孪生的「三张皮」问题:数据隐私、安全与伦理挑战
  • Hadoop学习:深入解析MapReduce的大数据魔力(上)
  • MQ(一)-MQ理论与消息中间件简介
  • vb与EXCEL的连接
  • java使用openOffice将excel转换pdf时,将所有列显示在一页
  • python数据容器
  • 【TypeScript】中定义与使用 Class 类的解读理解
  • 好用的数据库管理软件之idea(idea也有数据库???)
  • 《操作系统-李治军》测验错题集
  • DP-GAN-判别器代码
  • 基于多线程实现服务器并发
  • Golang之路---03 面向对象——接口与多态
  • 一条自由游动的鲸鱼
  • 将python源代码打包成.exe可执行文件
  • 【数据结构篇】手写双向链表、单向链表(超详细)