Springboot中如何使用@ComponentScan中excludeFilters

这篇文章将为大家详细讲解有关Springboot中如何使用@ComponentScan中excludeFilters,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

创新互联技术团队十多年来致力于为客户提供网站制作、成都做网站品牌网站设计网络营销推广、搜索引擎SEO优化等服务。经过多年发展,公司拥有经验丰富的技术团队,先后服务、推广了近1000家网站,包括各类中小企业、企事单位、高校等机构单位。

首先这边先给出基础的service类,该类我想在springboot加载过程中取消自动注入。

@Service注解的类

package com.wyq.test;import org.springframework.stereotype.Service;/** * @Author: wuyiqi * @Date: 2021-05-10 10:35 * @Description: */@Service("myBeanService")public class MyService {
}

@ComponentScan中excludeFilters使用

@ComponentScan可以设置includeFilters和excludeFilters,来自定义过滤器。一般excludeFilters用的比较多。

一、 过滤指定的类名

type = FilterType.ASSIGNABLE_TYPE是根据类class来过滤,后面classes指向类名

package com.wyq.test;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.FilterType;/** * @Author: wuyiqi * @Date: 2021-05-10 10:35 * @Description: */@SpringBootApplication@ComponentScan(value = "com.wyq.test", excludeFilters = {      @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {MyService.class})})public class TestApplication {   //和上面一样,省略}

这边注意下 MyService.class 就是上述的类,通过 FilterType.ASSIGNABLE_TYPE 可以直接指定这个类不进行自动注入。

二、过滤指定的注解

com.wyq.test包和子包下,排除有@Service注解的类

package com.wyq.test;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.FilterType;import org.springframework.stereotype.Service;/** * @Author: wuyiqi * @Date: 2021-05-10 10:35 * @Description: */@SpringBootApplication@ComponentScan(value = "info.pigg.study.java", excludeFilters = {      @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = {Service.class})})public class DictApplication {
}

三、自定义过滤

type = FilterType.CUSTOM,是自定义过滤,classes 指定的类要实现TypeFilter接口,在match方法中可以获取当前扫描到的类的信息,比如注解、类名和类路径

package com.wyq.test;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.FilterType;import org.springframework.stereotype.Service;/** * @Author: wuyiqi * @Date: 2021-05-10 10:35 * @Description: */@SpringBootApplication@ComponentScan(value = "info.pigg.study.java", excludeFilters = {      @ComponentScan.Filter(type = FilterType.CUSTOM, classes = {MyTypeFilter.class})})public class DictApplication {
}

在类名包含"MyService"时,match方法返回true,这样在excludeFilters时,包含"MyService"的类就会被排除掉

package com.wyq.test;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;/** * @Author: wuyiqi * @Date: 2021-05-10 10:35 * @Description: */public class MyTypeFilter implements TypeFilter
{   @Override   public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
   {      //获取当前类注解的信息      AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();      //获取当前类资源(类的路径)      Resource resource = metadataReader.getResource();

      ClassMetadata classMetadata = metadataReader.getClassMetadata();
      System.out.println("当前正在被扫描的类的类名" + classMetadata.getClassName());      if (classMetadata.getClassName().contains("MyService"))
      {         return true;
      }      return false;
   }
}

关于Springboot中如何使用@ComponentScan中excludeFilters就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

网页名称:Springboot中如何使用@ComponentScan中excludeFilters
分享URL:https://www.cdcxhl.com/article4/ihggoe.html

成都网站建设公司_创新互联,为您提供域名注册虚拟主机网站排名商城网站网站内链网站制作

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

外贸网站制作