spring--1--探究spring的启动流程

spring–探究spring的启动流程

文章目录

1 一个简单的spring程序

//声明一个可以读取xml配置文件的容器
ClassPathXmlApplicationContext classPathXmlApplicationContext=new
    ClassPathXmlApplicationContext();

//设置配置文件
classPathXmlApplicationContext.setConfigLocation("spring-context.xml");

//刷新容器,这是spring中最重要的方法,看懂了这个方法,bean的生命周期也就懂了
classPathXmlApplicationContext.refresh();

User user = (User) classPathXmlApplicationContext.getBean("user");

System.out.println(user.hashCode());

spring-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="user" class="cn.lx.spring.v1.User"></bean>

</beans>

2 setConfigLocation()方法

/**
 * Set the config locations for this application context in init-param style,
 * i.e. with distinct locations separated by commas, semicolons or whitespace.
 * <p>If not set, the implementation may use a default as appropriate.
 *
 * 让spring容器以你设置的配置文件进行初始化
 * 多个配置文件之间以逗号,分号,空格区分
 */
public void setConfigLocation(String location) {
    //CONFIG_LOCATION_DELIMITERS= ",; \t\n"
    setConfigLocations(StringUtils.tokenizeToStringArray(location, CONFIG_LOCATION_DELIMITERS));
}


/**
 * Set the config locations for this application context.
 * <p>If not set, the implementation may use a default as appropriate.
 */
public void setConfigLocations(@Nullable String... locations) {
    if (locations != null) {
        Assert.noNullElements(locations, "Config locations must not be null");
        this.configLocations = new String[locations.length];
        for (int i = 0; i < locations.length; i++) {
            //这里解析配置文件位置
            this.configLocations[i] = resolvePath(locations[i]).trim();
        }
    }
    else {
        this.configLocations = null;
    }
}



/**
 * Resolve the given path, replacing placeholders with corresponding
 * environment property values if necessary. Applied to config locations.
 * @param path the original file path
 * @return the resolved file path
 * @see org.springframework.core.env.Environment#resolveRequiredPlaceholders(String)
 *
 * 2.1初始化环境
 * 2.2解析配置文件路径
 */
protected String resolvePath(String path) {
    return getEnvironment().resolveRequiredPlaceholders(path);
}

2.1 初始化环境

/**
 * Return the {@code Environment} for this application context in configurable
 * form, allowing for further customization.
 * <p>If none specified, a default environment will be initialized via
 * {@link #createEnvironment()}.
 *
 * 获取当前环境,如果不存在就创建一个
 */
@Override
public ConfigurableEnvironment getEnvironment() {
    if (this.environment == null) {
        //首次访问,创建一个环境
        this.environment = createEnvironment();
    }
    return this.environment;
}

/**
 * Create and return a new {@link StandardEnvironment}.
 * <p>Subclasses may override this method in order to supply
 * a custom {@link ConfigurableEnvironment} implementation.
 */
protected ConfigurableEnvironment createEnvironment() {
    //spring容器的标准环境
    return new StandardEnvironment();
}

2.1.1 标准环境StandardEnvironment的用例图

我们首先看一下标准环境的类图

在这里插入图片描述

下面我们再看一下这4个接口方法,看看这几个接口都有什么作用

在这里插入图片描述

总结

  • PropertyResolver这个接口定义了一系列的获取某个key对应的value的方法,核心方法resolveRequiredPlaceholders,从名字就可以看出来,它是解析占位符的;其实StandardEnvironment虽然实现了PropertyResolver接口,但是它并没有具体实现这个接口方法,而是委托给PropertySourcesPropertyResolver(它实现了ConfigurablePropertyResolver接口,真正的业务逻辑都是在这个类中实现的,就如同ApplicationContextDefaultListableBeanFactory的关系一样)实现的。
  • ConfigurablePropertyResolver表示可配的值解析器,里面一堆setter方法,可以修改占位符,分隔符,ConversionService(类型转换器)。
  • Environment接口方法都是有关获取profile的,可以获取默认的profile,当前激活的profile
  • ConfigurableEnvironment表示可配的环境,对Environment接口进行增强,修改环境属性
2.1.2 创建标准环境StandardEnvironment

创建一个标准环境,StandardEnvironment的的默认构造方法会调用customizePropertySources方法

/**
 * 环境中的所有属性值都保存在这个里面
 */
private final MutablePropertySources propertySources = new MutablePropertySources();

/**
 * 和2.1.1中总结的对应起来了
 * 环境虽然实现了ConfigurablePropertyResolver接口,但是它将具体实现委托给PropertySourcesPropertyResolver
 */
private final ConfigurablePropertyResolver propertyResolver =
    new PropertySourcesPropertyResolver(this.propertySources);



/**
 * Create a new {@code Environment} instance, calling back to
 * {@link #customizePropertySources(MutablePropertySources)} during construction to
 * allow subclasses to contribute or manipulate {@link PropertySource} instances as
 * appropriate.
 * @see #customizePropertySources(MutablePropertySources)
 * 构造方法,调用customizePropertySources向环境中添加一些默认属性
 */
public AbstractEnvironment() {
    customizePropertySources(this.propertySources);
}


/**
 * Customize the set of property sources with those appropriate for any standard
 * Java environment:
 * <ul>
 * <li>{@value #SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME}
 * <li>{@value #SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME}
 * </ul>
 * <p>Properties present in {@value #SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME} will
 * take precedence over those in {@value #SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME}.
 * @see AbstractEnvironment#customizePropertySources(MutablePropertySources)
 * @see #getSystemProperties()
 * @see #getSystemEnvironment()
 */
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
    //将系统属性(systemProperties)加入到MutablePropertySources中
    propertySources.addLast(
        new PropertiesPropertySource(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, getSystemProperties()));
    //将系统环境变量(systemEnvironment)加入到MutablePropertySources中
    propertySources.addLast(
        new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment()));
}

2.2 解析配置路径

AbstractEnvironment类中

public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException {
    //解析spring的配置文件路径中的占位符
    return this.propertyResolver.resolveRequiredPlaceholders(text);
}

配置文件路径是由PropertySourcesPropertyResolver解析的

在这里插入图片描述

//占位符属性帮助类
private PropertyPlaceholderHelper strictHelper;

// ${
private String placeholderPrefix = SystemPropertyUtils.PLACEHOLDER_PREFIX;

// }
private String placeholderSuffix = SystemPropertyUtils.PLACEHOLDER_SUFFIX;

// :
private String valueSeparator = SystemPropertyUtils.VALUE_SEPARATOR;

//解析spring的配置文件路径中的占位符
public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException {
    if (this.strictHelper == null) {
        //首次创建一个帮助类
        this.strictHelper = createPlaceholderHelper(false);
    }
    //传入帮助类解析占位符
    return doResolvePlaceholders(text, this.strictHelper);
}


/**
 * 创建一个帮助类,解析占位符的业务有委托给这个帮助类
 */
private PropertyPlaceholderHelper createPlaceholderHelper(boolean ignoreUnresolvablePlaceholders) {
    //设置占位符的前缀后缀和分隔符,创建占位符属性帮助类
    //ignoreUnresolvablePlaceholders true忽略无法解析的占位符 false无法解析就报异常
    return new PropertyPlaceholderHelper(this.placeholderPrefix, this.placeholderSuffix,
                                         this.valueSeparator, ignoreUnresolvablePlaceholders);
}

传入帮助类解析占位符

private String doResolvePlaceholders(String text, PropertyPlaceholderHelper helper) {
    /**
     * 2.2.1理解一下this::getPropertyAsRawString这种编程方式
     * 2.2.2从这里我们就可以看到真正解析占位符的是帮助类的replacePlaceholders方法
     */
    return helper.replacePlaceholders(text, this::getPropertyAsRawString);
}
2.2.1函数式接口编程

this::getPropertyAsRawString写法是java8的函数式接口编程。然后再看这个接口类型PlaceholderResolver

/**
 * Strategy interface used to resolve replacement values for placeholders contained in Strings.
 * 
 * 策略接口:获取placeholderName对应的值
 * @FunctionalInterface 此注解代表这是一个函数式接口
 */
@FunctionalInterface
public interface PlaceholderResolver {

    /**
	 * Resolve the supplied placeholder name to the replacement value.
	 * @param placeholderName the name of the placeholder to resolve
	 * @return the replacement value, or {@code null} if no replacement is to be made
	 * 这个方法解析占位符
	 */
    @Nullable
    String resolvePlaceholder(String placeholderName);
}

在来看一下getPropertyAsRawString方法,它是PropertySourcesPropertyResolver类的一个方法

/**
 * Retrieve the specified property as a raw String,
 * i.e. without resolution of nested placeholders.
 * @param key the property name to resolve
 * @return the property value or {@code null} if none found
 * 
 * 从环境中获取key对应的值并返回,不解析嵌套的占位符
 * 可以很明显的看到方法的返回值、参数和函数式接口中的方法是相同的
 */
protected String getPropertyAsRawString(String key){
    //省略实现,见2.2.2
}

其实this::getPropertyAsRawString就相当于下面代码

//相当于一个匿名内部类
new PlaceholderResolver(){

    /**
     * Resolve the supplied placeholder name to the replacement value.
     *
     * @param placeholderName the name of the placeholder to resolve
     * @return the replacement value, or {@code null} if no replacement is to be made
     */
    public String resolvePlaceholder(String placeholderName) {
        //调用的是PropertySourcesPropertyResolver对象的getPropertyAsRawString()方法
        return getPropertyAsRawString(placeholderName);
    }
}
2.2.2 解析占位符
/**
 * Replaces all placeholders of format {@code ${name}} with the value returned
 * from the supplied {@link PlaceholderResolver}.
 * @param value the value containing the placeholders to be replaced
 * @param placeholderResolver the {@code PlaceholderResolver} to use for replacement
 * @return the supplied value with placeholders replaced inline
 *
 * 替换value中所有${name}格式的字符串,值调用PlaceholderResolver获取,实际上就是调用getPropertyAsRawString获取
 */
public String replacePlaceholders(String value, PlaceholderResolver placeholderResolver) {
    Assert.notNull(value, "'value' must not be null");
    //开始解析
    return parseStringValue(value, placeholderResolver, null);
}

开始解析

protected String parseStringValue(
    String value, PlaceholderResolver placeholderResolver, @Nullable Set<String> visitedPlaceholders) {

    //返回第一个${的下标
    int startIndex = value.indexOf(this.placeholderPrefix);
    //下标为-1说明配置文件名称中没有占位符,不用解析,直接返回
    if (startIndex == -1) {
        return value;
    }

    //创建一个线程不安全的可变字符串对象
    StringBuilder result = new StringBuilder(value);
    //一次循环代表解析了一个完整的占位符
    while (startIndex != -1) {
        //2.2.2.1获取占位符的后缀的下标
        int endIndex = findPlaceholderEndIndex(result, startIndex);
        //不等于-1 说明格式正确
        if (endIndex != -1) {
            //截取出占位符中的key
            String placeholder = result.substring(startIndex + this.placeholderPrefix.length(), endIndex);
            String originalPlaceholder = placeholder;
            //第一次肯定是空的,调用该方法是参数是null
            if (visitedPlaceholders == null) {
                visitedPlaceholders = new HashSet<>(4);
            }
            //set集合里面的元素不能重复,即占位符的key不能重复
            if (!visitedPlaceholders.add(originalPlaceholder)) {
                throw new IllegalArgumentException(
                    "Circular placeholder reference '" + originalPlaceholder + "' in property definitions");
            }
            // Recursive invocation, parsing placeholders contained in the placeholder key.
            //递归调用,解析占位符key中的占位符(占位符中又嵌套占位符)
            placeholder = parseStringValue(placeholder, placeholderResolver, visitedPlaceholders);
            // Now obtain the value for the fully resolved key...
            //2.2.2.2这行代码就是解析占位符key获取对应的值,函数式编程
            String propVal = placeholderResolver.resolvePlaceholder(placeholder);
            if (propVal == null && this.valueSeparator != null) {
                //从环境中获取值失败
                //占位符key中可能包含分隔符:
                //对分隔符进行处理
                int separatorIndex = placeholder.indexOf(this.valueSeparator);
                if (separatorIndex != -1) {
                    String actualPlaceholder = placeholder.substring(0, separatorIndex);
                    String defaultValue = placeholder.substring(separatorIndex + this.valueSeparator.length());
                    //再次从环境中获解析
                    propVal = placeholderResolver.resolvePlaceholder(actualPlaceholder);
                    if (propVal == null) {
                        propVal = defaultValue;
                    }
                }
            }
            if (propVal != null) {
                // Recursive invocation, parsing placeholders contained in the
                // previously resolved placeholder value.
                //递归调用,再次解析值中的占位符
                propVal = parseStringValue(propVal, placeholderResolver, visitedPlaceholders);
                //将可变字符串中的占位符替换为解析好的值
                result.replace(startIndex, endIndex + this.placeholderSuffix.length(), propVal);
                if (logger.isTraceEnabled()) {
                    logger.trace("Resolved placeholder '" + placeholder + "'");
                }
                //获取下一个占位符${的下标(${}-${}类型)
                //-1说明没有下一个,结束循环
                startIndex = result.indexOf(this.placeholderPrefix, startIndex + propVal.length());
            }
            else if (this.ignoreUnresolvablePlaceholders) {
                // Proceed with unprocessed value.
                //true忽略无法解析的占位符 
                startIndex = result.indexOf(this.placeholderPrefix, endIndex + this.placeholderSuffix.length());
            }
            else {
                //false无法解析就报异常
                throw new IllegalArgumentException("Could not resolve placeholder '" +
                                                   placeholder + "'" + " in value \"" + value + "\"");
            }
            //从集合中删除已解析的占位符key
            visitedPlaceholders.remove(originalPlaceholder);
        }
        else {
            //endIndex等于-1 说明格式错误
            //startIndex设为-1终止循环
            startIndex = -1;
        }
    }
    return result.toString();
}
2.2.2.1 获取占位符的后缀的下标
/**
 * @param buf 这个就是你输入的配置文件的名字
 * @param startIndex ${在buf中下标
 */
private int findPlaceholderEndIndex(CharSequence buf, int startIndex) {
    //获取${的下一个字符的下标
    int index = startIndex + this.placeholderPrefix.length();
    int withinNestedPlaceholder = 0;
    while (index < buf.length()) {
        //匹配buf字符串中index下标对应的字符是否为}
        if (StringUtils.substringMatch(buf, index, this.placeholderSuffix)) {
            if (withinNestedPlaceholder > 0) {
               //匹配到了},计数减一
                withinNestedPlaceholder--;
                index = index + this.placeholderSuffix.length();
            }
            else {
                //匹配到了${的另一半}
                return index;
            }
        }
        //匹配buf字符串中index下标对应的字符是否为{
        //说明在${}中嵌套了{},
        else if (StringUtils.substringMatch(buf, index, this.simplePrefix)) {
            //withinNestedPlaceholder计数里面嵌套了一个{}
            withinNestedPlaceholder++;
            index = index + this.simplePrefix.length();
        }
        else {
            index++;
        }
    }
    //返回-1说明占位符格式错误,可能是少了}
    return -1;
}
2.2.2.2解析占位符key获取对应的值
/**
 * 函数式接口编程
 * this::getPropertyAsRawString
 * 调用PropertySourcesPropertyResolver对象的getPropertyAsRawString方法
 */
protected String getPropertyAsRawString(String key) {
    //解析
    return getProperty(key, String.class, false);
}


/**
 * @param key 占位符的key
 * @param targetValueType 解析后的值的类型
 * @param resolveNestedPlaceholders 是否解析内部的占位符
 */
protected <T> T getProperty(String key, Class<T> targetValueType, boolean resolveNestedPlaceholders) {
    /**
     * 这个propertySources和StandardEnvironment中的propertySources是同一个,
     * 在狗仔对象的时候作为构造方法参数传到PropertySourcesPropertyResolver构造方法中的
     * 里面已经包含了系统属性systemProperties和环境变量systemEnvironment
     */
    if (this.propertySources != null) {
        for (PropertySource<?> propertySource : this.propertySources) {
            if (logger.isTraceEnabled()) {
                logger.trace("Searching for key '" + key + "' in PropertySource '" +
                             propertySource.getName() + "'");
            }
            //从PropertySource(系统属性或环境变量)获取对应值
            Object value = propertySource.getProperty(key);
            if (value != null) {
                //判断是否解析内部占位符的值
                if (resolveNestedPlaceholders && value instanceof String) {
                    value = resolveNestedPlaceholders((String) value);
                }
                //日志输出
                logKeyFound(key, propertySource, value);
                //将占位符key对应的值转化为目标类型,即string
                return convertValueIfNecessary(value, targetValueType);
            }
        }
    }
    if (logger.isTraceEnabled()) {
        logger.trace("Could not find key '" + key + "' in any property source");
    }
    return null;
}

配置文件路径解析完成

总结:该方法主要做两件事

  • 初始化环境:获取系统属性和环境变量

  • 解析配置路径(配置文件路径中包含占位符,解析占位符)

3 详解refresh()方法

public void refresh() throws BeansException, IllegalStateException {
    //加锁了,防止多线程情况下创建多个容器
    synchronized (this.startupShutdownMonitor) {
        //(3.1) Prepare this context for refreshing.
        prepareRefresh();

        //(3.2) Tell the subclass to refresh the internal bean factory.
        ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

        //(3.3) Prepare the bean factory for use in this context.
        prepareBeanFactory(beanFactory);

        try {
            //(3.4) Allows post-processing of the bean factory in context subclasses.
            postProcessBeanFactory(beanFactory);

            //(3.5) Invoke factory processors registered as beans in the context.
            invokeBeanFactoryPostProcessors(beanFactory);

            //(3.6) Register bean processors that intercept bean creation.
            registerBeanPostProcessors(beanFactory);

            //(3.7) Initialize message source for this context.
            initMessageSource();

            //(3.8) Initialize event multicaster for this context.
            initApplicationEventMulticaster();

            //(3.9) Initialize other special beans in specific context subclasses.
            onRefresh();

            //(3.10) Check for listener beans and register them.
            registerListeners();

            //(3.11) Instantiate all remaining (non-lazy-init) singletons.
            finishBeanFactoryInitialization(beanFactory);

            //(3.12) Last step: publish corresponding event.
            finishRefresh();
        }

        catch (BeansException ex) {
            if (logger.isWarnEnabled()) {
                logger.warn("Exception encountered during context initialization - " +
                            "cancelling refresh attempt: " + ex);
            }

            // Destroy already created singletons to avoid dangling resources.
            destroyBeans();

            // Reset 'active' flag.
            cancelRefresh(ex);

            // Propagate exception to caller.
            throw ex;
        }

        finally {
            // Reset common introspection caches in Spring's core, since we
            // might not ever need metadata for singleton beans anymore...
            resetCommonCaches();
        }
    }
}

3.1 Prepare this context for refreshing(容器刷新的准备工作)

protected void prepareRefresh() {
    // Switch to active.
    //记录系统当前时间
    this.startupDate = System.currentTimeMillis();

    /**
     * 容器状态
     * Flag that indicates whether this context is currently active.
     * private final AtomicBoolean active = new AtomicBoolean();
     *
	 * Flag that indicates whether this context has been closed already. 
	 * private final AtomicBoolean closed = new AtomicBoolean();
	 */
    this.closed.set(false);
    this.active.set(true);

    if (logger.isDebugEnabled()) {
        if (logger.isTraceEnabled()) {
            logger.trace("Refreshing " + this);
        }
        else {
            logger.debug("Refreshing " + getDisplayName());
        }
    }

    // Initialize any placeholder property sources in the context environment.
    //在上下文环境中初始化任何占位符属性资源
    //这是一个空方法,由子类实现
    initPropertySources();

    // Validate that all properties marked as required are resolvable:
    // see ConfigurablePropertyResolver#setRequiredProperties
    //验证所有被标记为必需的属性是可解析的(默认是没有被标记的属性的,用户可以手动添加setRequiredProperties)
    getEnvironment().validateRequiredProperties();

    // Store pre-refresh ApplicationListeners...
    if (this.earlyApplicationListeners == null) {
        //创建存储预刷新应用监听器的集合
        this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners);
    }
    else {
        // Reset local application listeners to pre-refresh state.
        //重设本地的应用监听器监听预刷新状态
        this.applicationListeners.clear();
        this.applicationListeners.addAll(this.earlyApplicationListeners);
    }

    // Allow for the collection of early ApplicationEvents,
    // to be published once the multicaster is available...
    //创建一个早期应用事件存储集合,一旦多播器可用就发布里面的事件
    this.earlyApplicationEvents = new LinkedHashSet<>();
}

3.2 Tell the subclass to refresh the internal bean factory(告诉子类刷新内部beanFactory)

/**
 * Tell the subclass to refresh the internal bean factory.
 * @return the fresh BeanFactory instance
 * @see #refreshBeanFactory()
 * @see #getBeanFactory()
 * 该方法创建了一个默认的beanFactory
 */
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
    //子类实现
    //ClassPathXmlApplicationContext xml配置文件
    //AnnotationConfigApplicationContext javaconfig配置文件 方法内容是不一样的
    refreshBeanFactory();
    return getBeanFactory();
}

我们看xml配置文件是如何创建beanFactory的

/**
 * This implementation performs an actual refresh of this context's underlying
 * bean factory, shutting down the previous bean factory (if any) and
 * initializing a fresh bean factory for the next phase of the context's lifecycle.
 * 这个方法对上下文底层的beanFactory执行实际的刷新工作,
 * 如果已经存在一个beanFactory,就关闭以前的,
 * 并为上下文生命周期的下一个阶段初始化一个新的beanFactory
 */
@Override
protected final void refreshBeanFactory() throws BeansException {
    //3.2.1判断beanFactory是否已存在
    if (hasBeanFactory()) {
        //销毁容器中所有的bean对象
        destroyBeans();
        //关闭beanFactory
        closeBeanFactory();
    }
    try {
        //3.2.2创建一个新的beanFactory
        DefaultListableBeanFactory beanFactory = createBeanFactory();
        //3.2.3设置beanFactory的id
        beanFactory.setSerializationId(getId());
        //3.2.4是否开启定制beanFactory
        customizeBeanFactory(beanFactory);
        //3.2.5读取配置文件,获取beanDefinition
        loadBeanDefinitions(beanFactory);
        this.beanFactory = beanFactory;
    }
    catch (IOException ex) {
        throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
    }
}

3.2.1 判断beanFactory是否已存在
//销毁容器中所有的bean对象
destroyBeans();
//关闭beanFactory
closeBeanFactory();
3.2.2 创建一个新的beanFactory
/**
 * Create an internal bean factory for this context.
 * Called for each {@link #refresh()} attempt.
 * <p>The default implementation creates a
 * {@link org.springframework.beans.factory.support.DefaultListableBeanFactory}
 * with the {@linkplain #getInternalParentBeanFactory() internal bean factory} of this
 * context's parent as parent bean factory. Can be overridden in subclasses,
 * for example to customize DefaultListableBeanFactory's settings.
 * @return the bean factory for this context
 * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowBeanDefinitionOverriding
 * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowEagerClassLoading
 * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowCircularReferences
 * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowRawInjectionDespiteWrapping
 */
protected DefaultListableBeanFactory createBeanFactory() {
    //创建一个beanFactory,并设置他的父beanFactory
    //getInternalParentBeanFactory()返回null,我们这里没有父子容器,
    //等使用了springmvc的时候,这里就不一样了
    return new DefaultListableBeanFactory(getInternalParentBeanFactory());
}
3.2.3 设置beanFactoryid
//getId()的值org.springframework.context.support.ClassPathXmlApplicationContext@5b464ce8
beanFactory.setSerializationId(getId());
3.2.4 个性化beanFactory
/**
 * Customize the internal bean factory used by this context.
 * Called for each {@link #refresh()} attempt.
 * <p>The default implementation applies this context's
 * {@linkplain #setAllowBeanDefinitionOverriding "allowBeanDefinitionOverriding"}
 * and {@linkplain #setAllowCircularReferences "allowCircularReferences"} settings,
 * if specified. Can be overridden in subclasses to customize any of
 * {@link DefaultListableBeanFactory}'s settings.
 * @param beanFactory the newly created bean factory for this context
 * @see DefaultListableBeanFactory#setAllowBeanDefinitionOverriding
 * @see DefaultListableBeanFactory#setAllowCircularReferences
 * @see DefaultListableBeanFactory#setAllowRawInjectionDespiteWrapping
 * @see DefaultListableBeanFactory#setAllowEagerClassLoading
 * 根据上下文启用定制beanFactory
 * allowBeanDefinitionOverriding	是否允许覆盖同名bean(相同名称,不同的bean定义)
 * allowCircularReferences	是否允许循环依赖
 * 这两个属性在创建DefaultListableBeanFactory的时候默认为true
 * 我们这里并没有在ClassPathXmlApplicationContext设置这两个属性
 * 
 * classPathXmlApplicationContext.setAllowBeanDefinitionOverriding(true);
 * classPathXmlApplicationContext.setAllowCircularReferences(true);
 */
protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
    if (this.allowBeanDefinitionOverriding != null) {
        beanFactory.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
    }
    if (this.allowCircularReferences != null) {
        beanFactory.setAllowCircularReferences(this.allowCircularReferences);
    }
}
3.2.5 读取配置文件,获取BeanDefinition
/**
 * Loads the bean definitions via an XmlBeanDefinitionReader.
 * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
 * @see #initBeanDefinitionReader
 * @see #loadBeanDefinitions
 * 通过XmlBeanDefinitionReader将配置文件中的bean定义信息读取到beanDefinition中
 * XmlBeanDefinitionReader只是BeanDefinitionReader接口一种策略实现,用来读取xml配置文件的
 * bean定义信息
 */
@Override
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
    // Create a new XmlBeanDefinitionReader for the given BeanFactory.
    //3.2.5.1根据当前beanFactory创建一个XmlBeanDefinitionReader
    XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

    // Configure the bean definition reader with this context's
    // resource loading environment.
    //3.2.5.2配置环境和上下文资源
    beanDefinitionReader.setEnvironment(this.getEnvironment());
    beanDefinitionReader.setResourceLoader(this);
    //3.2.5.3设置sax解析器解析xml
    beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

    // Allow a subclass to provide custom initialization of the reader,
    // then proceed with actually loading the bean definitions.
    //3.2.5.4初始化bean定义读取器
    initBeanDefinitionReader(beanDefinitionReader);
    //3.2.5.5使用beanDefinitionReader读取xml中定义的bean信息
    loadBeanDefinitions(beanDefinitionReader);
}
3.2.5.1 创建XmlBeanDefinitionReader
/**
 * Create new XmlBeanDefinitionReader for the given bean factory.
 * @param registry the BeanFactory to load bean definitions into,
 * in the form of a BeanDefinitionRegistry
 */
public XmlBeanDefinitionReader(BeanDefinitionRegistry registry) {
    //具体的在父类构造方法
    super(registry);
}


/**
 * Create a new AbstractBeanDefinitionReader for the given bean factory.
 * <p>If the passed-in bean factory does not only implement the BeanDefinitionRegistry
 * interface but also the ResourceLoader interface, it will be used as default
 * ResourceLoader as well. This will usually be the case for
 * {@link org.springframework.context.ApplicationContext} implementations.
 * <p>If given a plain BeanDefinitionRegistry, the default ResourceLoader will be a
 * {@link org.springframework.core.io.support.PathMatchingResourcePatternResolver}.
 * <p>If the passed-in bean factory also implements {@link EnvironmentCapable} its
 * environment will be used by this reader.  Otherwise, the reader will initialize and
 * use a {@link StandardEnvironment}. All ApplicationContext implementations are
 * EnvironmentCapable, while normal BeanFactory implementations are not.
 * @param registry the BeanFactory to load bean definitions into,
 * in the form of a BeanDefinitionRegistry
 * @see #setResourceLoader
 * @see #setEnvironment
 * 父类构造方法
 */
protected AbstractBeanDefinitionReader(BeanDefinitionRegistry registry) {
    Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
    this.registry = registry;

    // Determine ResourceLoader to use.
    //根据对象接口类型分配对象
    //这里的对象是DefaultListableBeanFactory,未实现ResourceLoader和EnvironmentCapable接口
    if (this.registry instanceof ResourceLoader) {
        this.resourceLoader = (ResourceLoader) this.registry;
    }
    else {
        //资源加载器
        this.resourceLoader = new PathMatchingResourcePatternResolver();
    }

    // Inherit Environment if possible
    if (this.registry instanceof EnvironmentCapable) {
        this.environment = ((EnvironmentCapable) this.registry).getEnvironment();
    }
    else {
        //环境
        this.environment = new StandardEnvironment();
    }
}
3.2.5.2 配置环境和上下文资源
/**
 * Set the ResourceLoader to use for resource locations.
 * If specifying a ResourcePatternResolver, the bean definition reader
 * will be capable of resolving resource patterns to Resource arrays.
 * <p>Default is PathMatchingResourcePatternResolver, also capable of
 * resource pattern resolving through the ResourcePatternResolver interface.
 * <p>Setting this to {@code null} suggests that absolute resource loading
 * is not available for this bean definition reader.
 * @see org.springframework.core.io.support.ResourcePatternResolver
 * @see org.springframework.core.io.support.PathMatchingResourcePatternResolver
 * 默认为PathMatchingResourcePatternResolver
 * 现在修改为ClassPathXmlApplicationContext
 */
public void setResourceLoader(@Nullable ResourceLoader resourceLoader) {
    this.resourceLoader = resourceLoader;
}
3.2.5.3 设置sax解析器解析xml
/**
 * Create a ResourceEntityResolver for the specified ResourceLoader
 * (usually, an ApplicationContext).
 * @param resourceLoader the ResourceLoader (or ApplicationContext)
 * to load XML entity includes with
 * 构造函数  构造一个sax方式解析xml的解析器
 * 该解析器实现jdk底层的org.xml.sax包下EntityResolver
 * jdk内置sax解析xml
 */
public ResourceEntityResolver(ResourceLoader resourceLoader) {
    super(resourceLoader.getClassLoader());
    this.resourceLoader = resourceLoader;
}
3.2.5.4 初始化XmlBeanDefinitionReader
//默认为true
private boolean validating = true;

/**
 * Initialize the bean definition reader used for loading the bean
 * definitions of this context. Default implementation is empty.
 * <p>Can be overridden in subclasses, e.g. for turning off XML validation
 * or using a different XmlBeanDefinitionParser implementation.
 * @param reader the bean definition reader used by this context
 * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader#setDocumentReaderClass
 */
protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
    //设置使用xml验证
    reader.setValidating(this.validating);
}
3.2.5.5 使用XmlBeanDefinitionReader读取xml中定义的配置信息
/**
 * Load the bean definitions with the given XmlBeanDefinitionReader.
 * <p>The lifecycle of the bean factory is handled by the {@link #refreshBeanFactory}
 * method; hence this method is just supposed to load and/or register bean definitions.
 * @param reader the XmlBeanDefinitionReader to use
 * @throws BeansException in case of bean registration errors
 * @throws IOException if the required XML document isn't found
 * @see #refreshBeanFactory
 * @see #getConfigLocations
 * @see #getResources
 * @see #getResourcePatternResolver
 */
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
    //我们使用的是ClassPathXmlApplicationContext,没有javaconfig,它肯定是空的
    Resource[] configResources = getConfigResources();
    if (configResources != null) {
        reader.loadBeanDefinitions(configResources);
    }
    //这里就是获取我们配置文件名(已经解析过占位符)
    String[] configLocations = getConfigLocations();
    if (configLocations != null) {
        //读取xml中定义的bean信息
        reader.loadBeanDefinitions(configLocations);
    }
}

读取xml中定义的bean信息

/**
 * 遍历,读取xml中定义的bean信息
 */
@Override
public int loadBeanDefinitions(String... locations) throws BeanDefinitionStoreException {
    Assert.notNull(locations, "Location array must not be null");
    int count = 0;
    for (String location : locations) {
        count += loadBeanDefinitions(location);
    }
    return count;
}

/**
 * 读取名字为location的xml中定义的bean信息
 */
@Override
public int loadBeanDefinitions(String location) throws BeanDefinitionStoreException {
    return loadBeanDefinitions(location, null);
}

loadBeanDefinitions方法

/**
 * Load bean definitions from the specified resource location.
 * <p>The location can also be a location pattern, provided that the
 * ResourceLoader of this bean definition reader is a ResourcePatternResolver.
 * @param location the resource location, to be loaded with the ResourceLoader
 * (or ResourcePatternResolver) of this bean definition reader
 * @param actualResources a Set to be filled with the actual Resource objects
 * that have been resolved during the loading process. May be {@code null}
 * to indicate that the caller is not interested in those Resource objects.
 * @return the number of bean definitions found
 * @throws BeanDefinitionStoreException in case of loading or parsing errors
 * @see #getResourceLoader()
 * @see #loadBeanDefinitions(org.springframework.core.io.Resource)
 * @see #loadBeanDefinitions(org.springframework.core.io.Resource[])
 */
public int loadBeanDefinitions(String location, @Nullable Set<Resource> actualResources) throws BeanDefinitionStoreException {
    //ResourceLoader实际就是ClassPathXmlApplicationContext
    //ResourceLoader是统一资源定位,spring中资源描述和加载是分开的
    ResourceLoader resourceLoader = getResourceLoader();
    if (resourceLoader == null) {
        throw new BeanDefinitionStoreException(
            "Cannot load bean definitions from location [" + location + "]: no ResourceLoader available");
    }

    //ClassPathXmlApplicationContext实现了ResourcePatternResolver
    if (resourceLoader instanceof ResourcePatternResolver) {
        // Resource pattern matching available.
        try {
            //Resource是spring的统一资源描述,是spring对字节输入流的包装
            //(5-1)根据文件名字获取spring统一资源描述
            Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
            //(5-2)根据统一资源描述获取beanDefinition
            int count = loadBeanDefinitions(resources);
            if (actualResources != null) {
                Collections.addAll(actualResources, resources);
            }
            if (logger.isTraceEnabled()) {
                logger.trace("Loaded " + count + " bean definitions from location pattern [" + location + "]");
            }
            return count;
        }
        catch (IOException ex) {
            throw new BeanDefinitionStoreException(
                "Could not resolve bean definition resource pattern [" + location + "]", ex);
        }
    }
    else {
        // Can only load single resources by absolute URL.
        //只能通过绝对url来加载资源
        Resource resource = resourceLoader.getResource(location);
        int count = loadBeanDefinitions(resource);
        if (actualResources != null) {
            actualResources.add(resource);
        }
        if (logger.isTraceEnabled()) {
            logger.trace("Loaded " + count + " bean definitions from location [" + location + "]");
        }
        return count;
    }
}

(5-1)根据文件路径获取spring资源描述
@Override
public Resource[] getResources(String locationPattern) throws IOException {
    //resourcePatternResolver在创建ClassPathXmlApplicationContext对象时由
    //构造函数设值了
    return this.resourcePatternResolver.getResources(locationPattern);
}


/**
 * Create a new AbstractApplicationContext with no parent.
 * 构造函数
 */
public AbstractApplicationContext() {
    this.resourcePatternResolver = getResourcePatternResolver();
}


/**
 * Return the ResourcePatternResolver to use for resolving location patterns
 * into Resource instances. Default is a
 * {@link org.springframework.core.io.support.PathMatchingResourcePatternResolver},
 * supporting Ant-style location patterns.
 * <p>Can be overridden in subclasses, for extended resolution strategies,
 * for example in a web environment.
 * <p><b>Do not call this when needing to resolve a location pattern.</b>
 * Call the context's {@code getResources} method instead, which
 * will delegate to the ResourcePatternResolver.
 * @return the ResourcePatternResolver for this context
 * @see #getResources
 * @see org.springframework.core.io.support.PathMatchingResourcePatternResolver
 */
protected ResourcePatternResolver getResourcePatternResolver() {
    //this是ClassPathXmlApplicationContext对象
    return new PathMatchingResourcePatternResolver(this);
}

接下来我们去PathMatchingResourcePatternResolver类的getResources方法

@Override
public Resource[] getResources(String locationPattern) throws IOException {
    Assert.notNull(locationPattern, "Location pattern must not be null");
    //路径以classpath*:开头
    if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) {
        // a class path resource (multiple resources for same name possible)
        if (getPathMatcher().isPattern(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()))) {
            //路径包含通配符* ?
            // a class path resource pattern
            return findPathMatchingResources(locationPattern);
        }
        else {
            //不包含
            // all class path resources with the given name
            return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()));
        }
    }
    else {
        // Generally only look for a pattern after a prefix here,
        // and on Tomcat only after the "*/" separator for its "war:" protocol.
        //路径以war:开头,代表配置文件在tomcat上
        int prefixEnd = (locationPattern.startsWith("war:") ? locationPattern.indexOf("*/") + 1 :
                         locationPattern.indexOf(':') + 1);
        if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) {
            // a file pattern
            //路径包含通配符* ?
            return findPathMatchingResources(locationPattern);
        }
        else {
            // 路径不包含通配符
            // a single resource with the given name
            //根据路径加载资源
            return new Resource[] {getResourceLoader().getResource(locationPattern)};
        }
    }
}

根据路径加载资源

@Override
public Resource getResource(String location) {
    Assert.notNull(location, "Location must not be null");

    // 首先,通过 ProtocolResolver 来加载资源
    for (ProtocolResolver protocolResolver : getProtocolResolvers()) {
        Resource resource = protocolResolver.resolve(location, this);
        if (resource != null) {
            return resource;
        }
    }

    
    if (location.startsWith("/")) {
        // 其次,以 / 开头,返回 ClassPathContextResource 类型的资源
        return getResourceByPath(location);
    }
    else if (location.startsWith(CLASSPATH_URL_PREFIX)) {
    	// 再次,以 classpath: 开头,返回 ClassPathResource 类型的资源
        return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
    }
    else {
        // 然后,根据是否为文件 URL ,是则返回 FileUrlResource 类型的资源,
        //否则返回 UrlResource 类型的资源
        try {
            // Try to parse the location as a URL...
            URL url = new URL(location);
            return (ResourceUtils.isFileURL(url) ? new FileUrlResource(url) : new UrlResource(url));
        }
        catch (MalformedURLException ex) {
            // 最后,返回 ClassPathContextResource 类型的资源
            // No URL -> resolve as resource path.
            return getResourceByPath(location);
        }
    }
}

统一资源定位这一块内容非常多,我会另写一篇文章记录

(5-2)根据统一资源描述获取beanDefinition
@Override
public int loadBeanDefinitions(Resource... resources) throws BeanDefinitionStoreException {
    Assert.notNull(resources, "Resource array must not be null");
    int count = 0;
    for (Resource resource : resources) {
        //加载单个resource
        count += loadBeanDefinitions(resource);
    }
    return count;
}

加载单个resource

/**
 * Load bean definitions from the specified XML file.
 * @param resource the resource descriptor for the XML file
 * @return the number of bean definitions found
 * @throws BeanDefinitionStoreException in case of loading or parsing errors
 */
@Override
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {
    return loadBeanDefinitions(new EncodedResource(resource));
}

具体如何加载,我会重新写一篇文章

3.3 Prepare the bean factory for use in this context(准备在当前上下文中使用beanFactory)

/**
 * Configure the factory's standard context characteristics,
 * such as the context's ClassLoader and post-processors.
 * @param beanFactory the BeanFactory to configure
 * 配置工厂的标准上下文特征,例如ClassLoader和post-processors
 */
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
    // Tell the internal bean factory to use the context's class loader etc.
    //3.3.1设置beanFactory的classLoader为当前context的classLoader
    beanFactory.setBeanClassLoader(getClassLoader());
    //3.3.2设置beanFactory的表达式语言处理器SpEL,Spring3增加了表达式语言的支持
    beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader()));
    //3.3.3向beanFactory中添加属性编辑器注册器
    beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment()));

    // Configure the bean factory with context callbacks.
    //3.3.4向beanFactory中添加了一个BeanPostProcessor
    beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
    //3.3.5设置忽略自动装配的aware接口
    beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
    beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
    beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
    beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
    beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
    beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);

    // BeanFactory interface not registered as resolvable type in a plain factory.
    // MessageSource registered (and found for autowiring) as a bean.
    //3.3.6设置几个特殊注入规则
    beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
    beanFactory.registerResolvableDependency(ResourceLoader.class, this);
    beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
    beanFactory.registerResolvableDependency(ApplicationContext.class, this);

    // Register early post-processor for detecting inner beans as ApplicationListeners.
    //3.3.7又向beanFactory中添加了一个BeanPostProcessor
    beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(this));

    // Detect a LoadTimeWeaver and prepare for weaving, if found.
    //3.3.8 aspectJ支持
    if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
        beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
        // Set a temporary ClassLoader for type matching.
        beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
    }

    // Register default environment beans.
    //3.3.9将三个环境相关的对象注册到容器中
    if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
        beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
    }
    if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
        beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
    }
    if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
        beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
    }
}
3.3.1 设置beanFactoryclassLoader为当前上下文的classLoader

查看ClassPathXmlApplicationContextUML图可知,该类是DefaultResourceLoader的子类

/**
 * Create a new DefaultResourceLoader.
 * <p>ClassLoader access will happen using the thread context class loader
 * at the time of this ResourceLoader's initialization.
 * @see java.lang.Thread#getContextClassLoader()
 * 默认构造函数中设置了classLoader
 */
public DefaultResourceLoader() {
    //Launcher$AppClassLoader
    this.classLoader = ClassUtils.getDefaultClassLoader();
}
3.3.2 设置beanFactory的表达式语言处理器,Spring3增加了表达式语言的支持
/**
 * Create a new {@code StandardBeanExpressionResolver} with the given bean class loader,
 * using it as the basis for expression compilation.
 * @param beanClassLoader the factory's bean class loader
 * 创建了一个标准的bean表达式解析器 就是我们常说的SpEL
 */
public StandardBeanExpressionResolver(@Nullable ClassLoader beanClassLoader) {
    this.expressionParser = new SpelExpressionParser(new SpelParserConfiguration(null, beanClassLoader));
}
3.3.3 向beanFactory中添加默认的属性编辑器注册器

ResourceEditorRegistrarSpring默认的属性编辑器注册器,registerCustomEditors方法里面注册了一系列的属性编辑器(用户可以通过BeanFactoryPostProcessor的实现类CustomEditorConfigurer实现添加自定义的属性编辑器注册器或者属性编辑器)

public class ResourceEditorRegistrar implements PropertyEditorRegistrar {

    private final PropertyResolver propertyResolver;

    private final ResourceLoader resourceLoader;


    /**
	 * Create a new ResourceEditorRegistrar for the given {@link ResourceLoader}
	 * and {@link PropertyResolver}.
	 * @param resourceLoader the ResourceLoader (or ResourcePatternResolver)
	 * to create editors for (usually an ApplicationContext)
	 * @param propertyResolver the PropertyResolver (usually an Environment)
	 * @see org.springframework.core.env.Environment
	 * @see org.springframework.core.io.support.ResourcePatternResolver
	 * @see org.springframework.context.ApplicationContext
	 */
    public ResourceEditorRegistrar(ResourceLoader resourceLoader, PropertyResolver propertyResolver) {
        this.resourceLoader = resourceLoader;
        this.propertyResolver = propertyResolver;
    }


    /**
	 * Populate the given {@code registry} with the following resource editors:
	 * ResourceEditor, InputStreamEditor, InputSourceEditor, FileEditor, URLEditor,
	 * URIEditor, ClassEditor, ClassArrayEditor.
	 * <p>If this registrar has been configured with a {@link ResourcePatternResolver},
	 * a ResourceArrayPropertyEditor will be registered as well.
	 * @see org.springframework.core.io.ResourceEditor
	 * @see org.springframework.beans.propertyeditors.InputStreamEditor
	 * @see org.springframework.beans.propertyeditors.InputSourceEditor
	 * @see org.springframework.beans.propertyeditors.FileEditor
	 * @see org.springframework.beans.propertyeditors.URLEditor
	 * @see org.springframework.beans.propertyeditors.URIEditor
	 * @see org.springframework.beans.propertyeditors.ClassEditor
	 * @see org.springframework.beans.propertyeditors.ClassArrayEditor
	 * @see org.springframework.core.io.support.ResourceArrayPropertyEditor
	 * 实例化后该方法会被调用
	 * 可以向PropertyEditorRegistry(实际山就是BeanWrapperImpl)添加一定数量的PropertyEditor
	 */
    @Override
    public void registerCustomEditors(PropertyEditorRegistry registry) {
        ResourceEditor baseEditor = new ResourceEditor(this.resourceLoader, this.propertyResolver);
        doRegisterEditor(registry, Resource.class, baseEditor);
        doRegisterEditor(registry, ContextResource.class, baseEditor);
        doRegisterEditor(registry, InputStream.class, new InputStreamEditor(baseEditor));
        doRegisterEditor(registry, InputSource.class, new InputSourceEditor(baseEditor));
        doRegisterEditor(registry, File.class, new FileEditor(baseEditor));
        doRegisterEditor(registry, Path.class, new PathEditor(baseEditor));
        doRegisterEditor(registry, Reader.class, new ReaderEditor(baseEditor));
        doRegisterEditor(registry, URL.class, new URLEditor(baseEditor));

        ClassLoader classLoader = this.resourceLoader.getClassLoader();
        doRegisterEditor(registry, URI.class, new URIEditor(classLoader));
        doRegisterEditor(registry, Class.class, new ClassEditor(classLoader));
        doRegisterEditor(registry, Class[].class, new ClassArrayEditor(classLoader));

        if (this.resourceLoader instanceof ResourcePatternResolver) {
            doRegisterEditor(registry, Resource[].class,
                             new ResourceArrayPropertyEditor((ResourcePatternResolver) this.resourceLoader, this.propertyResolver));
        }
    }

    /**
	 * Override default editor, if possible (since that's what we really mean to do here);
	 * otherwise register as a custom editor.
	 */
    private void doRegisterEditor(PropertyEditorRegistry registry, Class<?> requiredType, PropertyEditor editor) {
        //实际上是走这个分支
        if (registry instanceof PropertyEditorRegistrySupport) {
            ((PropertyEditorRegistrySupport) registry).overrideDefaultEditor(requiredType, editor);
        }
        else {
            registry.registerCustomEditor(requiredType, editor);
        }
    }

}
3.3.4 向容器中添加ApplicationContextAwareProcessor

我们可以看一下这个ApplicationContextAwareProcessor增强器干了啥

/**
 * bean对象初始化之前调用
 */
@Override
@Nullable
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    //未实现这些aware接口就什么也不做
    //ApplicationContextAware这个接口是不是很熟悉,如果我们某个bean中需要使用容器对象,
    //用来从容器中获取别的bean对象,是不是就要实现这个接口
    if (!(bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||
          bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||
          bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)){
        return bean;
    }

    AccessControlContext acc = null;

    if (System.getSecurityManager() != null) {
        acc = this.applicationContext.getBeanFactory().getAccessControlContext();
    }

    if (acc != null) {
        AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
            invokeAwareInterfaces(bean);
            return null;
        }, acc);
    }
    else {
        invokeAwareInterfaces(bean);
    }

    return bean;
}

/**
 * 调用这些aware接口set方法                               
 */
private void invokeAwareInterfaces(Object bean) {
    if (bean instanceof EnvironmentAware) {
        ((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());
    }
    if (bean instanceof EmbeddedValueResolverAware) {
        ((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver);
    }
    if (bean instanceof ResourceLoaderAware) {
        ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);
    }
    if (bean instanceof ApplicationEventPublisherAware) {
        ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);
    }
    if (bean instanceof MessageSourceAware) {
        ((MessageSourceAware) bean).setMessageSource(this.applicationContext);
    }
    if (bean instanceof ApplicationContextAware) {
        ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
    }
}

由此看来,这个ApplicationContextAwareProcessor就是帮我们调用aware接口方法,向bean中设置一些工厂内部对象。

3.3.5 设置6个忽略自动装配的aware接口

beanFactory创建的时候默认忽略6aware接口,分别为EnvironmentAwareEmbeddedValueResolverAwareResourceLoaderAwareApplicationEventPublisherAwareMessageSourceAwareApplicationContextAware

3.3.6 设置4个特殊注入规则

4个对象分别是BeanFactoryResourceLoaderApplicationEventPublisherApplicationContext

//比如一旦检测到自动装配的对象类型为BeanFactory.class,便会将beanFactory注入进去
//本质是将BeanFactory.class作为key,beanFactory作为value存入resolvableDependencies的map集合中
//装配的时候会去这个map中查找时候是对应的key类型
beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory); 
3.3.7 向容器中添加ApplicationListenerDetector

我们看一下ApplicationListenerDetector这个类,它实现了MergedBeanDefinitionPostProcessor接口,ApplicationListenerDetector主要是用来检测这个bean是否实现了ApplicationListener接口,实际就是判断这个bean是不是一个监听器。
下面是这个BeanPostProcessor的两个比较重要的方法

//缓存实例化的监听器名
private final transient Map<String, Boolean> singletonNames = new ConcurrentHashMap<>(256);


//bean实例化后执行
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
    //监听器
    if (ApplicationListener.class.isAssignableFrom(beanType)) {
        /**
         * 保存到当前对象singletonNames属性中
         * map集合的value值代表这个监听器是不是单例的
         */
        this.singletonNames.put(beanName, beanDefinition.isSingleton());
    }
}


//bean初始化完成后执行
public Object postProcessAfterInitialization(Object bean, String beanName) {
    if (bean instanceof ApplicationListener) {
        // potentially not detected as a listener by getBeanNamesForType retrieval
        Boolean flag = this.singletonNames.get(beanName);
        //单例的监听器
        if (Boolean.TRUE.equals(flag)) {
            // singleton bean (top-level or inner): register on the fly
            //将监听器添加到应用上下文中
            this.applicationContext.addApplicationListener((ApplicationListener<?>) bean);
        }
        //保存非单例的监听器主要是为了输出日志
        else if (Boolean.FALSE.equals(flag)) {
            if (logger.isWarnEnabled() && !this.applicationContext.containsBean(beanName)) {
                // inner bean with other scope - can't reliably process events
                logger.warn("Inner bean '" + beanName + "' implements ApplicationListener interface " +
                            "but is not reachable for event multicasting by its containing ApplicationContext " +
                            "because it does not have singleton scope. Only top-level listener beans are allowed " +
                            "to be of non-singleton scope.");
            }
            //去掉非单例的监听器缓存
            this.singletonNames.remove(beanName);
        }
    }
    return bean;
}

将监听器添加到应用上下文中

/** Statically specified listeners. */
//上下文中用来保存监听器对象的集合
private final Set<ApplicationListener<?>> applicationListeners = new LinkedHashSet<>();


@Override
public void addApplicationListener(ApplicationListener<?> listener) {
    Assert.notNull(listener, "ApplicationListener must not be null");
    //有事件多播器就把监听器注册进去,事件多播器创建在3.8章节
    if (this.applicationEventMulticaster != null) {
        this.applicationEventMulticaster.addApplicationListener(listener);
    }
    //保存在上下文中
    this.applicationListeners.add(listener);
}
3.3.8 aspectJ支持

AOP相关,这里先跳过,以后会单独来说。

3.3.9 将三个环境相关的对象注册到容器中
//String ENVIRONMENT_BEAN_NAME = "environment";,容器中环境对象
if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
    beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
}
//String SYSTEM_PROPERTIES_BEAN_NAME = "systemProperties";,系统属性
if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
    beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
}
//String SYSTEM_ENVIRONMENT_BEAN_NAME = "systemEnvironment";,系统环境
if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
    beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
}
3.3.10 registerSingleton方法(向容器中注册单例对象)
@Override
public void registerSingleton(String beanName, Object singletonObject) throws IllegalStateException {
    //调用父类的注册方法
    super.registerSingleton(beanName, singletonObject);
    //更新手动注册的单例bean名
    updateManualSingletonNames(set -> set.add(beanName), set -> !this.beanDefinitionMap.containsKey(beanName));
    clearByTypeCache();
}

父类的注册方法

@Override
public void registerSingleton(String beanName, Object singletonObject) throws IllegalStateException {
    Assert.notNull(beanName, "Bean name must not be null");
    Assert.notNull(singletonObject, "Singleton object must not be null");
    synchronized (this.singletonObjects) {
        //singletonObjects是一个map,容器中所有单例bean的缓存
        Object oldObject = this.singletonObjects.get(beanName);
        //已经存在一个相同名字的对象,抛异常,不能覆盖
        if (oldObject != null) {
            throw new IllegalStateException("Could not register object [" + singletonObject +
                                            "] under bean name '" + beanName + "': there is already object [" + oldObject + "] bound");
        }
        //单例bean注册到容器中
        addSingleton(beanName, singletonObject);
    }
}

/**
 * Add the given singleton object to the singleton cache of this factory.
 * <p>To be called for eager registration of singletons.
 * @param beanName the name of the bean
 * @param singletonObject the singleton object
 * 注册单例bean到容器中
 */
protected void addSingleton(String beanName, Object singletonObject) {
    synchronized (this.singletonObjects) {
        //添加到单例对象池中(一级缓存)
        this.singletonObjects.put(beanName, singletonObject);
        //删除三级缓存中保存的信息
        this.singletonFactories.remove(beanName);
         //删除二级缓存中保存的信息(保存提前初始化的对象,为了解决循环依赖)
        this.earlySingletonObjects.remove(beanName);
        //缓存已经实例化的单例对象名(空间换时间)
        this.registeredSingletons.add(beanName);
    }
}

更新手动注册的单例bean

/**
 * Update the factory's internal set of manual singleton names.
 * @param action the modification action
 * @param condition a precondition for the modification action
 * (if this condition does not apply, the action can be skipped)
 */
private void updateManualSingletonNames(Consumer<Set<String>> action, Predicate<Set<String>> condition) {
    /**
     * alreadyCreated属性是一个set集合里面保存了已经创建的bean的名字
     * hasBeanCreationStarted()方法判断alreadyCreated集合元素的个数
     * 有元素,说明容器已经开始了bean创建,此时为了保证线程安全,需要对beanDefinitionMap加锁
     */
    if (hasBeanCreationStarted()) {
        // Cannot modify startup-time collection elements anymore (for stable iteration)
        synchronized (this.beanDefinitionMap) {
            /**
             * condition接口判断beanDefinitionMap中是否存在该beanName
             * 存在返回false,否则为true
             * 存在beanName对应的BeanDefinition说明这个对象是由spring创建的
             * 那肯定就不能添加到manualSingletonNames集合中
             */
            if (condition.test(this.manualSingletonNames)) {
                Set<String> updatedSingletons = new LinkedHashSet<>(this.manualSingletonNames);
                action.accept(updatedSingletons);
                this.manualSingletonNames = updatedSingletons;
            }
        }
    }
    else {
        // Still in startup registration phase
        if (condition.test(this.manualSingletonNames)) {
            action.accept(this.manualSingletonNames);
        }
    }
}

manualSingletonNames集合中存放的是用户手动注册的单例对象,也就是说,如果不是spring根据对应的BeanDefinition实例化的对象通过registerSingleton方法注册到容器中的话,就会将对象名保存到manualSingletonNames集合中,因为这个对象是用户自己创建的,而不是spring创建。

3.3.11 addBeanPostProcessor方法(向容器中注册一个BeanPostProcessor
@Override
public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
    Assert.notNull(beanPostProcessor, "BeanPostProcessor must not be null");
    // Remove from old position, if any
    //先删除以前的
    this.beanPostProcessors.remove(beanPostProcessor);
    // Track whether it is instantiation/destruction aware
    //根据BeanPostProcessor的类型修改容器的标志位
    if (beanPostProcessor instanceof InstantiationAwareBeanPostProcessor) {
        //表明容器中有InstantiationAwareBeanPostProcessor对象
        this.hasInstantiationAwareBeanPostProcessors = true;
    }
    if (beanPostProcessor instanceof DestructionAwareBeanPostProcessor) {
        //表明容器中有DestructionAwareBeanPostProcessor对象
        this.hasDestructionAwareBeanPostProcessors = true;
    }
    // Add to end of list
    //所有的BeanPostProcessor对象都放在这个集合中
    this.beanPostProcessors.add(beanPostProcessor);
}

3.4 Allows post-processing of the bean factory in context subclasses(允许在上下文子类中对beanFactory进行增强)

该方法可以让我们在上下文的内部beanFactory标准初始化之后修改它,此时,所有的bean已经被加载,但是还没有实例化。并且它允许我们向beanFactory中添加自己的BeanPostProcessor

/**
 * Modify the application context's internal bean factory after its standard
 * initialization. All bean definitions will have been loaded, but no beans
 * will have been instantiated yet. This allows for registering special
 * BeanPostProcessors etc in certain ApplicationContext implementations.
 * @param beanFactory the bean factory used by the application context
 */
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
}

3.5 Invoke factory processors registered as beans in the context(实例化容器中所有BeanFactoryPostProcessor并且执行)

/**
 * Instantiate and invoke all registered BeanFactoryPostProcessor beans,
 * respecting explicit order if given.
 * <p>Must be called before singleton instantiation.
 */
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
    //实例化BeanFactoryPostProcessor并且执行
    PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());

    // Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime
    // (e.g. through an @Bean method registered by ConfigurationClassPostProcessor)
    //aop相关,现在先不考虑
    if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
        beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
        beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
    }
}

spring将处理PostProcessor增强器(包括BeanFactoryPostProcessorBeanPostProcessors)的过程委托给了PostProcessorRegistrationDelegate,典型的委托模式(非23种设计模式之一)。

PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors方法,看一下是如何处理BeanFactoryPostProcessor的?

public static void invokeBeanFactoryPostProcessors(
    ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {

    // Invoke BeanDefinitionRegistryPostProcessors first, if any.
    //记录已经处理的bean的名字
    Set<String> processedBeans = new HashSet<>();

    //DefaultListableBeanFactory实现了BeanDefinitionRegistry接口
    if (beanFactory instanceof BeanDefinitionRegistry) {
        //强转为BeanDefinitionRegistry
        BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
        /**
         * BeanFactoryPostProcessor分为如下两种类型
         * 后续处理的时候会将容器中所有的BeanFactoryPostProcessor分为两类
         * 保存到对应的集合中
         */
        List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
        List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();

        /*********************BeanDefinitionRegistryPostProcessor**********************/
        //1.执行容器中已经实例化的BeanDefinitionRegistryPostProcessor对象的BeanDefinitionRegistryPostProcessor方法
        for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
            //首先执行BeanDefinitionRegistryPostProcessor
            if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
                BeanDefinitionRegistryPostProcessor registryProcessor =
                    (BeanDefinitionRegistryPostProcessor) postProcessor;
                //执行BeanDefinitionRegistryPostProcessor接口方法
                registryProcessor.postProcessBeanDefinitionRegistry(registry);
                //保存
                registryProcessors.add(registryProcessor);
            }
            else {
                //保存
                regularPostProcessors.add(postProcessor);
            }
        }

        // Do not initialize FactoryBeans here: We need to leave all regular beans
        // uninitialized to let the bean factory post-processors apply to them!
        // Separate between BeanDefinitionRegistryPostProcessors that implement
        // PriorityOrdered, Ordered, and the rest.
        //保存当前正在处理的一种类型的BeanDefinitionRegistryPostProcessor
        List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();

        /***********************************************************************/
        //2.实例化,排序并执行实现了PriorityOrdered接口的BeanDefinitionRegistryPostProcessor
        // First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
        //从BeanDefinition和环境中查找所有的BeanDefinitionRegistryPostProcessor
        String[] postProcessorNames =
            beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
        //实例化实现了PriorityOrdered接口的BeanDefinitionRegistryPostProcessor
        for (String ppName : postProcessorNames) {
            if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
                //调用工厂的getBean方法实例化
                currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                //添加到processedBeans集合中,表明该bean已经处理
                processedBeans.add(ppName);
            }
        }
        //排序
        sortPostProcessors(currentRegistryProcessors, beanFactory);
        registryProcessors.addAll(currentRegistryProcessors);
        //执行上面实例化的BeanDefinitionRegistryPostProcessor
        invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
        currentRegistryProcessors.clear();

        /***********************************************************************/
        //3.实例化,排序并执行实现了Ordered接口的BeanDefinitionRegistryPostProcessor
        // Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
        postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
        for (String ppName : postProcessorNames) {
            //实例化剩下的BeanDefinitionRegistryPostProcessor中的有Ordered注解的
            if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
                currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                processedBeans.add(ppName);
            }
        }
        //排序
        sortPostProcessors(currentRegistryProcessors, beanFactory);
        registryProcessors.addAll(currentRegistryProcessors);
        //执行上面实例化的BeanDefinitionRegistryPostProcessor
        invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
        currentRegistryProcessors.clear();

        /***********************************************************************/
        //4.实例化,排序并执行剩下的BeanDefinitionRegistryPostProcessor
        // Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
        /**
         * 和上面两步差不多,实例化剩下的BeanDefinitionRegistryPostProcessor并执行它
         * 这里为什么要用一个while循环?
         * BeanDefinitionRegistryPostProcessor可以向容器中注册一个BeanDefinition
         * 那么如果这个BeanDefinition是一个BeanDefinitionRegistryPostProcessor呢
         * 不用循环,这个新注册BeanDefinitionRegistryPostProcessor永远不会执行
         */
        boolean reiterate = true;
        while (reiterate) {
            reiterate = false;
            postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
            for (String ppName : postProcessorNames) {
                if (!processedBeans.contains(ppName)) {
                    currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
                    processedBeans.add(ppName);
                    reiterate = true;
                }
            }
            sortPostProcessors(currentRegistryProcessors, beanFactory);
            registryProcessors.addAll(currentRegistryProcessors);
            invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
            currentRegistryProcessors.clear();
        }

        /***********************************************************************/
        // Now, invoke the postProcessBeanFactory callback of all processors handled so far.
        //5.执行上面所有实例化的BeanDefinitionRegistryPostProcessor父接口的postProcessBeanFactory方法
        /**
         * 再回调一下BeanDefinitionRegistryPostProcessor对象的另一个方法
         * BeanDefinitionRegistryPostProcessor是BeanFactoryPostProcessor的子接口
         * 上面调用的BeanDefinitionRegistryPostProcessor接口中特有的方法,
         * 现在就是调用它父接口中特有的方法了
         */
        invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
        invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
    }
    //beanFactory未实现BeanDefinitionRegistry,就调用BeanFactoryPostProcessor接口方法
    else {
        // Invoke factory processors registered with the context instance.
        invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
    }

    /********************BeanFactoryPostProcessor***************************/
    // Do not initialize FactoryBeans here: We need to leave all regular beans
    // uninitialized to let the bean factory post-processors apply to them!
    //6.实例化并分类BeanFactoryPostProcessor
    //和上面流程差不多,现在是实例化并执行BeanFactoryPostProcessor了
    String[] postProcessorNames =
        beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);

    // Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
    // Ordered, and the rest.
    List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
    List<String> orderedPostProcessorNames = new ArrayList<>();
    List<String> nonOrderedPostProcessorNames = new ArrayList<>();
    for (String ppName : postProcessorNames) {
        if (processedBeans.contains(ppName)) {
            // skip - already processed in first phase above
        }
        else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
            priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
        }
        else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
            orderedPostProcessorNames.add(ppName);
        }
        else {
            nonOrderedPostProcessorNames.add(ppName);
        }
    }

    /***********************************************************************/
    //7.执行有PriorityOrdered接口的BeanFactoryPostProcessor
    // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
    sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
    invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);

    /***********************************************************************/
    //8.执行有Ordered接口的BeanFactoryPostProcessor
    // Next, invoke the BeanFactoryPostProcessors that implement Ordered.
    List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
    for (String postProcessorName : orderedPostProcessorNames) {
        orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
    }
    sortPostProcessors(orderedPostProcessors, beanFactory);
    invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);

    /***********************************************************************/
    //9.执行剩下的BeanFactoryPostProcessor
    // Finally, invoke all other BeanFactoryPostProcessors.
    List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
    for (String postProcessorName : nonOrderedPostProcessorNames) {
        nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
    }
    invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);

    /***********************************************************************/
    //10.清理合并的BeanDefinition缓存
    // Clear cached merged bean definitions since the post-processors might have
    // modified the original metadata, e.g. replacing placeholders in values...
    //清理缓存中合并的BeanDefinition(主要是上面执行的BeanFactoryPostProcessor可能会修改了原始BeanDefinition)
    beanFactory.clearMetadataCache();
}

总结

  1. 执行已经实例化的BeanDefinitionRegistryPostProcessor的对象的postProcessBeanDefinitionRegistry方法。
  2. 获取容器中所有类型为BeanDefinitionRegistryPostProcessorbeanName,然后通过isTypeMatch方法过滤得到实现了PriorityOrdered接口的beanName,实例化,排序并执行postProcessBeanDefinitionRegistry方法。
  3. 再次获取容器中所有类型为BeanDefinitionRegistryPostProcessorbeanName,然后通过isTypeMatch方法过滤得到实现了Ordered接口的beanName,实例化,排序并执行postProcessBeanDefinitionRegistry方法。
  4. 再次获取容器中所有类型为BeanDefinitionRegistryPostProcessorbeanName,然后过滤掉23中已执行的BeanDefinitionRegistryPostProcessorbeanName,最后实例化剩下的beanName,排序,执行postProcessBeanDefinitionRegistry方法。
  5. 执行上面所有实例化的BeanDefinitionRegistryPostProcessor父接口的postProcessBeanFactory方法。
  6. 有关BeanDefinitionRegistryPostProcessor就已经处理完成了,接下来处理BeanFactoryPostProcessor。获取容器中所有类型为BeanFactoryPostProcessorbeanName,然后通过isTypeMatch方法对这些beanName进行实例化并分类(实现了PriorityOrdered接口、实现了Ordered接口、剩下的BeanFactoryPostProcessor)。
  7. 排序并执行实现了PriorityOrdered接口的BeanFactoryPostProcessor
  8. 排序并执行实现了Ordered接口的BeanFactoryPostProcessor
  9. 排序并执行剩下的BeanFactoryPostProcessor
  10. 清理合并的BeanDefinition缓存

到这里,BeanFactoryPostProcessor的执行流程就已经说完了,下面来说一下流程中所涉及到几个重点方法

3.5.1 getMergedLocalBeanDefinition方法(合并容器中具有父子关系的BeanDefinition
/**
 * Return a merged RootBeanDefinition, traversing the parent bean definition
 * if the specified bean corresponds to a child bean definition.
 * @param beanName the name of the bean to retrieve the merged definition for
 * @return a (potentially merged) RootBeanDefinition for the given bean
 * @throws NoSuchBeanDefinitionException if there is no bean with the given name
 * @throws BeanDefinitionStoreException in case of an invalid bean definition
 * 
 */
protected RootBeanDefinition getMergedLocalBeanDefinition(String beanName) throws BeansException {
    // Quick check on the concurrent map first, with minimal locking.
    //先从缓存中拿
    RootBeanDefinition mbd = this.mergedBeanDefinitions.get(beanName);
    //stale属性代表这个beanDefinition是否需要重新合并
    if (mbd != null && !mbd.stale) {
        return mbd;
    }
    /**
     * 缓存中获取不到就调用getMergedBeanDefinition方法合并
     * 3.5.1.1 getBeanDefinition方法(从容器中获取指定beanName的BeanDefinition)
     * 3.5.1.2 getMergedBeanDefinition方法(合并beanName对应的BeanDefinition)
     */
    return getMergedBeanDefinition(beanName, getBeanDefinition(beanName));
}
3.5.1.1 getBeanDefinition方法(从容器中获取指定beanNameBeanDefinition
public BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException {
    /**
     * beanDefinitionMap中保存了所有的BeanDefinition
     * 通过beanDefinitionReader读取配置文件得到的BeanDefinition都存在这个集合里面
     */ 
    BeanDefinition bd = this.beanDefinitionMap.get(beanName);
    if (bd == null) {
        if (logger.isTraceEnabled()) {
            logger.trace("No bean named '" + beanName + "' found in " + this);
        }
        throw new NoSuchBeanDefinitionException(beanName);
    }
    return bd;
}
3.5.1.2 getMergedBeanDefinition方法(合并beanName对应的BeanDefinition
/**
 * Return a RootBeanDefinition for the given top-level bean, by merging with
 * the parent if the given bean's definition is a child bean definition.
 * @param beanName the name of the bean definition
 * @param bd the original bean definition (Root/ChildBeanDefinition)
 * @return a (potentially merged) RootBeanDefinition for the given bean
 * @throws BeanDefinitionStoreException in case of an invalid bean definition
 */
protected RootBeanDefinition getMergedBeanDefinition(String beanName, BeanDefinition bd)
    throws BeanDefinitionStoreException {

    //调用这个方法合并
    return getMergedBeanDefinition(beanName, bd, null);
}

调用重载的getMergedBeanDefinition方法合并beanName对应的BeanDefinition

/**
 * Return a RootBeanDefinition for the given bean, by merging with the
 * parent if the given bean's definition is a child bean definition.
 * @param beanName the name of the bean definition
 * @param bd the original bean definition (Root/ChildBeanDefinition)
 * @param containingBd the containing bean definition in case of inner bean,
 * or {@code null} in case of a top-level bean
 * @return a (potentially merged) RootBeanDefinition for the given bean
 * @throws BeanDefinitionStoreException in case of an invalid bean definition
 */
protected RootBeanDefinition getMergedBeanDefinition(
    String beanName, BeanDefinition bd, @Nullable BeanDefinition containingBd)
    throws BeanDefinitionStoreException {

    synchronized (this.mergedBeanDefinitions) {
        RootBeanDefinition mbd = null;
        RootBeanDefinition previous = null;

        // Check with full lock now in order to enforce the same merged instance.
        //先从缓存中拿
        if (containingBd == null) {
            mbd = this.mergedBeanDefinitions.get(beanName);
        }

        /***************************合并BeanDefinition**********************/
        /**
         * 缓存中不存在,需要重新合并BeanDefinition
         * state=true,需要重新合并BeanDefinition
         */
        if (mbd == null || mbd.stale) {
            //保存先前合并的BeanDefinition
            previous = mbd;
            //该bean没有父bean
            if (bd.getParentName() == null) {
                // Use copy of given root bean definition.
                //将BeanDefinition转化为RootBeanDefinition
                if (bd instanceof RootBeanDefinition) {
                    mbd = ((RootBeanDefinition) bd).cloneBeanDefinition();
                }
                else {
                    mbd = new RootBeanDefinition(bd);
                }
            }
            //该bean有父bean, 需要和它的父BeanDefinition合并
            else {
                // Child bean definition: needs to be merged with parent.
                BeanDefinition pbd;
                try {
                    //父bean的名字去&符号(考虑FactoryBean)
                    String parentBeanName = transformedBeanName(bd.getParentName());
                    
                    //父beanName和子beanName不相同,说明在一个容器中
                    if (!beanName.equals(parentBeanName)) {
                        //递归,获取合并之后的父BeanDefinition
                        pbd = getMergedBeanDefinition(parentBeanName);
                    }
                    //父beanName和子beanName相同,说明父BeanDefinition在父容器中
                    else {
                        //获取父容器对象
                        BeanFactory parent = getParentBeanFactory();
                        if (parent instanceof ConfigurableBeanFactory) {
                            //递归,获取合并之后的父BeanDefinition
                            pbd = ((ConfigurableBeanFactory) parent).getMergedBeanDefinition(parentBeanName);
                        }
                        else {
                            throw new NoSuchBeanDefinitionException(parentBeanName,
                                                                    "Parent name '" + parentBeanName + "' is equal to bean name '" + beanName +
                                                                    "': cannot be resolved without a ConfigurableBeanFactory parent");
                        }
                    }
                }
                catch (NoSuchBeanDefinitionException ex) {
                    throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanName,
                                                           "Could not resolve parent bean definition '" + bd.getParentName() + "'", ex);
                }
                // Deep copy with overridden values.
                //将BeanDefinition类型转换为RootBeanDefinition
                mbd = new RootBeanDefinition(pbd);
                mbd.overrideFrom(bd);
            }

            // Set default singleton scope, if not configured before.
            //如果该bean没有设置scope,则将scope设置为singleton
            if (!StringUtils.hasLength(mbd.getScope())) {
                mbd.setScope(SCOPE_SINGLETON);
            }

            // A bean contained in a non-singleton bean cannot be a singleton itself.
            // Let's correct this on the fly here, since this might be the result of
            // parent-child merging for the outer bean, in which case the original inner bean
            // definition will not have inherited the merged outer bean's singleton status.
            /** 
             * 这段话的意思是一个包含了非单例bean的bean不能是单例的,
             * 它的scope需要被修改为非单例的scope
             * 但是,什么叫一个bean中包含了一个非单例bean?
             * 我们知道,在XML配置文件中两个bean标签不能直接嵌套,但是可以借助property标签,
             * 在property标签中嵌套一个bean标签,那么如果嵌套的这个bean是非单例的,
             * 则它外部的那个bean就必须的和内部的bean作用域保持一致
             */
            if (containingBd != null && !containingBd.isSingleton() && mbd.isSingleton()) {
                mbd.setScope(containingBd.getScope());
            }

            // Cache the merged bean definition for the time being
            // (it might still get re-merged later on in order to pick up metadata changes)
            /**
             * 缓存已经合并的beanDefinition
             * isCacheBeanMetadata() 判断是否需要缓存合并的BeanDefinition
             */
            if (containingBd == null && isCacheBeanMetadata()) {
                this.mergedBeanDefinitions.put(beanName, mbd);
            }
        }
    	
        /**
         * previous != null说明缓存中有值,mbd是重新合并的
         * 拷贝previous中的一部分值到mbd中
         */
        if (previous != null) {
            copyRelevantMergedBeanDefinitionCaches(previous, mbd);
        }
        
        //返回成功合并的BeanDefinition
        return mbd;
    }
}

总结一下这个方法的流程

  • 先获取缓存中已经合并的beanName对应的BeanDefinition

    • 能够获取到,判断state属性决定是否需要重新合并,true表示需要重新合并,false直接返回从缓存中得到的BeanDefinition
    • 获取不到,就进入合并的流程了。
  • 合并过程很简单,先递归获取已经合并的父BeanDefinition,然后根据父BeanDefinition构建一个RootBeanDefinition,拷贝当前BeanDefinition中值到新建的RootBeanDefinition中。

3.5.2 isTypeMatch方法

判断该beanName对应的bean是否是type类型的

@Override
public boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanDefinitionException {
    /**
     * ResolvableType你就理解为typeToMatch的包装,里面定义了一些属性和方法,方便获取
     * typeToMatch的类型信息
     * forRawClass()创建了一个匿名ResolvableType对象,并重写了ResolvableType类的几个方法
     */
    return isTypeMatch(name, ResolvableType.forRawClass(typeToMatch));
}


@Override
public boolean isTypeMatch(String name, ResolvableType typeToMatch) throws NoSuchBeanDefinitionException {
    return isTypeMatch(name, typeToMatch, true);
}

最终处理逻辑的是这个重载的isTypeMatch方法

/**
 * Internal extended variant of {@link #isTypeMatch(String, ResolvableType)}
 * to check whether the bean with the given name matches the specified type. Allow
 * additional constraints to be applied to ensure that beans are not created early.
 * @param name the name of the bean to query
 * @param typeToMatch the type to match against (as a
 * {@code ResolvableType})
 * @return {@code true} if the bean type matches, {@code false} if it
 * doesn't match or cannot be determined yet
 * @throws NoSuchBeanDefinitionException if there is no bean with the given name
 * @since 5.2
 * @see #getBean
 * @see #getType
 */
protected boolean isTypeMatch(String name, ResolvableType typeToMatch, boolean allowFactoryBeanInit)
    throws NoSuchBeanDefinitionException {

    String beanName = transformedBeanName(name);
    /**
     * 判断这个name是否以&开头(我们知道,如果我们要获取FactoryBean工厂自己,那么
     * 只需要在beanName前面添加一个&符号就可以了)
     */
    boolean isFactoryDereference = BeanFactoryUtils.isFactoryDereference(name);

    // Check manually registered singletons.
    /**
     * 获取当前beanName对应的单例对象,false表示不允许早期实例化(即不允许在该方法里面实例化)
     * 此处有一个陷阱,容器中singletonObjects保存的是FactoryBean对象,且对应的名字为去掉&
     * 符的名字。
     * FactoryBean对象创建流程见getBean方法
     */
    Object beanInstance = getSingleton(beanName, false);


    /*********************************已经实例化*************************************/
    //该名字对应的bean已经实例化了
    if (beanInstance != null && beanInstance.getClass() != NullBean.class) {
        //该bean是FactoryBean
        if (beanInstance instanceof FactoryBean) {
            //name取FactoryBean对象生产的对象
            if (!isFactoryDereference) {
                /**
                 * 3.5.2.1获取FactoryBean对象生产的对象的类型
                 * 原理很简单,就是调用了FactoryBean接口的getObjectType方法
                 * 这就是为什么我们实现FactoryBean接口需要重写这个方法
                 */
                Class<?> type = getTypeForFactoryBean((FactoryBean<?>) beanInstance);
                return (type != null && typeToMatch.isAssignableFrom(type));
            }
            //name取FactoryBean对象
            else {
                //直接判断beanInstance是不是typeToMatch类型的实例
                return typeToMatch.isInstance(beanInstance);
            }
        }

        //非FactoryBean,且名字不是以&开头(就是一个普通的bean)
        else if (!isFactoryDereference) {
            //匹配上了直接返回true
            if (typeToMatch.isInstance(beanInstance)) {
                // Direct match for exposed instance?
                return true;
            }
            //匹配不上,就检查泛型
            else if (typeToMatch.hasGenerics() && containsBeanDefinition(beanName)) {
                // Generics potentially only match on the target class, not on the proxy...
                RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
                Class<?> targetType = mbd.getTargetType();
                if (targetType != null && targetType != ClassUtils.getUserClass(beanInstance)) {
                    // Check raw class match as well, making sure it's exposed on the proxy.
                    Class<?> classToMatch = typeToMatch.resolve();
                    if (classToMatch != null && !classToMatch.isInstance(beanInstance)) {
                        return false;
                    }
                    if (typeToMatch.isAssignableFrom(targetType)) {
                        return true;
                    }
                }
                ResolvableType resolvableType = mbd.targetType;
                if (resolvableType == null) {
                    resolvableType = mbd.factoryMethodReturnType;
                }
                return (resolvableType != null && typeToMatch.isAssignableFrom(resolvableType));
            }
        }

        //非FactoryBean,且名字是以&开头,很明显的错误情况,不匹配
        return false;
    }

    /*********************************未实例化*************************************/
    else if (containsSingleton(beanName) && !containsBeanDefinition(beanName)) {
        //只有单例对象,却没有BeanDefinition,说明对象不是通过spring创建的,不匹配
        // null instance registered
        return false;
    }

    // No singleton instance found -> check bean definition.
    //spring缓存中没有对应名字的单例对象,接下来去检查BeanDefinition
    
    //检查父容器,判断beanName对应的bean是不是在父容器中
    BeanFactory parentBeanFactory = getParentBeanFactory();
    //存在父容器,并且子容器中不存在该beanName对应的BeanDefinition
    if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
        //父容器调用isTypeMatch方法,判断该beanName对应的bean是否是type类型的
        // No bean definition found in this factory -> delegate to parent.
        return parentBeanFactory.isTypeMatch(originalBeanName(name), typeToMatch);
    }

    // Retrieve corresponding bean definition.
    //获取beanName对应已经合并的BeanDefinition,见3.5.1
    RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
    //获取修饰该BeanDefinition的目标BeanDefinition持有者
    BeanDefinitionHolder dbd = mbd.getDecoratedDefinition();

    // Setup the types that we want to match against
    //获取该类型对应的class对象
    Class<?> classToMatch = typeToMatch.resolve();
    //未指定类型,设置类型为FactoryBean
    if (classToMatch == null) {
        classToMatch = FactoryBean.class;
    }
    //否则在原来类型上添加FactoryBean类型
    Class<?>[] typesToMatch = (FactoryBean.class == classToMatch ?
                               new Class<?>[] {classToMatch} : new Class<?>[] {FactoryBean.class, classToMatch});


    // Attempt to predict the bean type
    //当前bean的预测类型
    Class<?> predictedType = null;

    //处理拥有修饰BeanDefinition的FactoryBean
    // We're looking for a regular reference but we're a factory bean that has
    // a decorated bean definition. The target bean should be the same type
    // as FactoryBean would ultimately return.
    if (!isFactoryDereference && dbd != null && isFactoryBean(beanName, mbd)) {
        // We should only attempt if the user explicitly set lazy-init to true
        // and we know the merged bean definition is for a factory bean.
        if (!mbd.isLazyInit() || allowFactoryBeanInit) {
            //获取修饰的beanName对应的已经合并的BeanDefinition,见3.5.1.2
            RootBeanDefinition tbd = getMergedBeanDefinition(dbd.getBeanName(), dbd.getBeanDefinition(), mbd);
            //预测tbd对应的bean的类型,见3.5.2.4
            Class<?> targetType = predictBeanType(dbd.getBeanName(), tbd, typesToMatch);
            if (targetType != null && !FactoryBean.class.isAssignableFrom(targetType)) {
                predictedType = targetType;
            }
        }
    }

    //预测bean的类型
    // If we couldn't use the target type, try regular prediction.
    if (predictedType == null) {
        //3.5.2.4预测bean的类型
        predictedType = predictBeanType(beanName, mbd, typesToMatch);
        if (predictedType == null) {
            return false;
        }
    }

    // Attempt to get the actual ResolvableType for the bean.
    //代表当前bean的真实类型
    ResolvableType beanType = null;

    //当前bean的类型是FactoryBean,那么就获取FactoryBean生产的对象的类型
    // If it's a FactoryBean, we want to look at what it creates, not the factory class.
    if (FactoryBean.class.isAssignableFrom(predictedType)) {
        if (beanInstance == null && !isFactoryDereference) {
            //3.5.2.1 获取FactoryBean对象生产的对象的类型
            beanType = getTypeForFactoryBean(beanName, mbd, allowFactoryBeanInit);
            predictedType = beanType.resolve();
            if (predictedType == null) {
                return false;
            }
        }
    }
    /**
     * 当前bean的类型不是FactoryBean,但是你却使用了&符来获取FactoryBean对象
     * 这很明显是不对的,所以在此处调用predictBeanType方法重新进行预测bean的类型
     * 特殊情况:SmartInstantiationAwareBeanPostProcessor接口的predictBeanType方法将
     * 一个FactoryBean预测为非FactoryBean
     */
    else if (isFactoryDereference) {
        // Special case: A SmartInstantiationAwareBeanPostProcessor returned a non-FactoryBean
        // type but we nevertheless are being asked to dereference a FactoryBean...
        // Let's check the original bean class and proceed with it if it is a FactoryBean.
        predictedType = predictBeanType(beanName, mbd, FactoryBean.class);
        if (predictedType == null || !FactoryBean.class.isAssignableFrom(predictedType)) {
            return false;
        }
    }

    // We don't have an exact type but if bean definition target type or the factory
    // method return type matches the predicted type then we can use that.
    //beanType为null,说明当前bean不是一个FactoryBean
    if (beanType == null) {
        //当前bean的ResolvableType类型
        ResolvableType definedType = mbd.targetType;
        //未指定targetType,说明该bean可能是工厂方法返回的bean
        if (definedType == null) {
            //得到工厂方法的返回值类型
            definedType = mbd.factoryMethodReturnType;
        }
        //工厂方法生产的类型和预测类型相等,说明当前类型就是bean的真实类型
        if (definedType != null && definedType.resolve() == predictedType) {
            beanType = definedType;
        }
    }

    //先匹配真实类型
    // If we have a bean type use it so that generics are considered
    if (beanType != null) {
        return typeToMatch.isAssignableFrom(beanType);
    }

    //然后匹配预测类型
    // If we don't have a bean type, fallback to the predicted type
    return typeToMatch.isAssignableFrom(predictedType);
}

总结一下这个方法处理流程

  • 首先调用getSingleton方法从容器中获取beanName对应的实例,到了这里就分为两种情况。

  • 第一种:容器已经实例化beanName对应的bean。那么就直接判断这个实例的类型。

    • 它是一个FactoryBean,如果name不是以&开头,那么直接调用getObjectType方法就能的到它生产的对象的类型,然后再调用isAssignableFrom方法进行比较;如果name是以&开头可以直接调用isInstance方法比较FactoryBean本身。
    • 它不是一个FactoryBean,且name不是以&开头,说明该bean就是一个普通bean,直接调用isInstance方法进行判断;如果判断不成功,再检查泛型。
  • 第二种:容器还没有实例化beanName对应的bean。那么就判断这个BeanDefinition的类型。

    • 如果beanName对应的BeanDefinition在父容器中,递归匹配父容器中的BeanDefinition
    • 如果该BeanDefinition有一个包装的BeanDefinition,且是一个FactoryBean,调用predictBeanType方法预测这个包装的BeanDefinition的类型。
    • 如果没有包装的BeanDefinition,或者预测结果为null,那么就直接预测beanName对应的BeanDefinition的类型,预测类型为null,则不匹配。
    • 如果预测类型为FactoryBean,并且name不是以&开头,就调用getTypeForFactoryBean方法获取它生产的对象的类型。如果预测类型不是FactoryBean,并且name&开头,那么就调用predictBeanType方法重新预测当前bean的类型,判断预测类型为null或者FactoryBean,就表明不匹配。
    • 如果上面所有的都没有判断成功,到这一步说明这个BeanDefinition可能是一个工厂方法生产的对象的BeanDefinition,此时获取工厂方法返回值类型,和上面预测类型对比,如果相同,说明工厂方法返回值类型就是当前bean的真实类型.
    • 在方法最后,首先对真实类型进行匹配,其次才是预测类型。
3.5.2.1 getTypeForFactoryBean(获取FactoryBean对象生产的对象的类型)

第一种:FactoryBean对象已经创建了,此时获取它生产的对象的类型很简单,直接调用getObjectType方法即可

/**
 * Determine the type for the given FactoryBean.
 * @param factoryBean the FactoryBean instance to check
 * @return the FactoryBean's object type,
 * or {@code null} if the type cannot be determined yet
 */
@Nullable
protected Class<?> getTypeForFactoryBean(FactoryBean<?> factoryBean) {
    try {
        if (System.getSecurityManager() != null) {
            return AccessController.doPrivileged(
                (PrivilegedAction<Class<?>>) factoryBean::getObjectType, getAccessControlContext());
        }
        else {
            //调用getObjectType方法的到FactoryBean生产的对象的类型
            return factoryBean.getObjectType();
        }
    }
    catch (Throwable ex) {
        // Thrown from the FactoryBean's getObjectType implementation.
        logger.info("FactoryBean threw exception from getObjectType, despite the contract saying " +
                    "that it should return null if the type of its object cannot be determined yet", ex);
        return null;
    }
}

第二种:FactoryBean对象没有创建

/**
 * This implementation attempts to query the FactoryBean's generic parameter metadata
 * if present to determine the object type. If not present, i.e. the FactoryBean is
 * declared as a raw type, checks the FactoryBean's {@code getObjectType} method
 * on a plain instance of the FactoryBean, without bean properties applied yet.
 * If this doesn't return a type yet, and {@code allowInit} is {@code true} a
 * full creation of the FactoryBean is used as fallback (through delegation to the
 * superclass's implementation).
 * <p>The shortcut check for a FactoryBean is only applied in case of a singleton
 * FactoryBean. If the FactoryBean instance itself is not kept as singleton,
 * it will be fully created to check the type of its exposed object.
 */
@Override
protected ResolvableType getTypeForFactoryBean(String beanName, RootBeanDefinition mbd, boolean allowInit) {
    // Check if the bean definition itself has defined the type with an attribute
    ResolvableType result = getTypeForFactoryBeanFromAttributes(mbd);
    if (result != ResolvableType.NONE) {
        return result;
    }

    //内省解析该bean的类型
    ResolvableType beanType =
        (mbd.hasBeanClass() ? ResolvableType.forClass(mbd.getBeanClass()) : ResolvableType.NONE);

    // For instance supplied beans try the target type and bean class
    if (mbd.getInstanceSupplier() != null) {
        result = getFactoryBeanGeneric(mbd.targetType);
        if (result.resolve() != null) {
            return result;
        }
        result = getFactoryBeanGeneric(beanType);
        if (result.resolve() != null) {
            return result;
        }
    }

    // Consider factory methods
    /**
     * @Bean方法
     * 或者XML中factory-bean="xxx",factory-method="xxx"
     */
    String factoryBeanName = mbd.getFactoryBeanName();
    String factoryMethodName = mbd.getFactoryMethodName();

    // Scan the factory bean methods
    if (factoryBeanName != null) {
        if (factoryMethodName != null) {
            // Try to obtain the FactoryBean's object type from its factory method
            // declaration without instantiating the containing bean at all.
            //获取factoryBeanName对应的BeanDefinition
            BeanDefinition factoryBeanDefinition = getBeanDefinition(factoryBeanName);
            Class<?> factoryBeanClass;
            /**
             * 判断该factoryBeanDefinition是否已经加载到JVM中了
             * 能够获取到对应的clazz对象,肯定已经加载了
             * 下面if和else就是为了获取工厂Bean的clazz对象
             */
            if (factoryBeanDefinition instanceof AbstractBeanDefinition &&
                ((AbstractBeanDefinition) factoryBeanDefinition).hasBeanClass()) {
                factoryBeanClass = ((AbstractBeanDefinition) factoryBeanDefinition).getBeanClass();
            }
            //未加载,就先合并BeanDefinition,然后获取类型
            else {
                RootBeanDefinition fbmbd = getMergedBeanDefinition(factoryBeanName, factoryBeanDefinition);
                //获取给定BeanDefinition对应的bean的类型,见3.5.2.4
                factoryBeanClass = determineTargetType(factoryBeanName, fbmbd);
            }
            if (factoryBeanClass != null) {
                //获取工厂方法生产的FactoryBean对象的泛型类型,见3.5.3
                result = getTypeForFactoryBeanFromMethod(factoryBeanClass, factoryMethodName);
                if (result.resolve() != null) {
                    return result;
                }
            }
        }
        // If not resolvable above and the referenced factory bean doesn't exist yet,
        // exit here - we don't want to force the creation of another bean just to
        // obtain a FactoryBean's object type...
        //如果经过上述流程还是无法解析出FactoryBean生产的对象的类型,那么就直接返回NONE
        if (!isBeanEligibleForMetadataCaching(factoryBeanName)) {
            return ResolvableType.NONE;
        }
    }

    // If we're allowed, we can create the factory bean and call getObjectType() early
    //允许提前初始化这个FactoryBean
    if (allowInit) {
        /**
         * 单例多例初始化策略
         * 此时容器可能已经开始实例化对象了,会从二级三级缓存中拿到预实例化的对象
         */
        FactoryBean<?> factoryBean = (mbd.isSingleton() ?
                                      getSingletonFactoryBeanForTypeCheck(beanName, mbd) :
                                      getNonSingletonFactoryBeanForTypeCheck(beanName, mbd));
        if (factoryBean != null) {
            // Try to obtain the FactoryBean's object type from this early stage of the instance.
            //调用第一种getTypeForFactoryBean方法,来获取FactoryBean生产的对象的类型
            Class<?> type = getTypeForFactoryBean(factoryBean);
            if (type != null) {
                return ResolvableType.forClass(type);
            }
            // No type found for shortcut FactoryBean instance:
            // fall back to full creation of the FactoryBean instance.
            //里面调用doGetBean方法创建FactoryBean实例,然后获取它生产的对象的类型
            return super.getTypeForFactoryBean(beanName, mbd, true);
        }
    }

    //静态工厂方法
    if (factoryBeanName == null && mbd.hasBeanClass() && factoryMethodName != null) {
        // No early bean instantiation possible: determine FactoryBean's type from
        // static factory method signature or from class inheritance hierarchy...
        //获取工厂方法生产的FactoryBean的泛型信息
        return getTypeForFactoryBeanFromMethod(mbd.getBeanClass(), factoryMethodName);
    }
    //获取当前bean的FactoryBean接口的泛型信息
    result = getFactoryBeanGeneric(beanType);
    if (result.resolve() != null) {
        return result;
    }
    return ResolvableType.NONE;
}
3.5.2.2 BeanDefinitionHolder

先来看这个类的属性,就三个,看名字我们也知道它代表什么。

public class BeanDefinitionHolder implements BeanMetadataElement {

    /**
     * BeanMetadataElement接口的getSource方法获取的是这个对象
     */
    private final BeanDefinition beanDefinition;

    private final String beanName;

    //别名
    @Nullable
    private final String[] aliases;
}

其实就是将某个BeanDefinintion的相关信息集中到一起,方便我们使用

3.5.2.3 isFactoryBean方法

第一种:针对spring管理的bean(有BeanDefinition),判断mbd是不是一个FactoryBean

/**
 * Check whether the given bean is defined as a {@link FactoryBean}.
 * @param beanName the name of the bean
 * @param mbd the corresponding bean definition
 */
protected boolean isFactoryBean(String beanName, RootBeanDefinition mbd) {
    Boolean result = mbd.isFactoryBean;
    if (result == null) {
        //获取mbd对应的bean的类型
        Class<?> beanType = predictBeanType(beanName, mbd, FactoryBean.class);
        result = (beanType != null && FactoryBean.class.isAssignableFrom(beanType));
        mbd.isFactoryBean = result;
    }
    return result;
}

第二种:比第一种功能更强大,它会先判断beanName对应的实例,其次才是BeanDefinition,也就是说,这个方法可以判断用户手动注册到容器的实例是不是一个FactoryBean

@Override
public boolean isFactoryBean(String name) throws NoSuchBeanDefinitionException {
    String beanName = transformedBeanName(name);
    //从容器中获取beanName对应的实例
    Object beanInstance = getSingleton(beanName, false);
    //有实例
    if (beanInstance != null) {
        return (beanInstance instanceof FactoryBean);
    }
    //无实例,且子容器没有beanName对应的BeanDefinition,递归检查父容器
    // No singleton instance found -> check bean definition.
    if (!containsBeanDefinition(beanName) && getParentBeanFactory() instanceof ConfigurableBeanFactory) {
        // No bean definition found in this factory -> delegate to parent.
        return ((ConfigurableBeanFactory) getParentBeanFactory()).isFactoryBean(name);
    }
    //调用第一种方法判断BeanDefinition对应的bean是不是一个FactoryBean
    return isFactoryBean(beanName, getMergedLocalBeanDefinition(beanName));
}
3.5.2.4 predictBeanType方法(预测对应的bean的类型)
@Override
@Nullable
protected Class<?> predictBeanType(String beanName, RootBeanDefinition mbd, Class<?>... typesToMatch) {
    //得到mbd对应的类型
    Class<?> targetType = determineTargetType(beanName, mbd, typesToMatch);
    // Apply SmartInstantiationAwareBeanPostProcessors to predict the
    // eventual type after a before-instantiation shortcut.
    /**
     * 工厂中有一个标志位hasInstantiationAwareBeanPostProcessors
     * 当调用addBeanPostProcessor方法向容器中注册一个InstantiationAwareBeanPostProcessor对象
     * 时,就会将该标志位置为true,表明容器中包含InstantiationAwareBeanPostProcessor对象,
     * 见3.3.11
     * hasInstantiationAwareBeanPostProcessors()返回该标志位的值
     */
    if (targetType != null && !mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
        boolean matchingOnlyFactoryBean = typesToMatch.length == 1 && typesToMatch[0] == FactoryBean.class;
        for (BeanPostProcessor bp : getBeanPostProcessors()) {
            if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {
                SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp;
                /**
                 * 执行SmartInstantiationAwareBeanPostProcessor接口的predictBeanType方法
                 * 最终预测该bean的类型
                 */
                Class<?> predicted = ibp.predictBeanType(targetType, beanName);
                if (predicted != null &&
                    (!matchingOnlyFactoryBean || FactoryBean.class.isAssignableFrom(predicted))) {
                    return predicted;
                }
            }
        }
    }
    return targetType;
}


/**
 * Determine the target type for the given bean definition.
 * @param beanName the name of the bean (for error handling purposes)
 * @param mbd the merged bean definition for the bean
 * @param typesToMatch the types to match in case of internal type matching purposes
 * (also signals that the returned {@code Class} will never be exposed to application code)
 * @return the type for the bean if determinable, or {@code null} otherwise
 */
@Nullable
protected Class<?> determineTargetType(String beanName, RootBeanDefinition mbd, Class<?>... typesToMatch) {
    //获取该bean对应的类型
    Class<?> targetType = mbd.getTargetType();
    
    //工厂方法创建的bean的类型解析
    if (targetType == null) {
        targetType = (mbd.getFactoryMethodName() != null ?
                      getTypeForFactoryMethod(beanName, mbd, typesToMatch) :
                      resolveBeanClass(mbd, beanName, typesToMatch));
        if (ObjectUtils.isEmpty(typesToMatch) || getTempClassLoader() == null) {
            mbd.resolvedTargetType = targetType;
        }
    }
    return targetType;
}

这里执行SmartInstantiationAwareBeanPostProcessor接口的predictBeanType方法是什么意思呢?

springSmartInstantiationAwareBeanPostProcessor接口中添加了一个predictBeanType方法,该方法会在postProcessBeforeInstantiation方法执行之前预测该bean的类型。这里提前说一下,InstantiationAwareBeanPostProcessor接口的postProcessBeforeInstantiation方法可以在实例化之前返回一个对象,这样就会跳过后面spring标准的实例化,属性填充,初始化流程。

下面是RootBeanDefinition类的getTargetType方法

/**
 * Return the target type of this bean definition, if known
 * (either specified in advance or resolved on first instantiation).
 * @since 3.2.2
 */
@Nullable
public Class<?> getTargetType() {
    //resolvedTargetType是已经解析的类型
    if (this.resolvedTargetType != null) {
        return this.resolvedTargetType;
    }
    //spring解析XML配置文件或配置类,会将用户定义的bean的类型包装为ResolvableType,保存到targetType属性中
    ResolvableType targetType = this.targetType;
    return (targetType != null ? targetType.resolve() : null);
}
3.5.3 getTypeForFactoryBeanFromMethod方法(获取工厂方法生产的FactoryBean的泛型信息)
/**
 * Introspect the factory method signatures on the given bean class,
 * trying to find a common {@code FactoryBean} object type declared there.
 * @param beanClass the bean class to find the factory method on
 * @param factoryMethodName the name of the factory method
 * @return the common {@code FactoryBean} object type, or {@code null} if none
 */
private ResolvableType getTypeForFactoryBeanFromMethod(Class<?> beanClass, String factoryMethodName) {
    // CGLIB subclass methods hide generic parameters; look at the original user class.
    //获取源clazz对象
    Class<?> factoryBeanClass = ClassUtils.getUserClass(beanClass);
    //反射遍历所有的方法,得到指定工厂方法返回类型(FactoryBean)中的泛型信息
    FactoryBeanMethodTypeFinder finder = new FactoryBeanMethodTypeFinder(factoryMethodName);
    //doWithMethods方法回调了finder中的doWith方法
    ReflectionUtils.doWithMethods(factoryBeanClass, finder, ReflectionUtils.USER_DECLARED_METHODS);
    return finder.getResult();
}

我们先来看一下这个FactoryBeanMethodTypeFinder,它是AbstractAutowireCapableBeanFactory类中定义的一个内部类

/**
 * {@link MethodCallback} used to find {@link FactoryBean} type information.
 */
private static class FactoryBeanMethodTypeFinder implements MethodCallback {

    private final String factoryMethodName;

    //FactoryBean中的泛型信息
    private ResolvableType result = ResolvableType.NONE;

    //构造方法,初始化工厂方法名
    FactoryBeanMethodTypeFinder(String factoryMethodName) {
        this.factoryMethodName = factoryMethodName;
    }

    @Override
    public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
        //工厂方法生产FactoryBean
        if (isFactoryBeanMethod(method)) {
            //内省解析工厂方法的返回类型
            ResolvableType returnType = ResolvableType.forMethodReturnType(method);
            //获取返回类型中指定的泛型
            ResolvableType candidate = returnType.as(FactoryBean.class).getGeneric();
            if (this.result == ResolvableType.NONE) {
                this.result = candidate;
            } 
            else {
                Class<?> resolvedResult = this.result.resolve();
                //获取给定类的共同祖先类
                Class<?> commonAncestor = ClassUtils.determineCommonAncestor(candidate.resolve(), resolvedResult);
                if (!ObjectUtils.nullSafeEquals(resolvedResult, commonAncestor)) {
                    this.result = ResolvableType.forClass(commonAncestor);
                }
            }
        }
    }

    //判断该工厂方法是不是生产一个FactoryBean
    private boolean isFactoryBeanMethod(Method method) {
        return (method.getName().equals(this.factoryMethodName) &&
                FactoryBean.class.isAssignableFrom(method.getReturnType()));
    }

    ResolvableType getResult() {
        Class<?> resolved = this.result.resolve();
        boolean foundResult = resolved != null && resolved != Object.class;
        return (foundResult ? this.result : ResolvableType.NONE);
    }
}

总结:

  • FactoryBeanMethodTypeFinder封装了工厂方法生产出来的FactoryBean的泛型信息。
  • getTypeForFactoryBeanFromMethod方法会遍历beanClass的所有的方法,找到beanClass中定义的factoryMethodName方法的返回值的泛型信息。
3.5.4 getBeanNamesForType方法(获取类型对应的所有的beanbeanName
public String[] getBeanNamesForType(@Nullable Class<?> type, boolean includeNonSingletons, boolean allowEagerInit) {
    //configurationFrozen默认为false(true表示冻结当前容器中的BeanDefinition)
    if (!isConfigurationFrozen() || type == null || !allowEagerInit) {
        //根据类型获取对应的beanNames
        return doGetBeanNamesForType(ResolvableType.forRawClass(type), includeNonSingletons, allowEagerInit);
    }
    
    //下面是容器中以空间换时间的缓存,直接从缓存中获取
    Map<Class<?>, String[]> cache =
        (includeNonSingletons ? this.allBeanNamesByType : this.singletonBeanNamesByType);
    String[] resolvedBeanNames = cache.get(type);
    if (resolvedBeanNames != null) {
        return resolvedBeanNames;
    }
    //缓存中没有就调用doGetBeanNamesForType获取容器中类型对应的beanNames
    resolvedBeanNames = doGetBeanNamesForType(ResolvableType.forRawClass(type), includeNonSingletons, true);
    if (ClassUtils.isCacheSafe(type, getBeanClassLoader())) {
        cache.put(type, resolvedBeanNames);
    }
    return resolvedBeanNames;
}

根据类型获取对应的beanNames

private String[] doGetBeanNamesForType(ResolvableType type, boolean includeNonSingletons, boolean allowEagerInit) {
    //这个list集合保存类型对应的BeanNames
    List<String> result = new ArrayList<>();

    // Check all bean definitions.
    //遍历容器中所有的beanNames
    for (String beanName : this.beanDefinitionNames) {
        // Only consider bean as eligible if the bean name is not defined as alias for some other bean.
        //非别名
        if (!isAlias(beanName)) {
            try {
                //合并具有父子关系的beanDefinition,见3.5.1
                RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
                // Only check bean definition if it is complete.
                if (!mbd.isAbstract() && (allowEagerInit ||
                                          (mbd.hasBeanClass() || !mbd.isLazyInit() || isAllowEagerClassLoading()) &&
                                          !requiresEagerInitForType(mbd.getFactoryBeanName()))) {
                    //是不是一个factoryBean,见3.5.2.3
                    boolean isFactoryBean = isFactoryBean(beanName, mbd);
                    //获取包装的BeanDefinition的持有者
                    BeanDefinitionHolder dbd = mbd.getDecoratedDefinition();
                    boolean matchFound = false;
                    //containsSingleton(beanName) 从单例对象池中获取该beanName对应的单例对象
                    boolean allowFactoryBeanInit = (allowEagerInit || containsSingleton(beanName));
                    boolean isNonLazyDecorated = (dbd != null && !mbd.isLazyInit());
                    //不是FactoryBean
                    if (!isFactoryBean) {
                        if (includeNonSingletons || isSingleton(beanName, mbd, dbd)) {
                            //判断该beanName对应的bean是否是type类型的,见3.5.2
                            matchFound = isTypeMatch(beanName, type, allowFactoryBeanInit);
                        }
                    }
                    //FactoryBean
                    else {
                        if (includeNonSingletons || isNonLazyDecorated ||
                            (allowFactoryBeanInit && isSingleton(beanName, mbd, dbd))) {
                            //和FactoryBean生产的对象的类型比较
                            matchFound = isTypeMatch(beanName, type, allowFactoryBeanInit);
                        }
                        //不匹配则和FactoryBean类型比较
                        if (!matchFound) {
                            // In case of FactoryBean, try to match FactoryBean instance itself next.
                            beanName = FACTORY_BEAN_PREFIX + beanName;
                            matchFound = isTypeMatch(beanName, type, allowFactoryBeanInit);
                        }
                    }
                    //匹配成功,添加到集合中保存
                    if (matchFound) {
                        result.add(beanName);
                    }
                }
            }
            catch (CannotLoadBeanClassException | BeanDefinitionStoreException ex) {
                if (allowEagerInit) {
                    throw ex;
                }
                // Probably a placeholder: let's ignore it for type matching purposes.
                LogMessage message = (ex instanceof CannotLoadBeanClassException ?
                                      LogMessage.format("Ignoring bean class loading failure for bean '%s'", beanName) :
                                      LogMessage.format("Ignoring unresolvable metadata in bean definition '%s'", beanName));
                logger.trace(message, ex);
                // Register exception, in case the bean was accidentally unresolvable.
                onSuppressedException(ex);
            }
            catch (NoSuchBeanDefinitionException ex) {
                // Bean definition got removed while we were iterating -> ignore.
            }
        }
    }

    // Check manually registered singletons too.
    //检查用户手动注册到容器中的bean
    for (String beanName : this.manualSingletonNames) {
        try {
            // In case of FactoryBean, match object created by FactoryBean.
            //判断beanName对应的实例是不是一个FactoryBean,见3.5.2.3
            if (isFactoryBean(beanName)) {
                //都是先判断FactoryBean生产的对象的类型,不成功才判读FactoryBean
                if ((includeNonSingletons || isSingleton(beanName)) && isTypeMatch(beanName, type)) {
                    result.add(beanName);
                    // Match found for this bean: do not match FactoryBean itself anymore.
                    continue;
                }
                // In case of FactoryBean, try to match FactoryBean itself next.
                beanName = FACTORY_BEAN_PREFIX + beanName;
            }
            // Match raw bean instance (might be raw FactoryBean).
            if (isTypeMatch(beanName, type)) {
                result.add(beanName);
            }
        }
        catch (NoSuchBeanDefinitionException ex) {
            // Shouldn't happen - probably a result of circular reference resolution...
            logger.trace(LogMessage.format(
                "Failed to check manually registered singleton with name '%s'", beanName), ex);
        }
    }

    return StringUtils.toStringArray(result);
}
3.5.5 getType()方法(根据beanName获取对应bean的类型)

这是AbstractBeanFactory类的一个重要方法,因为该方法涉及到很多方法的源码都在此处讲解,所以此方法的源码讲解也放在此处。

@Override
@Nullable
public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
    return getType(name, true);
}

@Override
@Nullable
public Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException {
    String beanName = transformedBeanName(name);

    // Check manually registered singletons.
    //获取缓存中beanName对应的实例
    Object beanInstance = getSingleton(beanName, false);

    /************************************已实例化*******************************/
    //缓存中有该beanName对应的实例
    if (beanInstance != null && beanInstance.getClass() != NullBean.class) {
        //FactoryBean,且name不以&开头
        if (beanInstance instanceof FactoryBean && !BeanFactoryUtils.isFactoryDereference(name)) {
            //获取FactoryBean生产的对象的类型,见3.5.2.1
            return getTypeForFactoryBean((FactoryBean<?>) beanInstance);
        }
        //非FactoryBean的类型
        else {
            return beanInstance.getClass();
        }
    }

    /************************************未实例化*******************************/
    //缓存中没有该beanName对应的实例,先去父容器中递归检查
    // No singleton instance found -> check bean definition.
    BeanFactory parentBeanFactory = getParentBeanFactory();
    if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
        // No bean definition found in this factory -> delegate to parent.
        return parentBeanFactory.getType(originalBeanName(name));
    }

    //获取beanName对应的合并之后的BeanDefinition,见3.5.1
    RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);

    // Check decorated bean definition, if any: We assume it'll be easier
    // to determine the decorated bean's type than the proxy's type.
    //获取包装的BeanDefinition对应的bean的类型
    BeanDefinitionHolder dbd = mbd.getDecoratedDefinition();
    if (dbd != null && !BeanFactoryUtils.isFactoryDereference(name)) {
        RootBeanDefinition tbd = getMergedBeanDefinition(dbd.getBeanName(), dbd.getBeanDefinition(), mbd);
        //预测包装的BeanDefinition对应的bean的类型,见3.5.2.4
        Class<?> targetClass = predictBeanType(dbd.getBeanName(), tbd);
        if (targetClass != null && !FactoryBean.class.isAssignableFrom(targetClass)) {
            return targetClass;
        }
    }

    //预测原始的BeanDefinition对应的bean的类型,见3.5.2.4
    Class<?> beanClass = predictBeanType(beanName, mbd);

    //预测类型为FactoryBean
    // Check bean class whether we're dealing with a FactoryBean.
    if (beanClass != null && FactoryBean.class.isAssignableFrom(beanClass)) {
        //获取FactoryBean生产的对象的类型,见3.5.2.1
        if (!BeanFactoryUtils.isFactoryDereference(name)) {
            // If it's a FactoryBean, we want to look at what it creates, not at the factory class.
            return getTypeForFactoryBean(beanName, mbd, allowFactoryBeanInit).resolve();
        }
        else {
            return beanClass;
        }
    }
    //预测类型非FactoryBean
    else {
        //直接返回预测类型
        return (!BeanFactoryUtils.isFactoryDereference(name) ? beanClass : null);
    }
}

3.6 Register bean processors that intercept bean creation(注册BeanPostProcessor

/**
 * Instantiate and register all BeanPostProcessor beans,
 * respecting explicit order if given.
 * <p>Must be called before any instantiation of application beans.
 * 实例化和注册所有的BeanPostProcessor
 */
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
    //具体的逻辑委托给了PostProcessorRegistrationDelegate类的registerBeanPostProcessors方法
    PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
}

实例化和注册所有的BeanPostProcessor,委派模式

public static void registerBeanPostProcessors(
    ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) {

    //获取容器中所有类型为BeanPostProcessor的名字,见3.5.4
    String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);

    // Register BeanPostProcessorChecker that logs an info message when
    // a bean is created during BeanPostProcessor instantiation, i.e. when
    // a bean is not eligible for getting processed by all BeanPostProcessors.
    /**
     * beanFactory.getBeanPostProcessorCount()会获取到beanFactory中BeanPostProcessor
     * 的数量,目前有两个,一个是3.3.4中添加的ApplicationContextAwareProcessor
     * 另一个是3.3.7中添加的AApplicationListenerDetector
     * 总之,beanProcessorTargetCount包含了统计了容器中所有的BeanPostProcessor的数量
     */
    int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length;
    /**
     * 添加了一个BeanPostProcessor
     * 这个BeanPostProcessor作用是判断在当前方法中使用getBean方法实例化的对象
     * 是不是一个合格的BeanPostProcessor,不合格就打印日志
     */
    beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));

    // Separate between BeanPostProcessors that implement PriorityOrdered,
    // Ordered, and the rest.
    /**
     * 接下来的代码和执行BeanFactoryPostProcessor差不多
     * 都是找到所有的BeanPostProcessor,按实现PriorityOrdered接口,添加了Ordered注解的,
     * 除了上面两个剩下的,三类分别实例化,排序,注册到beanFactory中(注意啊,这些BeanPostProcessor
     * 现在都不执行)
     */
    List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
    List<BeanPostProcessor> internalPostProcessors = new ArrayList<>();
    List<String> orderedPostProcessorNames = new ArrayList<>();
    List<String> nonOrderedPostProcessorNames = new ArrayList<>();
    for (String ppName : postProcessorNames) {
        if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
            BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
            priorityOrderedPostProcessors.add(pp);
            if (pp instanceof MergedBeanDefinitionPostProcessor) {
                internalPostProcessors.add(pp);
            }
        }
        else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
            orderedPostProcessorNames.add(ppName);
        }
        else {
            nonOrderedPostProcessorNames.add(ppName);
        }
    }

    // First, register the BeanPostProcessors that implement PriorityOrdered.
    sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
    registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors);

    // Next, register the BeanPostProcessors that implement Ordered.
    List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
    for (String ppName : orderedPostProcessorNames) {
        BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
        orderedPostProcessors.add(pp);
        if (pp instanceof MergedBeanDefinitionPostProcessor) {
            internalPostProcessors.add(pp);
        }
    }
    sortPostProcessors(orderedPostProcessors, beanFactory);
    registerBeanPostProcessors(beanFactory, orderedPostProcessors);

    // Now, register all regular BeanPostProcessors.
    List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
    for (String ppName : nonOrderedPostProcessorNames) {
        BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class);
        nonOrderedPostProcessors.add(pp);
        if (pp instanceof MergedBeanDefinitionPostProcessor) {
            internalPostProcessors.add(pp);
        }
    }
    registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);

    // Finally, re-register all internal BeanPostProcessors.
    sortPostProcessors(internalPostProcessors, beanFactory);
    registerBeanPostProcessors(beanFactory, internalPostProcessors);

    // Re-register post-processor for detecting inner beans as ApplicationListeners,
    // moving it to the end of the processor chain (for picking up proxies etc).
    //重新注册一个ApplicationListenerDetector(主要是为了将这个BeanPostProcessor移到末尾)
    beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));
}

3.7 Initialize message source for this context(国际化)

/**
 * Initialize the MessageSource.
 * Use parent's if none defined in this context.
 * 初始化MessageSource
 */
protected void initMessageSource() {
    ConfigurableListableBeanFactory beanFactory = getBeanFactory();
    /**
     * 这里是判断beanDefinition中是已经存在一个MessageSource(用户自定义的)
     * 从这里可以看出来,如果我们需要使用这个功能,那么我们可以使用配置类或者xml
     * 自定义一个MessageSource放到容器中,名字固定为messageSource
     */
    if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {
        this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);
        // Make MessageSource aware of parent MessageSource.
        if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource) {
            HierarchicalMessageSource hms = (HierarchicalMessageSource) this.messageSource;
            if (hms.getParentMessageSource() == null) {
                // Only set parent context as parent MessageSource if no parent MessageSource
                // registered already.
                hms.setParentMessageSource(getInternalParentMessageSource());
            }
        }
        if (logger.isTraceEnabled()) {
            logger.trace("Using MessageSource [" + this.messageSource + "]");
        }
    }
    else {
        // Use empty MessageSource to be able to accept getMessage calls.
        //创建一个空的MessageSource,并放到容器中
        DelegatingMessageSource dms = new DelegatingMessageSource();
        dms.setParentMessageSource(getInternalParentMessageSource());
        this.messageSource = dms;
        beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);
        if (logger.isTraceEnabled()) {
            logger.trace("No '" + MESSAGE_SOURCE_BEAN_NAME + "' bean, using [" + this.messageSource + "]");
        }
    }
}

3.8 Initialize event multicaster for this context(初始化事件多播器)

/**
 * Initialize the ApplicationEventMulticaster.
 * Uses SimpleApplicationEventMulticaster if none defined in the context.
 * @see org.springframework.context.event.SimpleApplicationEventMulticaster
 * 初始化事件多播器
 * 如果客户自定义了一个多播器就实例化它,然后将它放到容器中
 * 否则就使用默认的SimpleApplicationEventMulticaster多播器
 * 流程比较简单,和上面的MessageSource差不多
 */
protected void initApplicationEventMulticaster() {
    ConfigurableListableBeanFactory beanFactory = getBeanFactory();
    if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
        //实例化客户自定义的多播器(会自动注册到容器中)
        this.applicationEventMulticaster =
            beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
        if (logger.isTraceEnabled()) {
            logger.trace("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
        }
    }
    else {
        this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
        //注册到容器中
        beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
        if (logger.isTraceEnabled()) {
            logger.trace("No '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "' bean, using " +
                         "[" + this.applicationEventMulticaster.getClass().getSimpleName() + "]");
        }
    }
}

3.9 Initialize other special beans in specific context subclasses(允许在上下文子类中初始化其他特殊的bean

/**
 * Template method which can be overridden to add context-specific refresh work.
 * Called on initialization of special beans, before instantiation of singletons.
 * <p>This implementation is empty.
 * @throws BeansException in case of errors
 * @see #refresh()
 * 这是一个空方法,留着子类实现的(如果用户有一些其他特殊的bean需要实例化(不需要BeanPostProcessor
 * 增强),就可以在这里进行
 */
protected void onRefresh() throws BeansException {
    // For subclasses: do nothing by default.
}

3.10 Check for listener beans and register them(注册所有的listener,此处不会实例化监听器)

/**
 * Add beans that implement ApplicationListener as listeners.
 * Doesn't affect other listeners, which can be added without being beans.
 */
protected void registerListeners() {
    // Register statically specified listeners first.
    //将已经实例化的listener注册到多播器中
    for (ApplicationListener<?> listener : getApplicationListeners()) {
        getApplicationEventMulticaster().addApplicationListener(listener);
    }

    // Do not initialize FactoryBeans here: We need to leave all regular beans
    // uninitialized to let post-processors apply to them!
    //获取容器中所有BeanDefinition中的监听器的名字,并注册名字到多播器中,这里没有实例化这些监听器
    String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
    for (String listenerBeanName : listenerBeanNames) {
        getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
    }

    // Publish early application events now that we finally have a multicaster...
    //使用多播器发布所有早期事件
    Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
    //使容器中的早期事件集合失效,因为此时已经有事件多播器了,事件可以直接发布,不需要缓存
    this.earlyApplicationEvents = null;
    if (!CollectionUtils.isEmpty(earlyEventsToProcess)) {
        for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
            getApplicationEventMulticaster().multicastEvent(earlyEvent);
        }
    }
}

3.11 Instantiate all remaining (non-lazy-init) singletons(实例化剩下的所有单例的,非懒加载的BeanDefinition

/**
 * Finish the initialization of this context's bean factory,
 * initializing all remaining singleton beans.
 */
protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
    // Initialize conversion service for this context.
    //如果有的话,实例化ConversionService(统一类型转换服务)
    if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
        beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
        beanFactory.setConversionService(
            beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
    }

    // Register a default embedded value resolver if no bean post-processor
    // (such as a PropertyPlaceholderConfigurer bean) registered any before:
    // at this point, primarily for resolution in annotation attribute values.
    /**
     * 添加一个内部的值解析器
     * 可以很明显看到这个值解析器就是借助我们初始化环境的时候用来解析XML配置文件路径
     * 的PropertySourcesPropertyResolver解析器。见2.2
     * 所以这个解析器就是去环境找对应值
     */
    if (!beanFactory.hasEmbeddedValueResolver()) {
        beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
    }

    // Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
    //实例化LoadTimeWeaverAware
    String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
    for (String weaverAwareName : weaverAwareNames) {
        getBean(weaverAwareName);
    }

    // Stop using the temporary ClassLoader for type matching.
    //停止使用临时的类加载器
    beanFactory.setTempClassLoader(null);

    // Allow for caching all bean definition metadata, not expecting further changes.
    //允许缓存所有beanDefinition元数据,实际上就是缓存所有bean的名字,冻结配置
    beanFactory.freezeConfiguration();

    // Instantiate all remaining (non-lazy-init) singletons.
    //这个方法是重点,它实例化容器中剩下的所有非懒加载的单例bean
    beanFactory.preInstantiateSingletons();
}

实例化容器中剩下的所有非懒加载的单例bean

public void preInstantiateSingletons() throws BeansException {
    if (logger.isTraceEnabled()) {
        logger.trace("Pre-instantiating singletons in " + this);
    }

    // Iterate over a copy to allow for init methods which in turn register new bean definitions.
    // While this may not be part of the regular factory bootstrap, it does otherwise work fine.
    //beanDefinitionNames的副本
    List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);

    // Trigger initialization of all non-lazy singleton beans...
    //遍历beanDefinitionNames,获取对应的beanDefinition,然后实例化
    for (String beanName : beanNames) {
        //根据beanName获取本地的已经合并的beanDefinition,见3.5.1
        RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
        // 非抽象,单例,非懒加载
        if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
            //是FactoryBean,见3.5.2.3
            if (isFactoryBean(beanName)) {
                //名字前加&符,先创建工厂对象
                Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
                if (bean instanceof FactoryBean) {
                    FactoryBean<?> factory = (FactoryBean<?>) bean;
                    boolean isEagerInit;
                    if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
                        isEagerInit = AccessController.doPrivileged(
                            (PrivilegedAction<Boolean>) ((SmartFactoryBean<?>) factory)::isEagerInit,
                            getAccessControlContext());
                    }
                    else {
                        //SmartFactoryBean接口可以指定是否需要提前实例化化FactoryBean创建的对象
                        isEagerInit = (factory instanceof SmartFactoryBean &&
                                       ((SmartFactoryBean<?>) factory).isEagerInit());
                    }
                    if (isEagerInit) {
                        //实例化beanName对应的bean
                        getBean(beanName);
                    }
                }
            }
            //非FactoryBean
            else {
                //直接实例化beanName对应的bean
                getBean(beanName);
            }
        }
    }

    // Trigger post-initialization callback for all applicable beans...
    //遍历调用所有实现了SmartInitializingSingleton接口的bean的afterSingletonsInstantiated方法
    for (String beanName : beanNames) {
        Object singletonInstance = getSingleton(beanName);
        if (singletonInstance instanceof SmartInitializingSingleton) {
            SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
            if (System.getSecurityManager() != null) {
                AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
                    smartSingleton.afterSingletonsInstantiated();
                    return null;
                }, getAccessControlContext());
            }
            else {
                smartSingleton.afterSingletonsInstantiated();
            }
        }
    }
}

总结:

  1. 容器调用getBean(beanName)方法来实例化bean
  2. 当所有的bean实例化完成后才会调用实现了SmartInitializingSingleton接口beanafterSingletonsInstantiated方法。
  3. 如果beanName对应的BeanDefinition是一个FactoryBean,那么会先创建FactoryBean对象,然后再判断是否需要提前初始化FactoryBean创建的对象。

3.12 Last step: publish corresponding event(最后一步:发布对应的事件)

/**
 * Finish the refresh of this context, invoking the LifecycleProcessor's
 * onRefresh() method and publishing the
 * {@link org.springframework.context.event.ContextRefreshedEvent}.
 * 完成上下文的刷新,执行LifecycleProcessor的onRefresh()方法,并发布上下文刷新完成事件
 */
protected void finishRefresh() {
    // Clear context-level resource caches (such as ASM metadata from scanning).
    //清理缓存
    clearResourceCaches();

    // Initialize lifecycle processor for this context.
    //为上下文初始化一个LifecyleProcessor(默认为DefaultLifecycleProcessor)
    initLifecycleProcessor();

    // Propagate refresh to lifecycle processor first.
    //执行生命周期方法
    getLifecycleProcessor().onRefresh();

    // Publish the final event.
    //发布事件
    publishEvent(new ContextRefreshedEvent(this));

    // Participate in LiveBeansView MBean, if active.
    //生命周期相关
    LiveBeansView.registerApplicationContext(this);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值