今天给大家分享一下Spring中@ComponentScan注解的用法。希望对您有所帮助!1、@ComponentScan注解的作用作用是定义包扫描规则,然后根据定义的规则找出哪些类需要自动组装到springbean容器中,然后交给spring进行统一管理。说明:所有标有@Controller、@Service、@Repository、@Component的类都可以被spring扫描到。2、@ComponentScan注解属性介绍2.1value指定要扫描的包路径2.2excludeFilters(排除规则)excludeFilters=Filter[]指定指定包扫描时根据规则排除的组件2.3includeFilters(包含规则)includeFilters=Filter[]指定扫描包时,根据规则指定要包含的组件。注意:必须设置useDefaultFilters=false(系统默认为true,需要手动设置)includeFilters包含过滤规则才能生效。2.4FilterType属性FilterType.ANNOTATION:根据注解过滤FilterType.ASSIGNABLE_TYPE:根据给定的类型指定具体的类,子类也会被扫描到FilterType.ASPECTJ:使用ASPECTJ表达式FilterType.REGEX:正则FilterType.CUSTOM:自定义ruleuseDefaultFilters:配置是否开启可以检测@Component,@Repository,@Service,@Controller注解的类,Java8语法可以指定多个@ComponentScan,Java83以下可以用@ComponentScans()配置多个规则,Example3.1各种过滤规则示例//includeFilters用法includeAnimal.class类可以被扫描,包括它的子类@ComponentScan(value="com.spring"includeFilters={@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE,classes={Animal.class})})//excludeFilters用法排除包含@Controller注解的类@ComponentScan(value="com.spring",excludeFilters={@ComponentScan.Filter(type=FilterType.ANNOTATION,classes={Controller.class}),})//ComponentScans用法@ComponentScans(value={@ComponentScan(value="com.spring",includeFilters={@ComponentScan.Filter(类型=FilterType.ANNOTATION,classes={Controller.class})},useDefaultFilters=false),@ComponentScan(value="com.spring",excludeFilters={@ComponentScan.Filter(type=FilterType.ANNOTATION,classes={Repository.class})})})*///@ComponentScan//Java8语法可以指定多个@ComponentScan,//@ComponentScans()可以在Java8以下配置多个规则@ComponentScan(value="com.spring",excludeFilters={@ComponentScan.Filter(type=FilterType.ANNOTATION,classes={Controller.class,Controller.class}),},includeFilters={@ComponentScan.Filter(type=FilterType.ANNOTATION,classes={Controller.class,Controller.class}),})3.2自定义过滤规则需要新建TestTypeFilter.javapackagecom.spring.config;importorg.springframework.core.io.Res来源;导入org.springframework.core.type.AnnotationMetadata;导入org.springframework.core.type.ClassMetadata;导入org.springframework.core.type.classreading.MetadataReader;导入org.springframework.core.type.classreading.MetadataReaderFactory;importorg.springframework.core.type.filter.TypeFilter;importjava.io.IOException;/***metadataReader读取当前正在扫描的类信息*metadataReaderFactory可以获取任何其他类的信息*/publicclassTestTypeFilterimplementsTypeFilter{publicbooleanmatch(MetadataReadermetadataReader,MetadataReaderFactorymetadataReaderFactory)throwsIOException{//获取当前类注解信息AnnotationMetadataannotationMetadata=metadataReader.getAnnotationMetadata();//获取当前正在扫描的类信息ClassMetadataclassMetadata=metadataReader.getClass/Metadata(/获取当前类资源信息(比如类的文件路径)Resourceresource=metadataReader.getResource();StringclassName=classMetadata.getClassName();System.out.println("类名:"+cl驴名);如果(className.contains(“控制器”)){返回真;}else{返回错误;}}}3.3新建测试类TestComponentScan.javapackagecom.spring.test;importcom.spring.config.TestComponentScanConfig;importorg.springframework.context.annotation.AnnotationConfigApplicationContext;公共类TestComponentScan{publicstaticvoidmain(String[]args){AnnotationConfigApplicationContextannotationContext=newAnnotationConfigApplicationContext(TestComponentScanConfig.class);StringDefinitions的String[]名称=注释;名称:名称){System.out.println(名称);具体运行效果可以查看控制台输出,是否符合预期,如有不明之处欢迎交流
