【Spring源代码阅读之五】解析配置类的内部类、@PropertySource、@ComponentScan,处理@ImportResource注解

导图

在这里插入图片描述

ConfigurationClassParser#processMemberClasses

/**
 * 处理配置类的内部类
 */
private void processMemberClasses(ConfigurationClass configClass, SourceClass sourceClass) throws IOException {
	/**
	 * 获取配置类的内部类的SourceClass实例集合
	 */
	Collection<SourceClass> memberClasses = sourceClass.getMemberClasses();
	/**
	 * 如果存在内部类,则对内部类进行解析
	 */
	if (!memberClasses.isEmpty()) {

		/**
		 * 构建需要处理的集合
		 */
		List<SourceClass> candidates = new ArrayList<>(memberClasses.size());

		/**
		 * 遍历所有的内部类,如果内部类是配置类,即配置了一些配置注解
		 * 并且内部类的列名和宿主类的类名不同时放入处理集合中
		 */
		for (SourceClass memberClass : memberClasses) {
			if (ConfigurationClassUtils.isConfigurationCandidate(memberClass.getMetadata()) &&
					!memberClass.getMetadata().getClassName().equals(configClass.getMetadata().getClassName())) {
				candidates.add(memberClass);
			}
		}
		/**
		 * 对需要处理的内部配置类集合进行排序
		 */
		OrderComparator.sort(candidates);
		/**
		 * 对内部类进行处理
		 */
		for (SourceClass candidate : candidates) {
			/**
			 * 如果import队列里面存在内部类宿主类,则证明宿主类正在等待处理或者正在处理
			 * 这时产生问题报告,此种情况是异常的
			 */
			if (this.importStack.contains(configClass)) {
				this.problemReporter.error(new CircularImportProblem(configClass, this.importStack));
			}
			/**
			 * 否则的话就加入处理队列中并解析内部配置类
			 * 最后在处理队列中弹出处理完成的宿主类
			 * 这里注意宿主类
			 * class A{
			 *     class B {
			 *     		class C {}
			 *     }
			 *  }
			 *  B的宿主类是A,C的宿主类是B
			 * 这里实际上是一个递归调用,因为内部类也可能有内部类(这种情况很少)
			 * pop函数弹出的永远是最后处理内部类的宿主类
			 */
			else {
				this.importStack.push(configClass);
				try {
					processConfigurationClass(candidate.asConfigClass(configClass));
				}
				finally {
					this.importStack.pop();
				}
			}
		}
	}
}

ConfigurationClassParser#processPropertySource

/**
 * 解析@PropertySource注解
 */
private void processPropertySource(AnnotationAttributes propertySource) throws IOException {
	/**
	 * 获得name参数值
	 */
	String name = propertySource.getString("name");
	if (!StringUtils.hasLength(name)) {
		name = null;
	}
	/**
	 * 获得encoding参数值,编码格式
	 */
	String encoding = propertySource.getString("encoding");
	if (!StringUtils.hasLength(encoding)) {
		encoding = null;
	}
	/**
	 * 提取value属性,配置文本,并保证value属性有值,没有值则抛异常
	 */
	String[] locations = propertySource.getStringArray("value");
	Assert.isTrue(locations.length > 0, "At least one @PropertySource(value) location is required");
	/**
	 * 获取ignoreResourceNotFound值,是否忽略未找到的资源,默认是false
	 */
	boolean ignoreResourceNotFound = propertySource.getBoolean("ignoreResourceNotFound");

	/**
	 * 获取属性源解析工厂,若没有指定则使用默认的
	 */
	Class<? extends PropertySourceFactory> factoryClass = propertySource.getClass("factory");
	PropertySourceFactory factory = (factoryClass == PropertySourceFactory.class ?
			DEFAULT_PROPERTY_SOURCE_FACTORY : BeanUtils.instantiateClass(factoryClass));

	/**
	 * 遍历配置文本
	 */
	for (String location : locations) {
		try {
			/**
			 * 对配置文本中的$占位符做解析,替换成代表的值
			 */
			String resolvedLocation = this.environment.resolveRequiredPlaceholders(location);
			/**
			 * 将配置文本指向的配置文件转化为属性源Resource对象
			 */
			Resource resource = this.resourceLoader.getResource(resolvedLocation);
			/**
			 * 将属性源添加到环境中
			 */
			addPropertySource(factory.createPropertySource(name, new EncodedResource(resource, encoding)));
		}
		catch (IllegalArgumentException | FileNotFoundException | UnknownHostException ex) {
			// Placeholders not resolvable or resource not found when trying to open it
			if (ignoreResourceNotFound) {
				if (logger.isInfoEnabled()) {
					logger.info("Properties location [" + location + "] not resolvable: " + ex.getMessage());
				}
			}
			else {
				throw ex;
			}
		}
	}
}

ConfigurationClassParser#addPropertySource

/**
 * 将配置添加到容器中
 */
private void addPropertySource(PropertySource<?> propertySource) {
	/**
	 * 获取配置名
	 */
	String name = propertySource.getName();
	/**
	 * 获取容器环境中的配置
	 */
	MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources();

	/**
	 * 在容器环境中是否有同名的配置
	 */
	if (this.propertySourceNames.contains(name)) {
		// We've already added a version, we need to extend it
		/**
		 * 根据名称获取环境中的配置
		 */
		PropertySource<?> existing = propertySources.get(name);

		/**
		 * 确定环境中的配置不为空
		 */
		if (existing != null) {
			/**
			 * 这里暂时看不懂
			 */
			PropertySource<?> newSource = (propertySource instanceof ResourcePropertySource ?
					((ResourcePropertySource) propertySource).withResourceName() : propertySource);
			/**
			 * 如果环境中同名称的资源实现了CompositePropertySource接口,Spring则会将新加入的配置
			 * 作为首选,则之前的配置作为备选,具体可以查看addFirstPropertySource()方法,实际上里面
			 * 包含了一个List集合,Spring巧妙的将新的配置放置在0坐标上
			 */
			if (existing instanceof CompositePropertySource) {
				((CompositePropertySource) existing).addFirstPropertySource(newSource);
			}
			/**
			 * 如果环境之前存在的配置不是复合配置,则转换成复合配置
			 */
			else {
				/**
				 * 这里也暂时看不懂
				 */
				if (existing instanceof ResourcePropertySource) {
					existing = ((ResourcePropertySource) existing).withResourceName();
				}
				/**
				 * 构建一个复合配置对象,实际上里面维护了一个List列表
				 * 然后放入配置,将新的配置放在第一位首选,之前存在于环境中的配置放在后面,作为备选
				 * 并且将环境中的配置替换为复合配置对象
				 */
				CompositePropertySource composite = new CompositePropertySource(name);
				composite.addPropertySource(newSource);
				composite.addPropertySource(existing);
				propertySources.replace(name, composite);
			}
			return;
		}
	}

	/**
	 * 如果环境中的配置名称是空的,则证明还没有配置放置到环境中,这时这个配置是最低优先级的配置
	 * 因为一旦有同名称新的配置加入的话,这个配置就成为备选配置了
	 */
	if (this.propertySourceNames.isEmpty()) {
		propertySources.addLast(propertySource);
	}
	else {
		/**
		 * 获取最低优先级的属性源名称
		 */
		String firstProcessed = this.propertySourceNames.get(this.propertySourceNames.size() - 1);
		/**
		 * 将新加入的配置放到配置源列表中
		 */
		propertySources.addBefore(firstProcessed, propertySource);
	}
	this.propertySourceNames.add(name);
}

MutablePropertySources#addBefore

/**
 * 添加新加入的属性源,他的搜索优先级高于相对属性源(相对属性源我的理解是默认属性源)
 */
public void addBefore(String relativePropertySourceName, PropertySource<?> propertySource) {
	if (logger.isDebugEnabled()) {
		logger.debug("Adding PropertySource '" + propertySource.getName() +
				"' with search precedence immediately higher than '" + relativePropertySourceName + "'");
	}
	/**
	 * 判断他两个的属性源名称是否一致,如果一致的话则抛异常
	 */
	assertLegalRelativeAddition(relativePropertySourceName, propertySource);
	/**
	 * 从属性源列表中删除这个属性,严谨判断,因为在之前的判断中都是使用的名称比较
	 */
	removeIfPresent(propertySource);
	/**
	 * 获得相对属性源的坐标
	 */
	int index = assertPresentAndGetIndex(relativePropertySourceName);
	/**
	 * 向指定坐标添加配置,相对属性源将向后移位
	 */
	addAtIndex(index, propertySource);
}

private void addAtIndex(int index, PropertySource<?> propertySource) {
	removeIfPresent(propertySource);
	this.propertySourceList.add(index, propertySource);
}

ComponentScanAnnotationParser#parse

/**
 * 对ComponentScan中描述的包中的类进行扫描解析成BeanDefinition
 * 本方法主要是对扫描器设置必要的配置,具体的扫描解析调用的是扫描器中的方法
 */
public Set<BeanDefinitionHolder> parse(AnnotationAttributes componentScan, final String declaringClass) {
	/**
	 * 定义一个扫描器
	 */
	ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(this.registry,
			componentScan.getBoolean("useDefaultFilters"), this.environment, this.resourceLoader);

	/**
	 * 获取@ComponentScan注解中设置的BeanName生成器
	 */
	Class<? extends BeanNameGenerator> generatorClass = componentScan.getClass("nameGenerator");
	boolean useInheritedGenerator = (BeanNameGenerator.class == generatorClass);
	scanner.setBeanNameGenerator(useInheritedGenerator ? this.beanNameGenerator :
			BeanUtils.instantiateClass(generatorClass));

	/**
	 * 设置代理模型,关于web的设置
	 */
	ScopedProxyMode scopedProxyMode = componentScan.getEnum("scopedProxy");
	if (scopedProxyMode != ScopedProxyMode.DEFAULT) {
		scanner.setScopedProxyMode(scopedProxyMode);
	}
	else {
		Class<? extends ScopeMetadataResolver> resolverClass = componentScan.getClass("scopeResolver");
		scanner.setScopeMetadataResolver(BeanUtils.instantiateClass(resolverClass));
	}

	/**
	 * 设置符合扫描条件的类文件,即定义哪些类文件可以被扫描到
	 * 通过阅读@ComponentScan注解源码,内容就是"**\/*.class"就是哪些后缀名的文件可被扫描进来
	 */
	scanner.setResourcePattern(componentScan.getString("resourcePattern"));

	/**
	 * 设置指定包下的类可被扫描进来的过滤器,这个属性将扫描的范围进一步缩小
	 */
	for (AnnotationAttributes filter : componentScan.getAnnotationArray("includeFilters")) {
		for (TypeFilter typeFilter : typeFiltersFor(filter)) {
			scanner.addIncludeFilter(typeFilter);
		}
	}
	/**
	 * 设置指定包下的类不能被扫描进来的过滤器
	 */
	for (AnnotationAttributes filter : componentScan.getAnnotationArray("excludeFilters")) {
		for (TypeFilter typeFilter : typeFiltersFor(filter)) {
			scanner.addExcludeFilter(typeFilter);
		}
	}

	/**
	 * 设置包下扫描到的类是否需要懒加载,默认不需要
	 */
	boolean lazyInit = componentScan.getBoolean("lazyInit");
	if (lazyInit) {
		scanner.getBeanDefinitionDefaults().setLazyInit(true);
	}

	/**
	 * 这里是解析扫描包的路径
	 * 注意:@ComponentScan注解中value属性值为扫描的包,这个属性是一个数组,可以给一个数组扫描多个包
	 * 还有一种方式为给一个字符串,通过分割符分割,以达到扫描多个包的目的,分割符Spring中默认的有逗号、分号和换行符
	 * 还有一种方式是给一个数组,数组中每个字符串又指定了多个包,只不过是以分隔符分割的
	 * 我们可以使用多种方式实现,Spring都能给你解析,放到basePackages集合中待处理
	 */
	Set<String> basePackages = new LinkedHashSet<>();
	String[] basePackagesArray = componentScan.getStringArray("basePackages");
	for (String pkg : basePackagesArray) {
		String[] tokenized = StringUtils.tokenizeToStringArray(this.environment.resolvePlaceholders(pkg),
				ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);
		Collections.addAll(basePackages, tokenized);
	}

	/**
	 * 获取@ComponentScan注解的basePackageClasses属性,这个属性中放置的是类的Class,如果设置了这个属性
	 * Spring会将这个类所在包加到扫描包路径的集合中
	 * 也就是说,我们配置扫描包的路径时,可以给value属性设置路径,也可以在包路径下写一个类(只是一个空类,无逻辑)
	 * 把这个类给basePackageClasses属性,Spring会解析这个类的包路径加到扫描集合中待扫描
	 */
	for (Class<?> clazz : componentScan.getClassArray("basePackageClasses")) {
		basePackages.add(ClassUtils.getPackageName(clazz));
	}

	/**
	 * 通过以上步骤后,需要进行待扫描包的集合中却没有需要扫描的包,此时,Spring会认为@ComponentScan注解中没有关于
	 * 需要扫描包的配置,那么Spring会将加了@ComponentScan注解类的所在包路径加到集合中待扫描
	 */
	if (basePackages.isEmpty()) {
		basePackages.add(ClassUtils.getPackageName(declaringClass));
	}

	/**
	 * 无论扫描包的路径是什么,需要将配置了这个扫描包的类排除,因为这个类不需要再次被扫描进来
	 */
	scanner.addExcludeFilter(new AbstractTypeHierarchyTraversingFilter(false, false) {
		@Override
		protected boolean matchClassName(String className) {
			return declaringClass.equals(className);
		}
	});

	/**
	 * 调用扫描器的方法进行扫描
	 */
	return scanner.doScan(StringUtils.toStringArray(basePackages));
}

ClassPathBeanDefinitionScanner#doScan

/**
 * 正式执行扫描包,注意的是,扫描出来的BeanDefinition已经在此方法中完成了注册到环境中
 */
protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
	Assert.notEmpty(basePackages, "At least one base package must be specified");
	/**
	 * 装载BeanDefinitionHolder集合
	 */
	Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<>();
	/**
	 * 循环包的路径依次扫描包
	 */
	for (String basePackage : basePackages) {

		/**
		 * 扫描包下的类转换成BeanDefinition
		 */
		Set<BeanDefinition> candidates = findCandidateComponents(basePackage);
		/**
		 * 遍历扫描出来的BeanDefinition
		 */
		for (BeanDefinition candidate : candidates) {

			/**
			 * 设置动态代理模式
			 */
			ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
			candidate.setScope(scopeMetadata.getScopeName());
			/**
			 * 根据Name生成器生成类名,一般是首字母小写
			 */
			String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);

			/**
			 * 如果BeanDefinition是AbstractBeanDefinition类型
			 * 则将@ComponentScan中设置的配置给到BeanDefinition中
			 * 这些配置是在ComponentScanAnnotationParser#parse()中初始化类扫描器后设置到扫描器中的全局变量中的
			 * 注意:这里是一定会进的,因为扫描出来的BeanDefinition是ScannedGenericBeanDefinition类型
			 */
			if (candidate instanceof AbstractBeanDefinition) {
				postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);
			}
			/**
			 * 如果BeanDefinition是AnnotatedBeanDefinition类型,则根据类上的注解设置信息
			 * 注意:这里的if和上面的if并不是排它关系,Spring会先设置全局值,如果类上再有注解
			 * 		则注解的配置会覆盖扫描器全局配置,则对于类的BeanDefinition来说,
			 * 		上面保证了扫描到的类的全局配置,这里保证了扫描到的类的个性化配置
			 */
			if (candidate instanceof AnnotatedBeanDefinition) {
				AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);
			}
			/**
			 * 判断BeanDefinition是否有加载冲突
			 *
			 *
			 */
			if (checkCandidate(beanName, candidate)) {
				/**
				 * 构建一个BeanDefinitionHolder封装类封装信息
				 */
				BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);
				/**
				 * 获取此BeanDefinition的需要动态代理模型的Holder放到最终的集合中
				 * 如果此BeanDefinition不需要动态代理模型,则放入的是此BeanDefinition的Holder
				 */
				definitionHolder =
						AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
				beanDefinitions.add(definitionHolder);
				/**
				 * 向DefaultListableBeanFactory工厂中注册BeanDefinition
				 */
				registerBeanDefinition(definitionHolder, this.registry);
			}
		}
	}
	return beanDefinitions;
}
ClassPathScanningCandidateComponentProvider#findCandidateComponents
/**
 * 将包路径下的类扫描成BeanDefinition
 */
public Set<BeanDefinition> findCandidateComponents(String basePackage) {
	if (this.componentsIndex != null && indexSupportsIncludeFilters()) {
		return addCandidateComponentsFromIndex(this.componentsIndex, basePackage);
	}
	else {
		return scanCandidateComponents(basePackage);
	}
}
ClassPathScanningCandidateComponentProvider#scanCandidateComponents
/**
 * 执行包的扫描操作
 */
private Set<BeanDefinition> scanCandidateComponents(String basePackage) {
	/**
	 * 定义集合,用来存放扫描到并可以放到容器中的类
	 */
	Set<BeanDefinition> candidates = new LinkedHashSet<>();
	try {
		/**
		 * 定义扫描的全路径,即相对于系统的全路径(如果是Windows系统的话就包括盘符)
		 */
		String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
				resolveBasePackage(basePackage) + '/' + this.resourcePattern;

		/**
		 * 执行扫描操作,转换成一个个Resource对象,这个数组中是包含路径下所有的class文件的
		 * 注意:这里Spring使用的是ASM技术,此技术相当牛逼
		 */
		Resource[] resources = getResourcePatternResolver().getResources(packageSearchPath);

		/**
		 * 获取日志的打印级别
		 */
		boolean traceEnabled = logger.isTraceEnabled();
		boolean debugEnabled = logger.isDebugEnabled();

		/**
		 * 遍历扫描出来的类
		 */
		for (Resource resource : resources) {

			/**
			 * trace日志级别打印
			 */
			if (traceEnabled) {
				logger.trace("Scanning " + resource);
			}
			/**
			 * 如果文件是可读的,就处理
			 */
			if (resource.isReadable()) {
				try {
					/**
					 * 获得类的元数据读取器
					 */
					MetadataReader metadataReader = getMetadataReaderFactory().getMetadataReader(resource);

					/**
					 * 判断元信息是否符合加载条件
					 */
					if (isCandidateComponent(metadataReader)) {
						/**
						 * 通过构造函数构建一个BeanDefinition
						 */
						ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader);
						sbd.setResource(resource);
						sbd.setSource(resource);

						/**
						 * 判断BeanDefinition是否符合Spring实例化的规则,即是否是可实例化的类
						 * 符合的话就加入到客家仔队列中
						 */
						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 {
						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;
}
isCandidateComponent
/**
 * 判断是否符合添加的条件,就是执行@ComponentScan注解中配置的excludeFilters排除条件和includeFilters加入条件
 */
protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException {
	/**
	 * 如果程序员没有添加的话,此处会有一个excludeFilter,是在执行ComponentScanAnnotationParser#parse()中添加的
	 * 主要是过滤排除@ComponentScan注解类的重复加载
	 */
	for (TypeFilter tf : this.excludeFilters) {
		if (tf.match(metadataReader, getMetadataReaderFactory())) {
			return false;
		}
	}
	/**
	 * 如果程序员没有添加并且@ComponentScan注解中useDefaultFilters属性为默认true的话
	 * 会包含一个Spring默认的加载过滤器,此时此处会有一个includeFilter,
	 * 是在执行ComponentScanAnnotationParser#parse()的83行
	 * 创建ClassPathBeanDefinitionScanner扫描器实例时经过调用链生成的,具体请看这个类的构造方法
	 */
	for (TypeFilter tf : this.includeFilters) {
		if (tf.match(metadataReader, getMetadataReaderFactory())) {
			return isConditionMatch(metadataReader);
		}
	}
	return false;
}

ClassPathBeanDefinitionScanner构造方法

/**
 * 构建了一个路径扫描成BeanDefinition的类
 */
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
		Environment environment, @Nullable ResourceLoader resourceLoader) {

	Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
	this.registry = registry;

	/**
	 * 是否使用默认的过滤规则,@ComponentScan中是默认使用的
	 */
	if (useDefaultFilters) {
		registerDefaultFilters();
	}
	setEnvironment(environment);
	setResourceLoader(resourceLoader);
}

registerDefaultFilters

/**
 * 配置默认的扫描规则
 */
protected void registerDefaultFilters() {
	/**
	 * 添加一个包含过滤器,这里使用的是@Component注解,即扫描出来的类需要有此注解可加载到容器中
	 * 注意,@Controller、@Service、@Repository、@Configuration都含有此注解
	 */
	this.includeFilters.add(new AnnotationTypeFilter(Component.class));
	ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();

	/**
	 * 如果存在JSR-250或者JSR-330相关的包,则加入他们相关的包含过滤器
	 * 注解为@ManagedBean、@Named
	 */
	try {
		this.includeFilters.add(new AnnotationTypeFilter(
				((Class<? extends Annotation>) ClassUtils.forName("javax.annotation.ManagedBean", cl)), false));
		logger.debug("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(new AnnotationTypeFilter(
				((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Named", cl)), false));
		logger.debug("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
	}
	catch (ClassNotFoundException ex) {
		// JSR-330 API not available - simply skip.
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值