码迷,mamicode.com
首页 > 其他好文 > 详细

@ComponentScan 扫包

时间:2020-04-26 17:19:41      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:继承   nta   exception   获取   names   config   turn   ade   one   

  在容器中读取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 扫包

标签:继承   nta   exception   获取   names   config   turn   ade   one   

原文地址:https://www.cnblogs.com/li-lun/p/12780485.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!