由于Spring源码过于庞大,文章中不会列出细节,须要大家花时间下去研究哦
spring类扫描主要依赖ClassPathBeanDefinitionScanner类(源码一中分析过)
ClassPathBeanDefinitionScanner实例化时最终会调用ClassPathScanningCandidateComponentProvider#registerDefaultFilters方法:
useDefaultFilters是spring默认传的true,看下这个registerDefaultFilters方法:
可以看到其提供了includeFilters的成员变量,存放可供扫描的注解,这里可以看到三种情况的类可以注册为bean。
1.添加了spring原生@Component注解的类
2.添加了javax的@ManagedBean注解的类
3.添加了javax的@Named注解的类
可以看下ClassPathBeanDefinitionScanner具体的扫描过程:
ClassPathBeanDefinitionScanner#scan(会传一个包路径,扫描这个包下的类)--->
ClassPathBeanDefinitionScanner#doScan--->
ClassPathScanningCandidateComponentProvider#findCandidateComponents--->
ClassPathScanningCandidateComponentProvider#scanCandidateComponents--->
ClassPathScanningCandidateComponentProvider#isCandidateComponent
isCandidateComponent方法就会判断是不是符合上面的三种情况,符合的话就注册为bd。这里的bd是ScannedGenericBeanDefinition:
这里面存在两个filters,一个includeFilters,一个excludeFilters,看名字也知道啥意思,同时有两个方法支持添加这两个list中加数据:
这两个方法的调用是通过解析@ComponentScan注解时使用的:
所以可以通过@ComponentScan注解的includeFilters和excludeFilters属性指定须要排除的某些bean,这里的typeFiltersFor方法会将注解中的属性信息转换为TypeFilter对象,可以看下这个方法理解这两个属性具体如何用,给个使用总结吧:
1.FilterType.ANNOTATION,匹配指定的注解,配合classes属性使用,指定一个注解类的class
2.FilterType.ASSIGNABLE_TYPE,匹配指定的普通类,配合classes属性使用,指定一个普通类的class
3/.FilterType.ASPECTJ,配合pattern属性使用,通配符匹配
4.FilterType.REGEX,配合pattern属性使用,通配符匹配