如何使用@component-scan排除不需要的类

导读:本篇文章讲解 如何使用@component-scan排除不需要的类,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

前因

我们都知道component-scan在springmvc或者springboot中可以扫描包路径,但是我们如何使用里面的属性排除不需要扫描的类?

方式一

使用spring的xml配置方式实现,这个是基本功,知道这种方式,那么注解方式就容易理解了

<!-- 定义项目扫描包的路径,并且排除ApplicationContextConfig和WebSpringMVCServletConfig-->
<context:component-scan base-package="com.leo">
    <context:exclude-filter type="assignable" expression="com.leo.config.ApplicationContextConfig"/>
	<context:exclude-filter type="assignable" expression="com.leo.config.WebSpringMVCServletConfig"/>
</context:component-scan>

里面的type有以下几种类型,用法不一一举例了,大家可以自己尝试。

过滤器类型 描述
annotation 过滤器扫描使用注解标注的那些类,并通过expression指定注解全类名
assignable 过滤器扫描派生于expression属性指定的类
aspectj 过滤器扫描与expression属性所指定的AspectJ表达式所匹配的那些类
custom 使用自定义的org.springframework.core.type.TypeFliter实现类,并通过expression指定全类名
regex 过滤器扫描类的名称与expression属性指定正则表达式所匹配的那些类

方式二

这种是通过@component-scan方式在配置扫描包路径的时候直接不包含要排除的包。举例说明一下如下:
项目的包路径是:com.leo.configcon.leo.controllercon.leo.service
我不需要扫描com.leo.config,那么配置@component-scan的时候就可以使用下面的配置直接不用扫描com.leo.config

@ComponentScan(basePackages = {"con.leo.controller","con.leo.service"})

方式三

其实大部分的情况是我们只有com.leo.config下面的某个类需要排除,那么可以使用FilterType.ASSIGNABLE_TYPE排除。参考如下配置

@ComponentScan(basePackages = {"com.leo"}, excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {ApplicationContextConfig.class, WebSpringMVCServletConfig.class}))

方式四

如果上面的class非常多的话,使用上面的方式就不太合适了,那我们可以使用FilterType.ANNOTATION加自定义注解去实现。参考如下配置

@ComponentScan(basePackages = {"com.leo"}, excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = ScanIgnore.class))

自定义@ScanIgnore实现

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ScanIgnore {
}

方式五

也可通过自定义的过滤器去实现。使用FilterType.CUSTOM加上自定义的过滤器

@ComponentScan(basePackages = {"com.leo"}, excludeFilters = @ComponentScan.Filter(type = FilterType.CUSTOM, classes = MyTypeFilter.class))

自定义过滤器MyTypeFilter

public class MyTypeFilter implements TypeFilter{
    /**
     * 过滤当前类的信息,如果包含的则不需要扫描
     *
     * @param metadataReader 读取当前正在扫描的信息
     * @param metadataReaderFactory 可以获取到其他任何类的信息
     * @return
     * @throws IOException
     */
    @Override
    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();
        //包含这个包路径的则被拦截
        if(className.startsWith("com.leo.config")){
            System.out.println("===============>"+className);
            return false;
        }
        return true;
    }
}

方式六

通过表达式来实现排除,使用FilterType.REGEX,配合pattern来实现。参考配合如下:

@ComponentScan(basePackages = {"com.leo"}, excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.leo.config.*"))

总结

以上都是通过excludeFilters实现排除不需要类的方式,如果是反向操作,需要添加需要的类,则将上面的关键字替换为includeFilters就好了。如果是配置文件的实现则

<context:exclude-filter></context:exclude-filter>
替换成
<context:include-filter></context:include-filter>

spring的配置文件是基础,后面的无论是springmvc亦或者是springboot都是在这个基础上扩展的,所以打好基础很重要。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/72701.html

(0)
小半的头像小半

相关推荐

半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!