@ComponentScan注解
Spring中@ComponentScan注解的作用就是根据定义的扫描路径,把符合扫描规则的类装配到spring的bean容器中。
源码
basePackages与value:? 用于指定包的路径,进行扫描
basePackageClasses: 用于指定某个类的包的路径进行扫描
nameGenerator: bean的名称的生成器
useDefaultFilters: 是否开启对@Component,@Repository,@Service,@Controller的类进行检测
includeFilters: 包含的过滤条件 FilterType.ANNOTATION:按照注解过滤,FilterType.ASSIGNABLE_TYPE:按照给定的类型,FilterType.ASPECTJ:使用ASPECTJ表达式,FilterType.REGEX:正则,FilterType.CUSTOM:自定义规则
excludeFilters: 排除的过滤条件,用法和includeFilters一样
注解方式
总结
Spring ComponentScan注解有以下特性:
-
自定扫描路径下边带有@Controller,@Service,@Repository,@Component注解加入spring容器
-
通过includeFilters加入扫描路径下没有以上注解的类加入spring容器
-
通过excludeFilters过滤出不用加入spring容器的类
-
自定义增加了@Component注解的注解方式
@SpringBootApplication
@Component
@ComponentSc