Spring - @CompentScan全解 旧城等待, 2022-10-22 02:55 169阅读 0赞 ### 文章目录 ### * 基本使用 * * excludeFilters * includeFilters * @ComponentScan.Filter * * 4种类型 * 自定义FilterType ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lhbmdzaGFuZ3dlaQ_size_16_color_FFFFFF_t_70] -------------------- # 基本使用 # 在配置类上写@CompentScan注解来进行包扫描 @Configuration @ComponentScan(basePackages = { "com.artisan"}) public class MainConfig { } -------------------- ## 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 { } 这里会 排除标注了@Controller注解 和 特定的ArtisanService ,不会被加载到IOC容器中。 ![在这里插入图片描述][20210403105845256.gif_pic_center] -------------------- ## 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 { } ![在这里插入图片描述][20210403105857501.gif_pic_center] -------------------- # @ComponentScan.Filter # ## 4种类型 ## ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lhbmdzaGFuZ3dlaQ_size_16_color_FFFFFF_t_70 1] * 注解形式的FilterType.**ANNOTATION** `比如@Controller @Service @Repository @Compent` * **指定类型**的 FilterType.**ASSIGNABLE\_TYPE** @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,value = { ArtisanService.class}) * 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; } } 引用自定义类型的Filter @ComponentScan(basePackages = { "com.tuling.testcompentscan"},excludeFilters = { @ComponentScan.Filter(type = FilterType.CUSTOM,value = ArtisanFilterType.class) },includeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION,value = Repository.class) }) -------------------- 测试的话,打印下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); } } } ![在这里插入图片描述][20210403110601621.gif_pic_center] [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lhbmdzaGFuZ3dlaQ_size_16_color_FFFFFF_t_70]: /images/20221022/23ebba42b3464b8fbf51dce3056fb828.png [20210403105845256.gif_pic_center]: /images/20221022/94ab2aaf03824a1fa5f87ee0b442d0db.png [20210403105857501.gif_pic_center]: /images/20221022/0b09178b68c041b790becc577d4acb39.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lhbmdzaGFuZ3dlaQ_size_16_color_FFFFFF_t_70 1]: /images/20221022/84e26bcda72447298dd43c56e17a833c.png [20210403110601621.gif_pic_center]: /images/20221022/ae50647243464021b6d1125ec3c3a8e5.png
还没有评论,来说两句吧...