网站内容的作用,网站后台管理系统素材,手机无法安装wordpress,图片加文字在线制作目录 1、ComponentScan注解的作用 2、ComponentScan注解属性介绍 2.1 value 2.2 excludeFilters#xff08;排除规则#xff09; 2.3 includeFilters#xff08;包含规则#xff09; 2.4 FilterType属性 3、示例 3.1 各种过滤过滤规则示例 3.2 自定义过滤规则 需要新建 Tes… 目录 1、ComponentScan注解的作用 2、ComponentScan注解属性介绍 2.1 value 2.2 excludeFilters排除规则 2.3 includeFilters包含规则 2.4 FilterType属性 3、示例 3.1 各种过滤过滤规则示例 3.2 自定义过滤规则 需要新建 TestTypeFilter.java 3.3 新建测试类 TestComponentScan.java 今天给大家分享Spring中ComponentScan注解的用法希望对大家能有所帮助 1、ComponentScan注解的作用 ComponentScan注解一般和Configuration注解一起使用主要的作用就是定义包扫描的规则然后根据定义的规则找出哪些需类需要自动装配到spring的bean容器中然后交由spring进行统一管理。 说明针对标注了Controller、Service、Repository、Component 的类都可以别spring扫描到。 2、ComponentScan注解属性介绍 2.1 value 指定要扫描的包路径 2.2 excludeFilters排除规则 excludeFiltersFilter[] 指定包扫描的时候根据规则指定要排除的组件 2.3 includeFilters包含规则 includeFilters Filter[] 指定包扫描的时候根据规则指定要包含的组件. 注意:要设置useDefaultFilters false系统默认为true需要手动设置 includeFilters包含过滤规则才会生效。 2.4 FilterType属性 FilterType.ANNOTATION按照注解过滤 FilterType.ASSIGNABLE_TYPE按照给定的类型,指定具体的类子类也会被扫描到 FilterType.ASPECTJ使用ASPECTJ表达式 FilterType.REGEX正则 FilterType.CUSTOM自定义规则 useDefaultFilters: 配置是否开启可以对加ComponentRepositoryServiceController注解的类进行检测 针对Java8 语法可以指定多个ComponentScanJava8以下可以用 ComponentScans() 配置多个规则 3、示例 3.1 各种过滤过滤规则示例 // includeFilters 用法 包含Animal.class类可以被扫描到包括其子类
ComponentScan(value com.springincludeFilters {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(type FilterType.ANNOTATION, classes {Controller.class})}, useDefaultFilters false) ,ComponentScan(value com.spring, excludeFilters {ComponentScan.Filter(type FilterType.ANNOTATION, classes { Repository.class})})}
)*/// ComponentScan
// 针对Java8 语法可以指定多个ComponentScanJava8以下可以用 //ComponentScans() 配置多个规则
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.java package com.spring.config;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;/*** metadataReader 读取到当前正在扫描的类信息* metadataReaderFactory 可以获取到其他任何类的信息*/
public class TestTypeFilter implements TypeFilter {public 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);if (className.contains(controller)) {return true;} else {return false;}}
} 3.3 新建测试类 TestComponentScan.java package com.spring.test;import com.spring.config.TestComponentScanConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class TestComponentScan {public static void main(String[] args) {AnnotationConfigApplicationContext annotationContext new AnnotationConfigApplicationContext(TestComponentScanConfig.class);String[] names annotationContext.getBeanDefinitionNames();for (String name : names) {System.out.println(name);}}
} 具体的运行效果可以查看控制台输出结果是否和预期的一样具体有不清楚的欢迎沟通交流。