目录:Springboot源码学习目录上文:06、SpringBoot 启动 准备运行环境(prepareEnvironment)流程(三)前言

一、创建应用上下文

debug进入createApplicationContext方法

protected ConfigurableApplicationContext createApplicationContext() {
        // 一个创建应用上下文的工厂方法
	return this.applicationContextFactory.create(this.webApplicationType);  
    }

工厂方法的默认实现,通过之前获取到的容器类型来决定创建什么样的工厂,此时我们的webApplicationType是SERVLET

ApplicationContextFactory DEFAULT = (webApplicationType) -> {
	try {
		switch (webApplicationType) {
		case SERVLET:
			return new AnnotationConfigServletWebServerApplicationContext();
		case REACTIVE:
			return new AnnotationConfigReactiveWebServerApplicationContext();
		default:
			return new AnnotationConfigApplicationContext();
		}
	}
	catch (Exception ex) {
		throw new IllegalStateException("Unable create a default ApplicationContext instance, "+ "you may need a custom ApplicationContextFactory", ex);
	}
    };

创建AnnotationConfigServletWebServerApplicationContext会初始化AnnotatedBeanDefinitionReaderClassPathBeanDefinitionScanner 看名字我们也大概知道,这两个类都和BeanDefinition的读取有关

public AnnotationConfigServletWebServerApplicationContext() {
        // AnnotatedBeanDefinitionReader 实例化过程见 标题二
	this.reader = new AnnotatedBeanDefinitionReader(this);
        // ClassPathBeanDefinitionScanner 实例化过程见 标题三
	this.scanner = new ClassPathBeanDefinitionScanner(this);
    }

父类中会初始化一个bean工厂DefaultListableBeanFactory

public GenericApplicationContext() {
	this.beanFactory = new DefaultListableBeanFactory();
    }

父类中还会初始化一个资源解析器getResourcePatternResolver

public AbstractApplicationContext() {
		this.resourcePatternResolver = getResourcePatternResolver();
	}

二、创建AnnotatedBeanDefinitionReader

AnnotatedBeanDefinitionReader在实例化的过程中会向上下文的工厂中注册很多重要的beanDefinition

// debug进入AnnotatedBeanDefinitionReader的构造方法
	public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry) {
                // 调用重载构造方法
		this(registry, getOrCreateEnvironment(registry));
	}

        // 重载构造方法
	public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry, Environment environment) {
		Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
		Assert.notNull(environment, "Environment must not be null");
                // 将当前上下文赋值给registry
		this.registry = registry;
		this.conditionEvaluator = new ConditionEvaluator(registry, environment, null);
                // 工具类,向上下文注入beanDefinition
		AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
	}

下面的逻辑都是AnnotationConfigUtils这个工具类的
通过此工具类的 registerAnnotationConfigProcessors方法注入的BeanDefinition的角色都是基础设施bean(ROLE_INFRASTRUCTURE)
注入的 BeanDefinition 有如下
ConfigurationClassPostProcessor:是一个BeanFactoryPostProcessor,用于解析@Component@PropertySources@ComponentScans@Import@ImportResource@BeanAutowiredAnnotationBeanPostProcessor:是一个BeanPostProcessor,用于解析 @Autowired@ValueCommonAnnotationBeanPostProcessor:是一个BeanPostProcessor,用于解析@Resource@WebServiceRef@EJBPersistenceAnnotationBeanPostProcessor:是一个BeanPostProcessor,和jpa相关,没有这个类的依赖不会注入这个类的BeanDefinitionEventListenerMethodProcessor:实现了BeanFactoryPostProcessor,和SmartInitializingSingleton,和DefaultEventListenerFactory配合将有@EventListener注解的方法封装为监听器
DefaultEventListenerFactory:主要功能是将有@EventListener注解的方法封装为监听器

public static void registerAnnotationConfigProcessors(BeanDefinitionRegistry registry) {
                // 调用重载
		registerAnnotationConfigProcessors(registry, null);
	}

        // 继续debug进入重载方法,就是真正的注入beanDefinition
        public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
			BeanDefinitionRegistry registry, @Nullable Object source) {

		DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry);
		if (beanFactory != null) {
			if (!(beanFactory.getDependencyComparator() instanceof AnnotationAwareOrderComparator)) {
				beanFactory.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
			}
			if (!(beanFactory.getAutowireCandidateResolver() instanceof ContextAnnotationAutowireCandidateResolver)) {
				beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
			}
		}

		Set<BeanDefinitionHolder> beanDefs = new LinkedHashSet<>(8);

                // 如果上下文中没有ConfigurationClassPostProcessor就注入
		if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME));
		}

                // 如果上下文中没有AutowiredAnnotationBeanPostProcessor就注入
		if (!registry.containsBeanDefinition(AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
		}

                // 如果上下文中没有CommonAnnotationBeanPostProcessor就注入
		// Check for JSR-250 support, and if present add the CommonAnnotationBeanPostProcessor.
		if (jsr250Present && !registry.containsBeanDefinition(COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
		}

                // 如果上下文中没有PersistenceAnnotationBeanPostProcessor,并且没有引入依赖就注入
		// Check for JPA support, and if present add the PersistenceAnnotationBeanPostProcessor.
		if (jpaPresent && !registry.containsBeanDefinition(PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition();
			try {
				def.setBeanClass(ClassUtils.forName(PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME,
						AnnotationConfigUtils.class.getClassLoader()));
			}
			catch (ClassNotFoundException ex) {
				throw new IllegalStateException(
						"Cannot load optional framework class: " + PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME, ex);
			}
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME));
		}

                // 如果上下文中没有EventListenerMethodProcessor就注入
		if (!registry.containsBeanDefinition(EVENT_LISTENER_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(EventListenerMethodProcessor.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_PROCESSOR_BEAN_NAME));
		}

                // 如果上下文中没有DefaultEventListenerFactory就注入
		if (!registry.containsBeanDefinition(EVENT_LISTENER_FACTORY_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(DefaultEventListenerFactory.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_FACTORY_BEAN_NAME));
		}

		return beanDefs;
	}

三、创建ClassPathBeanDefinitionScanner

主要是将@Component,@ManagedBean,@Named,放在过滤器中作为基础注解,使其和其子类,都可以被扫描注入spring上下文
其实在应用上下文创建时初始化的这个扫描器,在后续中并没有用到他的过滤器,只是在后续 @ComponentScan注解解析过程中又会创建一个,过程和本次一样
这里看到这里先给大家展示一下

// 调用构造方法,后续会多次调用重载的构造方法
	public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry) {
		this(registry, true);
	}

	public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters) {
		this(registry, useDefaultFilters, getOrCreateEnvironment(registry));
	}

	public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
			Environment environment) {

		this(registry, useDefaultFilters, environment,
				(registry instanceof ResourceLoader ? (ResourceLoader) registry : null));
	}
        
        // 调用构造方法
	public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
			Environment environment, @Nullable ResourceLoader resourceLoader) {

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

		if (useDefaultFilters) {
                        // 我们要看的重要的逻辑
			registerDefaultFilters();
		}
		setEnvironment(environment);
		setResourceLoader(resourceLoader);
	}

这个方法中会将 @Component@ManagedBean@Named 都加入包含的过滤器中,在后面 解析 @ComponentScan 时告知要讲那类注入到容器中

protected void registerDefaultFilters() {
		this.includeFilters.add(new AnnotationTypeFilter(Component.class));
		ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
		try {
			this.includeFilters.add(new AnnotationTypeFilter(
					((Class<? extends Annotation>) ClassUtils.forName("javax.annotation.ManagedBean", cl)), false));
			logger.trace("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.trace("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
		}
		catch (ClassNotFoundException ex) {
			// JSR-330 API not available - simply skip.
		}
	}