Package Scanning of Spring IoC Source Code Analysis (Annotation-based)

In the previous article, Spring IoC source code analysis (based on annotations), we analyzed that after we start Spring by passing in a package path through the AnnotationConfigApplicationContext class, we will first initialize the filtering rules for package scanning. So let's take a look at the specific process of packet scanning today.

Or look at the following code:
AnnotationConfigApplicationContext class

//该构造函数会自动扫描以给定的包及其子包下的所有类,并自动识别所有的Spring Bean,将其注册到容器中
	public AnnotationConfigApplicationContext(String... basePackages) {
		//初始化
		this();
		//扫描包、注册bean
		scan(basePackages);
		refresh();
	}

We analyzed the this() method above, which will initialize the AnnotatedBeanDefinitionReader reader and the ClassPathBeanDefinitionScanner scanner, and initialize the scan filtering rules.
Next, let's take a look at the scan(basePackages) method:
keep following it, and find that the scan() method in the ClassPathBeanDefinitionScanner class is called

//调用类路径Bean定义扫描器入口方法
	public int scan(String... basePackages) {
		//获取容器中已经注册的Bean个数
		int beanCountAtScanStart = this.registry.getBeanDefinitionCount();

		//启动扫描器扫描给定包
		doScan(basePackages);

		// Register annotation config processors, if necessary.
		//注册注解配置(Annotation config)处理器
		if (this.includeAnnotationConfig) {
			AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
		}

		//返回注册的Bean个数
		return (this.registry.getBeanDefinitionCount() - beanCountAtScanStart);
	}

It can be seen that the main doScan(basePackages) method implements the scanning logic. Let's continue to follow it.

//类路径Bean定义扫描器扫描给定包及其子包
	protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
		Assert.notEmpty(basePackages, "At least one base package must be specified");
		//创建一个集合,存放扫描到Bean定义的封装类
		Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<>();
		//遍历扫描所有给定的包
		for (String basePackage : basePackages) {
			//调用父类ClassPathScanningCandidateComponentProvider的方法
			//扫描给定类路径,获取符合条件的Bean定义
			Set<BeanDefinition> candidates = findCandidateComponents(basePackage);
			//遍历扫描到的Bean
			for (BeanDefinition candidate : candidates) {
				//获取@Scope注解的值,即获取Bean的作用域
				ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
				//为Bean设置作用域
				candidate.setScope(scopeMetadata.getScopeName());
				//为Bean生成名称
				String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);
				//如果扫描到的Bean不是Spring的注解Bean,则为Bean设置默认值,
				//设置Bean的自动依赖注入装配属性等
				if (candidate instanceof AbstractBeanDefinition) {
					postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
				}
				//如果扫描到的Bean是Spring的注解Bean,则处理其通用的Spring注解
				if (candidate instanceof AnnotatedBeanDefinition) {
					//处理注解Bean中通用的注解,在分析注解Bean定义类读取器时已经分析过
					AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);
				}
				//根据Bean名称检查指定的Bean是否需要在容器中注册,或者在容器中冲突
				if (checkCandidate(beanName, candidate)) {
					BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);
					//根据注解中配置的作用域,为Bean应用相应的代理模式
					definitionHolder =
							AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
					beanDefinitions.add(definitionHolder);
					//向容器注册扫描到的Bean
					registerBeanDefinition(definitionHolder, this.registry);
				}
			}
		}
		return beanDefinitions;
	}

This large piece of code is basically the code that Spring scans to identify annotations and register beans with the IOC container.
In line 10, there is a findCandidateComponents(basePackage) method, which is the specific scanning logic.
Keep track:
ClassPathScanningCandidateComponentProvider class

//扫描给定类路径的包
	public Set<BeanDefinition> findCandidateComponents(String basePackage) {
		//spring5.0开始 索引 开启的话生成文件META-INF/spring.components 后面加载直接从本地文件读取(一般不建议开启 spring.index.ignore=true)
		if (this.componentsIndex != null && indexSupportsIncludeFilters()) {
			return addCandidateComponentsFromIndex(this.componentsIndex, basePackage);
		}
		else {
			return scanCandidateComponents(basePackage);
		}
	}

Here is an if judgment, we default to the branch in else, that is, the scanCandidateComponents(basePackage) method.

private Set<BeanDefinition> scanCandidateComponents(String basePackage) {
		Set<BeanDefinition> candidates = new LinkedHashSet<>();
		try {
			//补全扫描路径,扫描所有.class文件 classpath*:com/mydemo/**/*.class
			String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
					resolveBasePackage(basePackage) + '/' + this.resourcePattern;
			//定位资源
			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 {
						//通过ASM获取class元数据,并封装在MetadataReader元数据读取器中
						MetadataReader metadataReader = getMetadataReaderFactory().getMetadataReader(resource);
						//判断该类是否符合@CompoentScan的过滤规则
						//过滤匹配排除excludeFilters排除过滤器(可以没有),包含includeFilter中的包含过滤器(至少包含一个)。
						if (isCandidateComponent(metadataReader)) {
							//把元数据转化为 BeanDefinition
							ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader);
							sbd.setResource(resource);
							sbd.setSource(resource);
							//判断是否是合格的bean定义
							if (isCandidateComponent(sbd)) {
								if (debugEnabled) {
									logger.debug("Identified candidate component class: " + resource);
								}
								//加入到集合中
								candidates.add(sbd);
							}
							else {
								//不合格 不是顶级类、具体类
								if (debugEnabled) {
									logger.debug("Ignored because not a concrete top-level class: " + resource);
								}
							}
						}
						else {
							//不符@CompoentScan过滤规则
							if (traceEnabled) {
								logger.trace("Ignored because not matching any filter: " + resource);
							}
						}
					}
					catch (Throwable ex) {
						throw new BeanDefinitionStoreException(
								"Failed to read candidate component class: " + resource, ex);
					}
				}
				else {
					if (traceEnabled) {
						logger.trace("Ignored because not readable: " + resource);
					}
				}
			}
		}
		catch (IOException ex) {
			throw new BeanDefinitionStoreException("I/O failure during classpath scanning", ex);
		}
		return candidates;
	}

Here is the main scanning logic, and the comments in the code have made it very clear.
Main process:

  • According to the package path, scan all .class files

  • According to the package path, generate the Resource object corresponding to .class

  • Obtain class metadata through ASM and encapsulate it in the MetadataReader metadata reader

  • Determine whether the class meets the filtering rules

  • Determine whether the class is an independent class, a concrete class

  • add to the collection

Let's take a closer look at the filtering method isCandidateComponent(metadataReader)

//判断元信息读取器读取的类是否符合容器定义的注解过滤规则
	//@CompoentScan的过滤规则支持5种 (注解、类、正则、aop、自定义)
	protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException {
		//如果读取的类的注解在排除注解过滤规则中,返回false
		for (TypeFilter tf : this.excludeFilters) {
			if (tf.match(metadataReader, getMetadataReaderFactory())) {
				return false;
			}
		}
		//如果读取的类的注解在包含的注解的过滤规则中,则返回ture
		for (TypeFilter tf : this.includeFilters) {
			//判断当前类的注解是否match规则
			if (tf.match(metadataReader, getMetadataReaderFactory())) {
				//是否有@Conditional注解,进行相关处理
				return isConditionMatch(metadataReader);
			}
		}
		//如果读取的类的注解既不在排除规则,也不在包含规则中,则返回false
		return false;
	}

Then trace the tf.match() method
AbstractTypeHierarchyTraversingFilter class

public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory)
			throws IOException {

		// This method optimizes avoiding unnecessary creation of ClassReaders
		// as well as visiting over those readers.
		//检查当前类的注解是否符合规律规则
		if (matchSelf(metadataReader)) {
			return true;
		}
		//check 类名是否符合规则
		ClassMetadata metadata = metadataReader.getClassMetadata();
		if (matchClassName(metadata.getClassName())) {
			return true;
		}

		//如果有继承父类
		if (this.considerInherited) {
			String superClassName = metadata.getSuperClassName();
			if (superClassName != null) {
				// Optimization to avoid creating ClassReader for super class.
				Boolean superClassMatch = matchSuperClass(superClassName);
				if (superClassMatch != null) {
					if (superClassMatch.booleanValue()) {
						return true;
					}
				}
				else {
					// Need to read super class to determine a match...
					try {
						if (match(metadata.getSuperClassName(), metadataReaderFactory)) {
							return true;
						}
					}
					catch (IOException ex) {
						logger.debug("Could not read super class [" + metadata.getSuperClassName() +
								"] of type-filtered class [" + metadata.getClassName() + "]");
					}
 				}
			}
		}

		//如果有实现接口
		if (this.considerInterfaces) {
			for (String ifc : metadata.getInterfaceNames()) {
				// Optimization to avoid creating ClassReader for super class
				Boolean interfaceMatch = matchInterface(ifc);
				if (interfaceMatch != null) {
					if (interfaceMatch.booleanValue()) {
						return true;
					}
				}
				else {
					// Need to read interface to determine a match...
					try {
						if (match(ifc, metadataReaderFactory)) {
							return true;
						}
					}
					catch (IOException ex) {
						logger.debug("Could not read interface [" + ifc + "] for type-filtered class [" +
								metadata.getClassName() + "]");
					}
				}
			}
		}

		return false;
	}

The main thing here is the matchSelf(metadataReader) method
AnnotationTypeFilter class

protected boolean matchSelf(MetadataReader metadataReader) {
		//获取注解元数据
		AnnotationMetadata metadata = metadataReader.getAnnotationMetadata();
		//check 注解及其派生注解中是否包含@Component
		//获取当前类的注解 metadata.hasAnnotation    @Controller
		//获取当前类的注解及其派生注解 metadata.hasAnnotation   @Controller包含的@Component\@Documented等等
		return metadata.hasAnnotation(this.annotationType.getName()) ||
				(this.considerMetaAnnotations && metadata.hasMetaAnnotation(this.annotationType.getName()));
	}

In this code, we can solve our previous doubts " How does Spring discover the classes modified by annotations such as @Configuration, @Controller, @Service? "
It turns out that the annotations @Configuration, @Controller, @Service are actually @Component The derived annotations, we look at the code of these annotations and find that they are decorated with @Component annotations. And spring gets these annotations including @Component through the metadata.hasMetaAnnotation () method, so they can be scanned. as follows:

Then we look back at the scanCandidateComponents(basePackage) method, and then there is an isCandidateComponent(sbd) method, as follows:

//是否是独立的类、具体的类
	protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
		AnnotationMetadata metadata = beanDefinition.getMetadata();
		return (metadata.isIndependent() && (metadata.isConcrete() ||
				(metadata.isAbstract() && metadata.hasAnnotatedMethods(Lookup.class.getName()))));
	}

The function of this method is to determine whether the class is

  • Top-level class (no parent or static inner class)

  • Concrete class (not abstract class or interface)

At this point, the findCandidateComponents(basePackage) method in the doScan(basePackages) method in the ClassPathBeanDefinitionScanner class has ended, that is, our package scanning has ended, and the scanned classes have been stored in the collection, and the result is to parse the registered beans. process.

Conclusion
Through this article, we can answer some of the previous questions:

  • How does Spring discover classes modified by annotations such as @Bean, @Controller, and @Service?
    Use the matchSelf(metadataReader) method to determine whether these annotations contain @Component

  • How does the @ComponentScan annotation work?
    Filter by isCandidateComponent(metadataReader) method

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324143166&siteId=291194637