Spring - @CompentScan全解

举报
小工匠 发表于 2021/09/10 01:34:11 2021/09/10
【摘要】 文章目录 基本使用excludeFiltersincludeFilters @ComponentScan.Filter4种类型自定义FilterType 基本使用 在配置...


在这里插入图片描述


基本使用

在配置类上写@CompentScan注解来进行包扫描

@Configuration
@ComponentScan(basePackages = {"com.artisan"})
public class MainConfig {

}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5

excludeFilters

@Configuration
@ComponentScan(basePackages = {"com.artisan"},excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION,value = {Controller.class}),
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,value = {ArtisanService.class})
})
public class MainConfig {
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

这里会 排除标注了@Controller注解 和 特定的ArtisanService ,不会被加载到IOC容器中。

在这里插入图片描述


includeFilters

若使用包含的用法, 需要把useDefaultFilters属性设置为false(true表示扫描全部的)

@Configuration
@ComponentScan(basePackages = {"com.artisan"},includeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION,value = {Controller.class, Service.class})
},useDefaultFilters = false)
public class MainConfig {
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

在这里插入图片描述


@ComponentScan.Filter

4种类型

在这里插入图片描述

  • 注解形式的FilterType.ANNOTATION 比如@Controller @Service @Repository @Compent

  • 指定类型的 FilterType.ASSIGNABLE_TYPE

    @ComponentScan.Filter(type =
    FilterType.ASSIGNABLE_TYPE,value = {ArtisanService.class})
    
        
       
    • 1
    • 2
  • aspectj类型的 FilterType.ASPECTJ(不常用)

  • 正则表达式的 FilterType.REGEX(不常用)

  • 自定义的 FilterType.CUSTOM


自定义FilterType


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;

 
public class ArtisanFilterType implements TypeFilter {

    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        //获取当前类的注解源信息
        AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();

        //获取当前类的class的源信息
        ClassMetadata classMetadata = metadataReader.getClassMetadata();
        //获取当前类的资源信息
        Resource resource =  metadataReader.getResource();
        System.out.println("类的路径:"+classMetadata.getClassName());
        if(classMetadata.getClassName().contains("dao")) {
            return true;
        }
        return false;
    }
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

引用自定义类型的Filter

@ComponentScan(basePackages = {"com.tuling.testcompentscan"},excludeFilters = { 
        @ComponentScan.Filter(type = FilterType.CUSTOM,value = ArtisanFilterType.class)
},includeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION,value = Repository.class)
})

  
 
  • 1
  • 2
  • 3
  • 4
  • 5

测试的话,打印下beanDefinition 即可

public class MainClass {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(MainConfig.class);
        String[] beanDefinationNames = ctx.getBeanDefinitionNames();
        for (String name:beanDefinationNames) {
            System.out.println("bean的定义信息:"+name);
        }

    }
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

在这里插入图片描述

文章来源: artisan.blog.csdn.net,作者:小小工匠,版权归原作者所有,如需转载,请联系作者。

原文链接:artisan.blog.csdn.net/article/details/115413646

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。