4、组件注册-自定义TypeFilter指定过滤规则

4.1 FilterType.ANNOTATION 按照注解方式

4.2 FilterType.ASSIGNABLE_TYPE 按照给定的类型

@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {BookService.class})

4.3 FilterType.ASPECTJ 按照ASPECTJ表达式

4.4 FilterType.REGEX 按照正则表达

4.5 FilterType.CUSTOM 按照自定义规则

@ComponentScan(value = "com.hw.springannotation", useDefaultFilters = false,
includeFilters = {
// @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Controller.class}),
// @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {BookService.class}),
@ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyTypeFilter.class})
}
) // 指定只扫描
  • 创建MyTypeFilter类,需要实现TypeFilter 接口,重写match()方法
package com.hw.springannotation.config;

import org.springframework.core.io.Resource;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.ClassMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter; import java.io.IOException; /**
* @Description TODO
* @Author hw
* @Date 2018/11/27 18:49
* @Version 1.0
*/
public class MyTypeFilter implements TypeFilter {
/**
* Determine whether this filter matches for the class described by
* the given metadata.
*
* @param metadataReader the metadata reader for the target class 读取到得当前正在扫描的类的信息
* @param metadataReaderFactory a factory for obtaining metadata readers 一个可以探索其他类信息的工厂类
* for other classes (such as superclasses and interfaces)
* @return whether this filter matches
* @throws IOException in case of I/O failure when reading metadata
*/
public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
// 获取当前类的注解信息
AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
// 获取当前类信息
ClassMetadata classMetadata = metadataReader.getClassMetadata();
// 获取当前类的资源(类的路径)
Resource resource = metadataReader.getResource(); String className = classMetadata.getClassName();
System.out.println("----" + className); if (className.contains("er")) {
return true;
}
return false;
}
}

4、组件注册-自定义TypeFilter指定过滤规则的更多相关文章

  1. 组件注册-自定义TypeFilter指定过滤规则

    组件注册-自定义TypeFilter指定过滤规则 4.1 FilterType.ANNOTATION 按照注解方式 4.2 FilterType.ASSIGNABLE_TYPE 按照给定的类型 @Co ...

  2. 【Spring注解驱动开发】自定义TypeFilter指定@ComponentScan注解的过滤规则

    写在前面 Spring的强大之处不仅仅是提供了IOC容器,能够通过过滤规则指定排除和只包含哪些组件,它还能够通过自定义TypeFilter来指定过滤规则.如果Spring内置的过滤规则不能够满足我们的 ...

  3. 自定义的AdBlock过滤规则

    自定义的AdBlock过滤规则 # 屏蔽百度首页的广告流 www.baidu.com##DIV[id="u1"] www.baidu.com##DIV[id="qrcod ...

  4. 【spring 注解驱动开发】spring组件注册

    尚学堂spring 注解驱动开发学习笔记之 - 组件注册 组件注册 1.@Configuration&@Bean给容器中注册组件 2.@ComponentScan-自动扫描组件&指定扫 ...

  5. Logback中如何自定义灵活的日志过滤规则

    当我们需要对日志的打印要做一些范围的控制的时候,通常都是通过为各个Appender设置不同的Filter配置来实现.在Logback中自带了两个过滤器实现:ch.qos.logback.classic ...

  6. Spring注解开发-全面解析常用注解使用方法之组件注册

    目录 1. @Configuration 2. @ComponentScan excludeFilters includeFilters 使用自定义TypeFilter 3. @Bean @Scope ...

  7. 【Spring注解驱动开发】组件注册-@ComponentScan-自动扫描组件&指定扫描规则

    写在前面 在实际项目中,我们更多的是使用Spring的包扫描功能对项目中的包进行扫描,凡是在指定的包或子包中的类上标注了@Repository.@Service.@Controller.@Compon ...

  8. 3、组件注册-@ComponentScan-自动扫描组件&指定扫描规则

    3.组件注册-@ComponentScan-自动扫描组件&指定扫描规则 3.1 xml方式 benas.xml 导入context命名空间 <?xml version="1.0 ...

  9. spring02-组件注册-@ComponentScan-自动扫描组件&指定扫描规则

    上一篇我们讲到,讲@Bean注解标在某个方法上,那么ioc容器启动的时候就会将方法返回值放到ioc容器中 在开发中,实际上包扫描用的比较多,接下来我们会介绍两种方式一种是基于xml,一种是基于注解. ...

随机推荐

  1. springcloud(六,多个服务提供者)

    spring cloud (一.服务注册demo_eureka) spring cloud (二.服务注册安全demo_eureka) spring cloud (三.服务提供者demo_provid ...

  2. 如何为python 2.7安装tensorflow?

    “TensorFlow在Windows上支持Python 3.5.x和3.6.x.” 因此,您无法在Windows上使用Python 2.7的tensorflow 如果您被迫使用Python 2.7, ...

  3. k8s本地部署

    k8s是什么 Kubernetes是容器集群管理系统,是一个开源的平台,可以实现容器集群的自动化部署.自动扩缩容.维护等功能. Kubernetes 具有如下特点: 便携性: 无论公有云.私有云.混合 ...

  4. 二进制安装K8S集群V1.16.3

    centos linux7.5 cat > /etc/hosts << EOF 192.168.199.221 master 192.168.199.222 node1 192.16 ...

  5. 洛谷疯狂coding~

    1.关于数学建模思想在coding之中的应用. 将马路作为一条数轴,每棵树的位置作为数轴上的坐标点,再将坐标点与数组的下标联系到一起,完成建模. 2.本题坑点在于对“其中有多少个数,恰好等于集合中另外 ...

  6. Prometheus 监控Docker服务器及Granfanna可视化

    Prometheus 监控Docker服务器及Granfanna可视化 cAdvisor(Container Advisor)用于收集正在运行的容器资源使用和性能信息. 使用Prometheus监控c ...

  7. 【java】Execption的 e.getMessage()为null的解决方法

    ================================ 场景: 当代码出现异常时通常都需要将异常信息写入到日志中,异常信息越详细越有利于问题的排查.而通过的Exception.getMess ...

  8. nginx 配置反向代理,负载均衡实战解析

    前言:NGINX的反向代理和负载均衡是网站架构中经常用到的一种高并发,高可用的方案,下面我们直接实战操作,当然理论也是要的. 一.反向代理 过程:反向代理:客户端 一>代理 <一> ...

  9. IdentityService4学习笔记之Authorization Code

    前文 本文所有内容来自官方文档,如果有写不明白的地方请下方留言或查看官方文档. 今天介绍Authorization Code模式,适用于保密类型的客户端,保密类型客户端可以理解为在服务器端生成页面(比 ...

  10. DISK2VHD 转win2008 无法启动

    windows 2008R2物理机,使用微软的DISK2VHD转换成虚拟盘,挂到虚拟机上,无法启动只有光标闪.找来window2008安装盘 选择“修复windows系统”, 调出cmd命令提示符Bo ...