一、@ComponentScan的作用
1.指定包扫描路径;
2.指定要扫描的类;
4.指定要过滤的类型
5.默认将@Controller,@Service,@Repository,@Component注解的类装配到spring容器中
原因:
@Controller,@Service,@Repository都对@Component进行了封装
二、用法
1.在SpringBoot中,启动类默认扫描所在包下的所有类,如果要扫描的类和启动类不再同一个包里,则不会扫描到,此时需要在启动类上使用@ComponentScan去指定扫描位置
@ComponentScan(basePackages = {"com.zrx.springboot.controller","com.zrx.springboot.service"})
2.basePackageClasses属性的使用:
@ComponentScan(basePackageClasses = {
IndexController.class,
ApiController.class
})
3.basePackagess属性的使用:
@ComponentScan(basePackages = {
"com.zrx.springboot.controller",
"com.zrx.springboot.service"
})
4.value属性的使用:和basePackagess用法一样
5.includeFilters属性:指定包扫描的时候根据规则指定要包含的组件。
6.excludeFilters属性:指定包扫描的时候根据规则指定要排除的组件
7.FilterType属性:
FilterTpye.ANNOTATION:按照注解过滤;
FilterType.ASSIGNABLE_TYPE:根据指定的类型,包括子类;
FilterType.ASPECTJ:使用ASPECTJ表达式;
FilterType.REGEX:正则;
FilterType.CUSTOM:自定义规则;
useDefaultFilters: 配置是否开启可以对加@Component,@Repository,@Service,@Controller注解的类进行检测。