您好,登录后才能下订单哦!
这篇文章将为大家详细讲解有关Springboot中如何使用@ComponentScan中excludeFilters,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
首先这边先给出基础的service类,该类我想在springboot加载过程中取消自动注入。
package com.wyq.test;import org.springframework.stereotype.Service;/** * @Author: wuyiqi * @Date: 2021-05-10 10:35 * @Description: */@Service("myBeanService")public class MyService { }
@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就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。