@ComponentScan 扫包

Posted li-lun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了@ComponentScan 扫包相关的知识,希望对你有一定的参考价值。

  在容器中读取Bean的信息,如遇到对扫包做

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);
        }

    }
}

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

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

1、排除用法 excludeFilters(排除@Controller注解的,和testService的)

@ComponentScan(basePackages = {"com.tuling.testcompentscan"},excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION,value  = {Controller.class}),
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,value = {TulingService.class})
})

2、包含用法includeFilters,需要注意若使用包含用法,需要把useDefaultFilters属性设置为false,默认true代表扫全部。排除掉包含Controller、Service

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

3、自定义过滤类型

@ComponentScan(basePackages = {"com.tuling.testcompentscan"},includeFilters = {
        @ComponentScan.Filter(type = FilterType.CUSTOM,value = TestFilterType.class)
},useDefaultFilters = false)
public class TestFilterType 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;
    }
}

 

组合使用,根据自定义类型排除bean,同时使用包含排除掉Repository的bean

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

 

 

@ComponentScan.Filter注解的  tyoe类型

   FilterType.ANNOTATION 表示      @Controller @Service @Repository  @Compent

   FilterType.ASSIGNABLE_TYPE   指定类型

   FilterType.CUSTOM 自定义类型  @ComponentScan.Filter(type =FilterType.ASSIGNABLE_TYPE,value = {TestService.class})   /*TestService 继承 TypeFilter 自定义过滤*/

  

 

以上是关于@ComponentScan 扫包的主要内容,如果未能解决你的问题,请参考以下文章

启动时候报错由于没有扫包 error creating bean with name

一起写框架-Ioc内核容器的实现-基础功能-ComponentScan支持组件注解限制

注解@componentscan

@componentscan注解的用法和作用

@ComponentScan 无参数

-002-@ComponentScan@Autowired的用法