/** * Perform a scan within the specified base packages, returning the registered bean definitions. * * <p>This method does <i>not</i> register an annotation config processor but rather leaves this * up to the caller. * * @param basePackages the packages to check for annotated classes * @return set of beans registered if any for tooling registration purposes (never {@code null}) */ protected Set<BeanDefinitionHolder> doScan(String... basePackages) { Assert.notEmpty(basePackages, "At least one base package must be specified"); Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<BeanDefinitionHolder>(); mylog.debug("遍历basePackages,获取包路径下面的.class所有类"); for (String basePackage : basePackages) { Set<BeanDefinition> candidates = findCandidateComponents(basePackage); mylog.debug("获取到有注解的bean集合并遍历,每个bean设置scope"); 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); } 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); mylog.debug("登记AnnotatedBeanDefinition类型的bean"); registerBeanDefinition(definitionHolder, this.registry); } } } return beanDefinitions; }
@Override protected Set<BeanDefinitionHolder> doScan(String... basePackages) { Assert.notEmpty(basePackages, "At least one base package must be specified"); Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<BeanDefinitionHolder>(); for (String basePackage : basePackages) { 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); } if (candidate instanceof AnnotatedBeanDefinition) { processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate); } ScannedGenericBeanDefinition bd = (ScannedGenericBeanDefinition) candidate; // 通用bean扫描 bd.setBeanClassName(HessianServiceExporter.class.getName()); // 设置bean名称 bd.setBeanClass(HessianServiceExporter.class); // 设置bean类 bd.getPropertyValues().add("service", registry.getBeanDefinition(beanName)); // 设置属性 service String[] interfaces = bd.getMetadata().getInterfaceNames(); // 获得元数据、接口名称 if (interfaces == null || interfaces.length == 0) continue; Class interf = null; try { interf = Class.forName(interfaces[0]); // 获得接口 } catch (ClassNotFoundException e) { continue; } bd.getPropertyValues().add("serviceInterface", interf); // 设置属性 serviceInterface candidate = bd; BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, "/" + beanName); // 新bean定义持有 definitionHolder = applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry); // 应用范围代理模式 beanDefinitions.add(definitionHolder); // 将bean定义持有放入bean定义持有set中 registerBeanDefinition(definitionHolder, this.registry); // 注册bean定义 } } if (beanDefinitions.isEmpty()) { System.out.println("not service be scaned"); } else { for (BeanDefinitionHolder holder : beanDefinitions) { AnnotatedBeanDefinition definition = (AnnotatedBeanDefinition) holder.getBeanDefinition(); // 注释的bean定义 System.out.println("service:" + holder.getBeanName()); // 注释的bean定义名称 System.out.println( "service:" + definition.getMetadata().getAnnotationTypes()); // 注释的bean定义类型 } } return beanDefinitions; }