spring源码解读 -- @Configuration配置类解析流程详解

org.springframework.context.annotation.ConfigurationClassPostProcessor#processConfigBeanDefinitions

本次来探讨下spring是如何解析配置类(即添加了@Configuration的类)

一、环境搭建

POM文件依赖如下:


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>cn.szyrmgroupId>
    <artifactId>spring-sourceartifactId>
    <version>1.0-SNAPSHOTversion>
    <properties>
        <maven.compiler.target>1.8maven.compiler.target>
        <maven.compiler.source>1.8maven.compiler.source>
        <spring-version>5.2.5.RELEASEspring-version>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>${spring-version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>${spring-version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-beansartifactId>
            <version>${spring-version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-expressionartifactId>
            <version>${spring-version}version>
        dependency>
    dependencies>

project>

二、测试

1、POJO类

package cn.szyrm.spring;

/**
 * 这个类在演示中,我们将会通过用配置
 * 在标注有@Bean方法进行配置
 */
public class Car {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

2、配置类

@Configuration
public class AppConfig {
    @Bean
    public Car car(){
        return  new Car();
    }
}

3、启动测试类

public class ApplicationTest {
    public static void main(String[] args) {
        ApplicationContext context =  new   AnnotationConfigApplicationContext  (AppConfig.class);
        Car car = context.getBean(Car.class);
        Assert.notNull(car,"容器中未查询到对应bean");
    }
}

4、运行ApplicationTest 类,发现没有异常。说明已经正确配置

三、源码解读

1、一步一步查找源码入口

这里我们既然是查看怎么解析Configuration配置类

  • 1)我们使用全局搜索Configuration.class

    发现有如下地方使用:

spring源码解读 -- @Configuration配置类解析流程详解_第1张图片

  • 2)、通过上面的搜索,发现是通过org.springframework.context.annotation.AnnotationConfigUtils#registerAnnotationConfigProcessors(org.springframework.beans.factory.support.BeanDefinitionRegistry,java.lang.Object) 注入到spring中的。

  • 3)、搜索上面方法的调用位置
    spring源码解读 -- @Configuration配置类解析流程详解_第2张图片

  • 4)、查看具体调用的位置

spring源码解读 -- @Configuration配置类解析流程详解_第3张图片

  • 5)、追溯到最终调用的位置

spring源码解读 -- @Configuration配置类解析流程详解_第4张图片

通过上面的分析,我们发现往spring 容器中注册了ConfigurationClassPostProcessor 这个工厂后置处理器,那么我们可以借助IDEA工具来查找spring 具体是什么时候实例这个对象?以及实例化该对象后所做的事情?

  • 6)、ConfigurationClassPostProcessor 类声明上添加断点

  • 7)、以debug模式启动main方法

spring源码解读 -- @Configuration配置类解析流程详解_第5张图片

这里补充一个知识点,从ConfigurationClassPostProcessor定义可以看出其实现了BeanDefinitionRegistryPostProcessor,那么在初始化完成后,就会调用接口中的相应方法进行处理。下面我们来通过源码进行追踪下。

spring源码解读 -- @Configuration配置类解析流程详解_第6张图片

8)、postProcessBeanDefinitionRegistry 方法的调用

由于该后置处理器同时实现了PriorityOrdered接口,所有会被最先调用。

spring源码解读 -- @Configuration配置类解析流程详解_第7张图片

org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanDefinitionRegistryPostProcessors 方法就会调用我们要继续调用的方法:org.springframework.context.annotation.ConfigurationClassPostProcessor#postProcessBeanDefinitionRegistry

spring源码解读 -- @Configuration配置类解析流程详解_第8张图片

	/**
	 * Derive further bean definitions from the configuration classes in the registry.
	 */
	@Override
	public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
		int registryId = System.identityHashCode(registry);
		if (this.registriesPostProcessed.contains(registryId)) {
			throw new IllegalStateException(
					"postProcessBeanDefinitionRegistry already called on this post-processor against " + registry);
		}
		if (this.factoriesPostProcessed.contains(registryId)) {
			throw new IllegalStateException(
					"postProcessBeanFactory already called on this post-processor against " + registry);
		}
		this.registriesPostProcessed.add(registryId);

		processConfigBeanDefinitions(registry);
	}

9)、postProcessBeanFactory 方法调用

在这里插入图片描述
spring源码解读 -- @Configuration配置类解析流程详解_第9张图片

2、核心类方法解析

1)、processConfigBeanDefinitions(registry)方法解读

	/**
	 * Build and validate a configuration model based on the registry of
	 * {@link Configuration} classes.
	 */
	public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) {
        
        //1、获取当前beanfactory中的所有的bean的定义信息
		List<BeanDefinitionHolder> configCandidates = new ArrayList<>();
		String[] candidateNames = registry.getBeanDefinitionNames();

		for (String beanName : candidateNames) {
		     //  ...省略部分代码
            //2、如果候选的bean是配置类--> 被  @Configuration  注释的类,那么就将其添加到候选的列表
		 if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.metadataReaderFactory)) {
				configCandidates.add(new BeanDefinitionHolder(beanDef, beanName));
			}
		}

		//3、新建ConfigurationClassParser,用来解析配置类
		// Parse each @Configuration class
		ConfigurationClassParser parser = new ConfigurationClassParser(
				this.metadataReaderFactory, this.problemReporter, this.environment,
				this.resourceLoader, this.componentScanBeanNameGenerator, registry);

		Set<BeanDefinitionHolder> candidates = new LinkedHashSet<>(configCandidates);
		do {
			parser.parse(candidates);
			parser.validate();
			// ...省略部分代码
            //在配置类中配置的bean 注册到容器中
			this.reader.loadBeanDefinitions(configClasses);
			//...省略部分代码
		}
		while (!candidates.isEmpty());
			// ...省略部分代码
	}

processConfigBeanDefinitions(registry)方法主要做了如下事情:

  • 1、获取spring 容器有的被@Configuration注释的类
  • 2、新建了一个ConfigurationClassParser类
  • 3、调用第二步新建的parser类,对候选的配置进行解析操作
  • 4、从解析好的配置中读取bean的定义并注册到容器中

2)、解析所有候选的配置类

ConfigurationClassParser#parse(java.util.Set)

在这个方法中,我们重点注意关注org.springframework.context.annotation.ConfigurationClassParser#doProcessConfigurationClass ,该方法的源码如下:

@Nullable
	protected final SourceClass doProcessConfigurationClass(
			ConfigurationClass configClass, SourceClass sourceClass, Predicate<String> filter)
			throws IOException {

		if (configClass.getMetadata().isAnnotated(Component.class.getName())) {
			// 1、首先递归地解析所有的内部类
			processMemberClasses(configClass, sourceClass, filter);
		}

		// 2、处理所有的@PropertySource 注解
		for (AnnotationAttributes propertySource : AnnotationConfigUtils.attributesForRepeatable(
				sourceClass.getMetadata(), PropertySources.class,
				org.springframework.context.annotation.PropertySource.class)) {
			if (this.environment instanceof ConfigurableEnvironment) {
				processPropertySource(propertySource);
			}
			else {
				logger.info("Ignoring @PropertySource annotation on [" + sourceClass.getMetadata().getClassName() +
						"]. Reason: Environment must implement ConfigurableEnvironment");
			}
		}

		// 3、处理所有的 @ComponentScan annotations
		Set<AnnotationAttributes> componentScans = AnnotationConfigUtils.attributesForRepeatable(
				sourceClass.getMetadata(), ComponentScans.class, ComponentScan.class);
		if (!componentScans.isEmpty() &&
				!this.conditionEvaluator.shouldSkip(sourceClass.getMetadata(), ConfigurationPhase.REGISTER_BEAN)) {
			for (AnnotationAttributes componentScan : componentScans) {
				// The config class is annotated with @ComponentScan -> perform the scan immediately
				Set<BeanDefinitionHolder> scannedBeanDefinitions =
						this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName());
				// Check the set of scanned definitions for any further config classes and parse recursively if needed
				for (BeanDefinitionHolder holder : scannedBeanDefinitions) {
					BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition();
					if (bdCand == null) {
						bdCand = holder.getBeanDefinition();
					}
					if (ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)) {
						parse(bdCand.getBeanClassName(), holder.getBeanName());
					}
				}
			}
		}

		// 4、Process any @Import annotations
		processImports(configClass, sourceClass, getImports(sourceClass), filter, true);

		// 5、Process any @ImportResource annotations
		AnnotationAttributes importResource =
				AnnotationConfigUtils.attributesFor(sourceClass.getMetadata(), ImportResource.class);
		if (importResource != null) {
			String[] resources = importResource.getStringArray("locations");
			Class<? extends BeanDefinitionReader> readerClass = importResource.getClass("reader");
			for (String resource : resources) {
				String resolvedResource = this.environment.resolveRequiredPlaceholders(resource);
				configClass.addImportedResource(resolvedResource, readerClass);
			}
		}

		// 6、Process individual @Bean methods
		Set<MethodMetadata> beanMethods = retrieveBeanMethodMetadata(sourceClass);
		for (MethodMetadata methodMetadata : beanMethods) {
			configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass));
		}

		//7、 Process default methods on interfaces
		processInterfaces(configClass, sourceClass);

		// Process superclass, if any
		if (sourceClass.getMetadata().hasSuperClass()) {
			String superclass = sourceClass.getMetadata().getSuperClassName();
			if (superclass != null && !superclass.startsWith("java") &&
					!this.knownSuperclasses.containsKey(superclass)) {
				this.knownSuperclasses.put(superclass, configClass);
				// Superclass found, return its annotation metadata and recurse
				return sourceClass.getSuperClass();
			}
		}

		// No superclass -> processing is complete
		return null;
	}

可以看出doProcessConfigurationClass 方法是整个解析的核心,主要针对配置类上的各个注解进行了解析

  • 1、解析配置类中的每一个内部类
  • 2、解析并加载配置类中加载的配置文件
  • 3、处理所有的 @ComponentScan、@ComponentScans注解
  • 4、处理所有的@Import注解
  • 5、处理所有的@ImportResource
  • 6、处理所有的添加@Bean的方法
  • 7、处理配置类接口中添加@Bean注解的方法

接下来我们将分析里面各个方法的具体实现:

解析配置类中的每一个内部类
	/**
	 * Register member (nested) classes that happen to be configuration classes themselves.
	 */
	private void processMemberClasses(ConfigurationClass configClass, SourceClass sourceClass,
			Predicate<String> filter) throws IOException {
		//1、获取所有的内部类
		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) {
				if (this.importStack.contains(configClass)) {
					this.problemReporter.error(new CircularImportProblem(configClass, this.importStack));
				}
				else {
					this.importStack.push(configClass);
					try {
                        //2、依次解析每一个配置类
						processConfigurationClass(candidate.asConfigClass(configClass), filter);
					}
					finally {
						this.importStack.pop();
					}
				}
			}
		}
	}

上面的方法是获取到配置类中的所有标记了@Configuration的内部类,然后在对这些候选的配置进行解析操作。

解析并加载配置类中加载的配置文件

/**
	 * Process the given @PropertySource annotation metadata.
	 * @param propertySource metadata for the @PropertySource annotation found
	 * @throws IOException if loading a property source failed
	 */
	private void processPropertySource(AnnotationAttributes propertySource) throws IOException {
		//... 省略部分代码
        //1、获取属性文件的位置
		String[] locations = propertySource.getStringArray("value");
		// ... 省略部分代码  

		for (String location : locations) {
			try {
				String resolvedLocation = this.environment.resolveRequiredPlaceholders(location);
                //2、根据位置信息来创建资源信息
				Resource resource = this.resourceLoader.getResource(resolvedLocation);
                //3、将属性文件配置到添加到系统环境中
				addPropertySource(factory.createPropertySource(name, new EncodedResource(resource, encoding)));
				// ... 省略部分代码  
			
			}
		}
	}

包扫描处理—处理所有的 @ComponentScan、@ComponentScans注解

	//1、从元数据中获取到进行包扫描的值
Set<AnnotationAttributes> componentScans = AnnotationConfigUtils.attributesForRepeatable(
				sourceClass.getMetadata(), ComponentScans.class, ComponentScan.class);
		if (!componentScans.isEmpty() &&
				!this.conditionEvaluator.shouldSkip(sourceClass.getMetadata(), ConfigurationPhase.REGISTER_BEAN)) {
			for (AnnotationAttributes componentScan : componentScans) {
				// The config class is annotated with @ComponentScan -> perform the scan immediately	
                // 2、使用componentScanParser进行包扫描
				Set<BeanDefinitionHolder> scannedBeanDefinitions =
						this.componentScanParser.parse(componentScan, sourceClass.getMetadata().getClassName());
                
				// 3、包扫描到的所有的bean中可能还存在配置类,这时候我们又需要对这些配置类递归地进行解析
				for (BeanDefinitionHolder holder : scannedBeanDefinitions) {
					BeanDefinition bdCand = holder.getBeanDefinition().getOriginatingBeanDefinition();
					if (bdCand == null) {
						bdCand = holder.getBeanDefinition();
					}
					if (ConfigurationClassUtils.checkConfigurationClassCandidate(bdCand, this.metadataReaderFactory)) {
						parse(bdCand.getBeanClassName(), holder.getBeanName());
					}
				}
			}
		}

可以看出该段代码的核心是 使用componentScanParser进行包扫描

public Set<BeanDefinitionHolder> parse(AnnotationAttributes componentScan, final String declaringClass) {
		ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(this.registry,
				componentScan.getBoolean("useDefaultFilters"), this.environment, this.resourceLoader);

		Class<? extends BeanNameGenerator> generatorClass = componentScan.getClass("nameGenerator");
		boolean useInheritedGenerator = (BeanNameGenerator.class == generatorClass);
		scanner.setBeanNameGenerator(useInheritedGenerator ? this.beanNameGenerator :
				BeanUtils.instantiateClass(generatorClass));

		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));
		}

		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);
		}

		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);
		}
		for (Class<?> clazz : componentScan.getClassArray("basePackageClasses")) {
			basePackages.add(ClassUtils.getPackageName(clazz));
		}
		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来进行扫描的。

处理所有的@Import注解

private void processImports(ConfigurationClass configClass, SourceClass currentSourceClass,
			Collection<SourceClass> importCandidates, Predicate<String> exclusionFilter,
			boolean checkForCircularImports) {

		if (importCandidates.isEmpty()) {
			return;
		}

		if (checkForCircularImports && isChainedImportOnStack(configClass)) {
			this.problemReporter.error(new CircularImportProblem(configClass, this.importStack));
		}
		else {
			this.importStack.push(configClass);
			try {
				for (SourceClass candidate : importCandidates) {
					if (candidate.isAssignable(ImportSelector.class)) {
						// Candidate class is an ImportSelector -> delegate to it to determine imports
						Class<?> candidateClass = candidate.loadClass();
						ImportSelector selector = ParserStrategyUtils.instantiateClass(candidateClass, ImportSelector.class,
								this.environment, this.resourceLoader, this.registry);
						Predicate<String> selectorFilter = selector.getExclusionFilter();
						if (selectorFilter != null) {
							exclusionFilter = exclusionFilter.or(selectorFilter);
						}
						if (selector instanceof DeferredImportSelector) {
							this.deferredImportSelectorHandler.handle(configClass, (DeferredImportSelector) selector);
						}
						else {
							String[] importClassNames = selector.selectImports(currentSourceClass.getMetadata());
							Collection<SourceClass> importSourceClasses = asSourceClasses(importClassNames, exclusionFilter);
							processImports(configClass, currentSourceClass, importSourceClasses, exclusionFilter, false);
						}
					}
					else if (candidate.isAssignable(ImportBeanDefinitionRegistrar.class)) {
						// Candidate class is an ImportBeanDefinitionRegistrar ->
						// delegate to it to register additional bean definitions
						Class<?> candidateClass = candidate.loadClass();
						ImportBeanDefinitionRegistrar registrar =
								ParserStrategyUtils.instantiateClass(candidateClass, ImportBeanDefinitionRegistrar.class,
										this.environment, this.resourceLoader, this.registry);
						configClass.addImportBeanDefinitionRegistrar(registrar, currentSourceClass.getMetadata());
					}
					else {
						// Candidate class not an ImportSelector or ImportBeanDefinitionRegistrar ->
						// process it as an @Configuration class
						this.importStack.registerImport(
								currentSourceClass.getMetadata(), candidate.getMetadata().getClassName());
						processConfigurationClass(candidate.asConfigClass(configClass), exclusionFilter);
					}
				}
			}
			catch (BeanDefinitionStoreException ex) {
				throw ex;
			}
			catch (Throwable ex) {
				throw new BeanDefinitionStoreException(
						"Failed to process import candidates for configuration class [" +
						configClass.getMetadata().getClassName() + "]", ex);
			}
			finally {
				this.importStack.pop();
			}
		}
	}

该方法主要的逻辑是:

  • 1、首先处理实现了ImportSelector接口的类

  • 2、处理实现了ImportBeanDefinitionRegistrar 接口的类

  • 3、将导入的类作为一个配置类来进行处理

处理所有的@ImportSource

	// Process any @ImportResource annotations
		AnnotationAttributes importResource =
				AnnotationConfigUtils.attributesFor(sourceClass.getMetadata(), ImportResource.class);
		if (importResource != null) {
			String[] resources = importResource.getStringArray("locations");
			Class<? extends BeanDefinitionReader> readerClass = importResource.getClass("reader");
			for (String resource : resources) {
				String resolvedResource = this.environment.resolveRequiredPlaceholders(resource);
				configClass.addImportedResource(resolvedResource, readerClass);
			}
		}

主要逻辑:

  • 1、获取配置类上的ImportResource 注解
  • 2、获取配置的文件位置信息
  • 3、将解析的信息添加到configClass

处理所有的@Bean methods

	Set<MethodMetadata> beanMethods = retrieveBeanMethodMetadata(sourceClass);
		for (MethodMetadata methodMetadata : beanMethods) {
			configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass));
		}
public void addBeanMethod(BeanMethod method) {
		this.beanMethods.add(method);
	}

3)、将配置类中定义的bean注册到容器中

ConfigurationClassBeanDefinitionReader#loadBeanDefinitions(configClasses)

	/**
	 * Read a particular {@link ConfigurationClass}, registering bean definitions
	 * for the class itself and all of its {@link Bean} methods.
	 */
	private void loadBeanDefinitionsForConfigurationClass(
			ConfigurationClass configClass, TrackedConditionEvaluator trackedConditionEvaluator) {

		if (trackedConditionEvaluator.shouldSkip(configClass)) {
			String beanName = configClass.getBeanName();
			if (StringUtils.hasLength(beanName) && this.registry.containsBeanDefinition(beanName)) {
				this.registry.removeBeanDefinition(beanName);
			}
			this.importRegistry.removeImportingClass(configClass.getMetadata().getClassName());
			return;
		}

		if (configClass.isImported()) {
			registerBeanDefinitionForImportedConfigurationClass(configClass);
		}
		for (BeanMethod beanMethod : configClass.getBeanMethods()) {
			loadBeanDefinitionsForBeanMethod(beanMethod);
		}

		loadBeanDefinitionsFromImportedResources(configClass.getImportedResources());
		 loadBeanDefinitionsFromRegistrars(configClass.getImportBeanDefinitionRegistrars());
	}

四、总结–解析流程图

spring 在容器解析的时候会自动的注入ConfigurationClassPostProcessor 这个工厂后置处理器,该处理器会解析所有被@Configuration标注的配置类。其对应示例图如下:

spring源码解读 -- @Configuration配置类解析流程详解_第10张图片

你可能感兴趣的:(spring,源码解析)