SpringBoot自动装配原理源码详解

文章目录

  • 自动装配原理及源码解析
    • 自动装配实现流程图简述
    • 自动装配步骤详解
      • 核心代码
      • 加载SpringBoot可以自动装配集合
      • 利用LinkedHashSet对得到的需要自动装配的集合去重
      • 过滤@SpringBootApplication注解中exclude参数集合
      • 利用AutoConfigurationImportFilter过滤器对不满足条件的配置类,进行过滤
        • OnClassCondition
        • OnWebApplicationCondition
        • OnBeanCondition
        • 源码解析
      • 按照优先级,对配置类进行实例化(bean初始化)

自动装配原理及源码解析

自动装配实现流程图简述

SpringBoot自动装配原理源码详解_第1张图片

自动装配步骤详解

核心代码

	@Override
		public void process(AnnotationMetadata annotationMetadata, DeferredImportSelector deferredImportSelector) {
			Assert.state(deferredImportSelector instanceof AutoConfigurationImportSelector,
					() -> String.format("Only %s implementations are supported, got %s",
							AutoConfigurationImportSelector.class.getSimpleName(),
							deferredImportSelector.getClass().getName()));
			//实现自动装配的核心代码
			AutoConfigurationEntry autoConfigurationEntry = ((AutoConfigurationImportSelector) deferredImportSelector)
					.getAutoConfigurationEntry(getAutoConfigurationMetadata(), annotationMetadata);
			this.autoConfigurationEntries.add(autoConfigurationEntry);
			for (String importClassName : autoConfigurationEntry.getConfigurations()) {
				this.entries.putIfAbsent(importClassName, annotationMetadata);
			}
		}
	protected AutoConfigurationEntry getAutoConfigurationEntry(AutoConfigurationMetadata autoConfigurationMetadata,
															   AnnotationMetadata annotationMetadata) {
		if (!isEnabled(annotationMetadata)) {
			return EMPTY_ENTRY;
		}
		//通过加载项目中所有jar包中META-INF/spring.factories获取可自动装配的类全路径集合
		AnnotationAttributes attributes = getAttributes(annotationMetadata);
		List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);

		//利用LinkedHashSet对得到的需要自动装配的集合去重
		configurations = removeDuplicates(configurations);
	   /*
	    获取@SpringBootApplication(exclude参数),去除不需要自动装配的类
		例如@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
		*/
		Set<String> exclusions = getExclusions(annotationMetadata, attributes);
		checkExcludedClasses(configurations, exclusions);
		configurations.removeAll(exclusions);

      /* 继续过滤不用自动装配的类(根据配置类上@ConditionalOnClass(KafkaTemplate.class)等条件过滤需要自动装配的类)
       * @ConditionalOnClass 当项目中存在改类,满足条件才是实例化该Bean,换句话说,就是需要kafka相关依赖
      * */
		configurations = filter(configurations, autoConfigurationMetadata);
		//触发自动装配相关的监听事件
		fireAutoConfigurationImportEvents(configurations, exclusions);

		return new AutoConfigurationEntry(configurations, exclusions);
	}

加载SpringBoot可以自动装配集合

	protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
		//通过加载项目中所有jar包中META-INF/spring.factories获取可自动装配的类全路径集合
		List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
				getBeanClassLoader());
		Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "
				+ "are using a custom packaging, make sure that file is correct.");
		return configurations;
	}

SpringFactoriesLoader.loadFactoryNames方法

 public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) {
        String factoryTypeName = factoryType.getName();
        return (List)loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList());
    }

加载所有包含META-INF/spring.factories的jar包中需要自动装配的类,并存放到map集合当中

private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
        MultiValueMap<String, String> result = (MultiValueMap)cache.get(classLoader);
        if (result != null) {
            return result;
        } else {
            try {
                Enumeration<URL> urls = classLoader != null ? classLoader.getResources("META-INF/spring.factories") : ClassLoader.getSystemResources("META-INF/spring.factories");
                LinkedMultiValueMap result = new LinkedMultiValueMap();

                while(urls.hasMoreElements()) {
                    URL url = (URL)urls.nextElement();
                    UrlResource resource = new UrlResource(url);
                    Properties properties = PropertiesLoaderUtils.loadProperties(resource);
                    Iterator var6 = properties.entrySet().iterator();

                    while(var6.hasNext()) {
                        Entry<?, ?> entry = (Entry)var6.next();
                        String factoryTypeName = ((String)entry.getKey()).trim();
                        String[] var9 = StringUtils.commaDelimitedListToStringArray((String)entry.getValue());
                        int var10 = var9.length;

                        for(int var11 = 0; var11 < var10; ++var11) {
                            String factoryImplementationName = var9[var11];
                            result.add(factoryTypeName, factoryImplementationName.trim());
                        }
                    }
                }

                cache.put(classLoader, result);
                return result;
            } catch (IOException var13) {
                throw new IllegalArgumentException("Unable to load factories from location [META-INF/spring.factories]", var13);
            }
        }
    }

利用LinkedHashSet对得到的需要自动装配的集合去重

protected final <T> List<T> removeDuplicates(List<T> list) {
		return new ArrayList<>(new LinkedHashSet<>(list));
	}

过滤@SpringBootApplication注解中exclude参数集合

核心方法:

	protected Set<String> getExclusions(AnnotationMetadata metadata, AnnotationAttributes attributes) {
		Set<String> excluded = new LinkedHashSet<>();
		excluded.addAll(asList(attributes, "exclude"));
		excluded.addAll(Arrays.asList(attributes.getStringArray("excludeName")));
		excluded.addAll(getExcludeAutoConfigurationsProperty());
		return excluded;
	}

利用AutoConfigurationImportFilter过滤器对不满足条件的配置类,进行过滤

先来看下会用的的过滤器,入下图所示
SpringBoot自动装配原理源码详解_第2张图片
下面我们每个类简单说明一些

OnClassCondition

源码分析
SpringBoot自动装配原理源码详解_第3张图片
重点分析
See Also: ConditionalOnClass, ConditionalOnMissingClass
@ConditionalOnClass :某个class位于类路径上,才会实例化一个Bean
@ConditionalOnMissingClass: 某个class类路径上不存在的时候,才会实例化一个Bean
简单描述:就是根据需要自动装配类上以上两个注解中条件满足与否过滤掉不需要加载的自动装配类

OnWebApplicationCondition

SpringBoot自动装配原理源码详解_第4张图片
同上重点分析
See Also:ConditionalOnWebApplication, ConditionalOnNotWebApplication
@ConditionalOnWebApplication: 当前项目是Web项目的条件下,才会实例化一个Bean
@ConditionalOnNotWebApplication: 当项目不是web项目的条件下,才会实例化一个Bean

OnBeanCondition

SpringBoot自动装配原理源码详解_第5张图片
同上重点分析
See Also:ConditionalOnBean, ConditionalOnMissingBean, ConditionalOnSingleCandidate
@ConditionalOnBean:当容器里有指定Bean的条件下,才会实例化一个Bean
@ConditionalOnMissingBean:当容器里没有指定Bean的情况下,才会实例化一个Bean
@ConditionalOnSingleCandidate:当指定Bean在容器中只有一个,或者虽然有多个但是指定首选Bean,才会实例化一个Bean

源码解析
	private List<String> filter(List<String> configurations, AutoConfigurationMetadata autoConfigurationMetadata) {
		long startTime = System.nanoTime();
		String[] candidates = StringUtils.toStringArray(configurations);
		boolean[] skip = new boolean[candidates.length];
		boolean skipped = false;
		//final List autoConfigurationImportFilters = getAutoConfigurationImportFilters();
		//获取过滤器集合,过滤得到满足条件的集合
		for (AutoConfigurationImportFilter filter : getAutoConfigurationImportFilters()) {
			invokeAwareMethods(filter);
			boolean[] match = filter.match(candidates, autoConfigurationMetadata);
			for (int i = 0; i < match.length; i++) {
				if (!match[i]) {
					skip[i] = true;
					candidates[i] = null;
					skipped = true;
				}
			}
		}
		if (!skipped) {
			return configurations;
		}
		List<String> result = new ArrayList<>(candidates.length);
		for (int i = 0; i < candidates.length; i++) {
			if (!skip[i]) {
				result.add(candidates[i]);
			}
		}
		if (logger.isTraceEnabled()) {
			int numberFiltered = configurations.size() - result.size();
			logger.trace("Filtered " + numberFiltered + " auto configuration class in "
					+ TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime) + " ms");
		}
		return new ArrayList<>(result);
	}

获取过滤器集合的方法

//主要获取到OnClassCondition,OnWebApplicationCondition,OnBeanCondition三个过滤器
	protected List<AutoConfigurationImportFilter> getAutoConfigurationImportFilters() {
		return SpringFactoriesLoader.loadFactories(AutoConfigurationImportFilter.class, this.beanClassLoader);
	}

按照优先级,对配置类进行实例化(bean初始化)

		@Override
		public Iterable<Entry> selectImports() {
			if (this.autoConfigurationEntries.isEmpty()) {
				return Collections.emptyList();
			}
			Set<String> allExclusions = this.autoConfigurationEntries.stream()
					.map(AutoConfigurationEntry::getExclusions).flatMap(Collection::stream).collect(Collectors.toSet());
			Set<String> processedConfigurations = this.autoConfigurationEntries.stream()
					.map(AutoConfigurationEntry::getConfigurations).flatMap(Collection::stream)
					.collect(Collectors.toCollection(LinkedHashSet::new));
			processedConfigurations.removeAll(allExclusions);
            //对配置类进行按照顺序进行Bean实例化
			return sortAutoConfigurations(processedConfigurations, getAutoConfigurationMetadata()).stream()
					.map((importClassName) -> new Entry(this.entries.get(importClassName), importClassName))
					.collect(Collectors.toList());
		}
	//获取实例化集合
		private List<String> sortAutoConfigurations(Set<String> configurations,
													AutoConfigurationMetadata autoConfigurationMetadata) {
			return new AutoConfigurationSorter(getMetadataReaderFactory(), autoConfigurationMetadata)
					.getInPriorityOrder(configurations);
		}

		//实例化Bean
		private MetadataReaderFactory getMetadataReaderFactory() {
			try {
				return this.beanFactory.getBean(SharedMetadataReaderFactoryContextInitializer.BEAN_NAME,
						MetadataReaderFactory.class);
			} catch (NoSuchBeanDefinitionException ex) {
				return new CachingMetadataReaderFactory(this.resourceLoader);
			}
		}

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