/**
* Register the default filter for {@link Component @Component}.
* <p>This will implicitly register all annotations that have the
* {@link Component @Component} meta-annotation including the
* {@link Repository @Repository}, {@link Service @Service}, and
* {@link Controller @Controller} stereotype annotations.
* <p>Also supports Java EE 6's {@link javax.annotation.ManagedBean} and
* JSR-330's {@link javax.inject.Named} annotations, if available.
*
*/@SuppressWarnings("unchecked")protectedvoidregisterDefaultFilters(){//过滤所有包含Component组件的类,看上方的注释,说过滤@Component,@Repository,@Controller,//其实后俩个都有@Component作为父注解this.includeFilters.add(newAnnotationTypeFilter(Component.class));//确保类注解是 ClassPathScanningCandidateComponentProvider的类加载器ClassLoader cl =ClassPathScanningCandidateComponentProvider.class.getClassLoader();try{this.includeFilters.add(newAnnotationTypeFilter(((Class<?extendsAnnotation>)ClassUtils.forName("javax.annotation.ManagedBean", cl)),false));
logger.trace("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");}catch(ClassNotFoundException ex){// JSR-250 1.1 API (as included in Java EE 6) not available - simply skip.}try{this.includeFilters.add(newAnnotationTypeFilter(((Class<?extendsAnnotation>)ClassUtils.forName("javax.inject.Named", cl)),false));
logger.trace("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");}catch(ClassNotFoundException ex){// JSR-330 API not available - simply skip.}}
打开doScan方法
protectedSet<BeanDefinitionHolder>doScan(String... basePackages){Assert.notEmpty(basePackages,"At least one base package must be specified");Set<BeanDefinitionHolder> beanDefinitions =newLinkedHashSet<>();for(String basePackage : basePackages){// 扫描到有注解的类并封装成 BeanDefinition对象Set<BeanDefinition> candidates =findCandidateComponents(basePackage);for(BeanDefinition candidate : candidates){ScopeMetadata scopeMetadata =this.scopeMetadataResolver.resolveScopeMetadata(candidate);
candidate.setScope(scopeMetadata.getScopeName());String beanName =this.beanNameGenerator.generateBeanName(candidate,this.registry);if(candidate instanceofAbstractBeanDefinition){postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);}if(candidate instanceofAnnotatedBeanDefinition){AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);}if(checkCandidate(beanName, candidate)){BeanDefinitionHolder definitionHolder =newBeanDefinitionHolder(candidate, beanName);
definitionHolder =AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder,this.registry);
beanDefinitions.add(definitionHolder);//注册beanregisterBeanDefinition(definitionHolder,this.registry);}}}return beanDefinitions;}