componentscan默认扫描,@componentscan 配置多个扫描包

  componentscan默认扫描,@componentscan 配置多个扫描包

  

目录

无注解方式组件扫描使用注解方式@ComponentScan使用@ComponentScan的扫描规则

 

  

无注解方式component-scan使用

之前,我们需要扫描工程下一些类上所标注的注解,这些常用注解有:

 

  @控制器、@服务、@组件、@存储库

  通过在春天的配置文件中配置上下文:组件-扫描扫描对应包下扫描这些注解的方式:

  豆子xmlns= http://www。spring框架。org/schema/beans xmlns : xsi= http://www。w3。org/2001/XML架构实例 xmlns : p= http://www。spring框架。org/schema/p xmlns : context= http://www。spring框架。 xsi 3360 schema location= http://www。spring- @Controller,@Service,@Component,@ Repository-context : Component-scan base-package= com。杰通。春天//豆子

  

注解方式@ComponentScan使用

建三个类,依次将

 

  @控制器、@存储库、@服务,标注这些类:

  图一

  现在通过使用注解@ComponentScan的方式来扫描所在包下面的这些类:之前定义的人员配置修改:

  包com。杰通。配置;导入org。spring框架。语境。注释。豆;导入org。spring框架。语境。注释。组件可以;导入org。spring框架。语境。注释。配置;导入com。杰通。春天。人;@ Configuration @ components可以( com。jek tong )公共类Person config { @ Bean( Person 01 )公共Person Person(){ return new Person(李四,21);}}测试,看是否扫描到这些注解所标注的类:PersonTest.java

  @ test public void test 02(){应用程序上下文AC=new AnnotationConfigApplicationContext(person config。类);人豆=AC。获取bean(人。类);系统。出去。println(bean);string[]bean定义名称=AC。getbean定义名称();for(String String : bean定义名称){ system。出去。println(字符串);}}测试效果:除了春天要自动加载的配置类以外也显示了刚才添加的配置类:

  图2

  为何会出现人员配置,因为@配置本身就是@组件注解的:

  图3

  

@ComponentScan的扫描规则

如果需要指定配置类的扫描规则,@ComponentScan提供对应的扫描方式@F

 

  ilter进行配置类的过滤:

  

// 扫描包的时候只规定扫描一些注解配置类。Filter[] includeFilters() default {};// 扫描包的时候可以排除一些注解配置类。 Filter[] excludeFilters() default {};

Filter其实也是一个注解,相当于@ComponentScan的子注解,可以看图4:

 

  

 

  图4

  Filter对应的过滤规则如下:

  第一种:扫描包的时候只规定扫描一些注解配置类【includeFilters】。

  使用这个includeFilters过滤规则,必须解除默认的过滤规则,

  使用【useDefaultFilters = false】:

  

package com.jektong.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.ComponentScan.Filter;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.FilterType;import org.springframework.stereotype.Controller;import com.jektong.spring.Person;@Configuration@ComponentScan(value = "com.jektong",includeFilters = {@Filter(type = FilterType.ANNOTATION,value= {Controller.class})},useDefaultFilters = false )public class PersonConfig {@Bean("person01")public Person person() {return new Person("李四",21);}}

这样就只会扫描用@Controller,标注的配置类交给Spring容器中了:

 

  

 

  图5

  第二种:扫描包的时候可以排除一些注解配置类【excludeFilters】。

  

 

  图6

  @Filter看上图,有5种不同类型的过滤策略。拿第一种举例,我们需要过滤使用@Controller注解的配置类:

  

package com.jektong.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.ComponentScan.Filter;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.FilterType;import org.springframework.stereotype.Controller;import com.jektong.spring.Person;@Configuration@ComponentScan(value = "com.jektong",excludeFilters = {@Filter(type = FilterType.ANNOTATION,value= {Controller.class})} )public class PersonConfig {@Bean("person01")public Person person() {return new Person("李四",21);}}

测试看一下发现图2中的personController不会交给Spring容器去管理了:

 

  

 

  图7

  上面的图6展示出5种不同类型的过滤策略,上面介绍了注解类型(FilterType.ANNOTATION),还有四种:

  重点看一下CUSTOM自定义扫描策略。

  

 

  从源码看,自定义扫描注解类型需要实现TypeFilter接口,下面就写一个实现类MyFilter.java:在实现类中可以自定义配置规则:

  

package com.jektong.config;import java.io.IOException;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;public class MyFilter implements TypeFilter {@Overridepublic boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)throws IOException {// 查看当前类的注解。AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();// 查看当前扫描类的信息ClassMetadata classMetadata = metadataReader.getClassMetadata();// 获取当前类资源Resource resource = metadataReader.getResource();String className = classMetadata.getClassName();System.out.println("className===>" + className);// 只要类名包含er则注册Spring容器if(className.contains("er")) {return true;}return false;}}

测试:

 

  PersonConfig 中进行扫描:

  

package com.jektong.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.ComponentScan.Filter;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.FilterType;import org.springframework.stereotype.Controller;import com.jektong.service.PersonService;import com.jektong.spring.Person;@Configuration@ComponentScan(value = "com.jektong",includeFilters = {@Filter(type = FilterType.CUSTOM,value= {MyFilter.class})},useDefaultFilters = false )public class PersonConfig {@Bean("person01")public Person person() {return new Person("李四",21);}}

可以看出扫描出包下面的类只要带er的全部扫描出来,并配置给Spring容器:

 

  

 

  ASSIGNABLE_TYPE:按照指定的类型去加载对应配置类:

  

package com.jektong.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.ComponentScan.Filter;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.FilterType;import org.springframework.stereotype.Controller;import com.jektong.service.PersonService;import com.jektong.spring.Person;@Configuration@ComponentScan(value = "com.jektong",includeFilters = {@Filter(type = FilterType.ASSIGNABLE_TYPE,value= {PersonService.class})},useDefaultFilters = false )public class PersonConfig {@Bean("person01")public Person person() {return new Person("李四",21);}}

尽管我们将PersonService.java上的注解去掉,使用ASSIGNABLE_TYPE依然会加载出来(自行测试)。

 

  ASPECTJ与REGEX基本不用,不用了解。

  以上就是@ComponentScan的具体用法,该兴趣的话可以看一下源码。

  到此这篇关于详解Spring系列之@ComponentScan自动扫描组件的文章就介绍到这了,更多相关Spring @ComponentScan内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: