上面不重要的方法我已经删掉了,首先获取use-default-filters属性,传入到ClassPathBeanDefinitionScanner构造器中判断是否使用默认的过滤器,如果是就调用registerDefaultFilters方法将@Component注解过滤器添加到includeFilters属性中;创建后紧接着调用了parseTypeFilters方法去解析include-filter和exclude-filter子标签,并分别添加到includeFilters和excludeFilters标签中(关于这两个标签的作用这里不再赘述),所以这一步就是创建包含过滤器的class扫描器,接着就可以调用scan方法完成扫描注册了(如果我们要自定义注解是不是也可以这样实现呢?)。
protected Set<BeanDefinitionHolder> doScan(String... basePackages) { Assert.notEmpty(basePackages, "At least one base package must be specified"); Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<>(); for (String basePackage : basePackages) { // 这里就是实际扫描符合条件的类并封装为ScannedGenericBeanDefinition对象 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 instanceof AbstractBeanDefinition) { postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName); } // 解析@Lazy、@Primary、@DependsOn等注解 if (candidate instanceof AnnotatedBeanDefinition) { AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate); } if (checkCandidate(beanName, candidate)) { BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName); definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry); beanDefinitions.add(definitionHolder); registerBeanDefinition(definitionHolder, this.registry); } } } return beanDefinitions; } public Set<BeanDefinition> findCandidateComponents(String basePackage) { if (this.componentsIndex != null && indexSupportsIncludeFilters()) { return addCandidateComponentsFromIndex(this.componentsIndex, basePackage); } else { // 主要看这,扫描所有符合条件的class文件并封装为ScannedGenericBeanDefinition return scanCandidateComponents(basePackage); } } private Set<BeanDefinition> scanCandidateComponents(String basePackage) { Set<BeanDefinition> candidates = new LinkedHashSet<>(); try { String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + resolveBasePackage(basePackage) + 'http://www.likecs.com/' + this.resourcePattern; // 获取class文件并加载为Resource Resource[] resources = getResourcePatternResolver().getResources(packageSearchPath); boolean traceEnabled = logger.isTraceEnabled(); boolean debugEnabled = logger.isDebugEnabled(); for (Resource resource : resources) { if (traceEnabled) { logger.trace("Scanning " + resource); } if (resource.isReadable()) { try { // 获取SimpleMetadataReader对象,该对象持有AnnotationMetadataReadingVisitor对象 MetadataReader metadataReader = getMetadataReaderFactory().getMetadataReader(resource); if (isCandidateComponent(metadataReader)) { // 将AnnotationMetadataReadingVisitor对象设置到ScannedGenericBeanDefinition中 ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader); sbd.setResource(resource); sbd.setSource(resource); if (isCandidateComponent(sbd)) { if (debugEnabled) { logger.debug("Identified candidate component class: " + resource); } candidates.add(sbd); } } } } } } return candidates; }这一次搞懂Spring自定义标签以及注解解析原理 (2)
内容版权声明:除非注明,否则皆为本站原创文章。