spring boot自动装配

1、自动装配流程(候选名单,去重,排除名单,过滤,触发事件),主要是类AutoConfigurationImportSelector

  • (1) 读取所有META-INF/spring.factory文件中@EnableAutoConfiguation注解关联的自动装配类集合,并去重
  • (2) 读取当前配置类所标注的@EnableAutoConfiguation属性exclude和excludeName并与 合并为自动装配class排除集合
  • (3) 检查自动装配class的排除集合是否合法
  • (4) 排除自动装配候选集合中排除名单
  • (5) 再次过滤自动装配Class集合中Class不存在的成员
  • (6) 触发一个自动装配的导入事件

2、AutoConfigurationImportSelector

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {

@SpringBootApplication 是一个组合注解,其中有一个注解@EnableAutoConfiguration用于开启自动注解,其定义如下:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

@EnableAutoConfiguration也是一个组合注解,其中,AutoConfigurationImportSelector就是@EnableAutoConfiguration所"@Import”的DeferredImportSelector实现类,组件自动装配逻辑均在selectImports(AnnotationMetadata) 方法中实现

	@Override
	public String[] selectImports(AnnotationMetadata annotationMetadata) {
		if (!isEnabled(annotationMetadata)) {
			return NO_IMPORTS;
		}
		// 加载自动装配的元信息
		AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
				.loadMetadata(this.beanClassLoader);
		// 获取应该加载的自动装配实体
		AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(autoConfigurationMetadata,
				annotationMetadata);
		return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
	}

用于获取需要加载的自动装配实体的方法getAutoConfigurationEntry的具体实现如下所示:

/**
	 * Return the {@link AutoConfigurationEntry} based on the {@link AnnotationMetadata}
	 * of the importing {@link Configuration @Configuration} class.
	 * @param autoConfigurationMetadata the auto-configuration metadata
	 * @param annotationMetadata the annotation metadata of the configuration class
	 * @return the auto-configurations that should be imported
	 */
	protected AutoConfigurationEntry getAutoConfigurationEntry(AutoConfigurationMetadata autoConfigurationMetadata,
			AnnotationMetadata annotationMetadata) {
		if (!isEnabled(annotationMetadata)) {
			return EMPTY_ENTRY;
		}
		// 获取标注类的元信息
		AnnotationAttributes attributes = getAttributes(annotationMetadata);
		// 获取自动装配的候选类名集合
		List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
		// 移除重复对象
		configurations = removeDuplicates(configurations);
		// 获取自动装配组件的排除名单
		Set<String> exclusions = getExclusions(annotationMetadata, attributes);
		// 检查自动装配的排除名单是否合法
		checkExcludedClasses(configurations, exclusions);
		// 移除自动装配名单中的排除组件
		configurations.removeAll(exclusions);
		// 过滤操作(执行去重和排除操作之后执行过滤操作)
		configurations = filter(configurations, autoConfigurationMetadata);
		// 自动装配的导入事件
		fireAutoConfigurationImportEvents(configurations, exclusions);
		return new AutoConfigurationEntry(configurations, exclusions);
	}

你可能感兴趣的:(springboot)