乐趣区

关于spring:IOC之bean的创建spring525

一、问题剖析

1.spring 容器的启动过程,启动期间都做了什么?什么时候创立单实例 bean?
2.ioc 是如何创立这些单实例 bean,是如何治理的?保留在哪里?

二、Debug 剖析

ClassPathXmlApplicationContext 的继承树

1. 创立 spring 容器,读取配置文件,类门路下加载配置文件

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

2. 最先加载的应该是动态初始化块,它会随着类的加载而加载

static {
// 在 WebLogic 8.1 中,在应用程序敞开时,紧急加载 ContextCLosedEvent 类以防止奇怪的类加载器问题。ContextClosedEvent.class.getName();}

3. 调用 ClassPathXmlApplicationContext 的构造方法

/**
 * 创立一个新的 classPathXmlApplicationcontext,从给定的 XML 文件加载定义,并主动刷新该 ontext。* @param applicationContext.xml
 * 如果上下文创立失败,@抛出 BeansException
 */
public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
// 调用类中其余结构
    this(new String[] {configLocation}, true, null);
}

4. 参数解析

参数名 参数值
String[] configLocations qpplicationContext.xml
refresh true
parent null
/**
 * 创立一个新的 classPathXmlApplicationContextwith 给定的父类,从给定的 XML 文件加载定义。* @param configLocations 资源地位数组
 * @param refresh 是否主动刷新上下文
 * 加载所有 bean 定义并创立所有单例。或者,在进一步的 configuri 之后手动调用刷新康泰克斯的
 * @param 父上下文后手动调用刷新
 * 如果上下文创立失败,@抛出 BeansException
 * @see #refresh()
 */

public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) throws BeansException {
    // 1. 初始化父类
    super(parent);
    // 2. 设置本地的配置信息
    setConfigLocations(configLocations);
    // 3. 实现 Spring 容器的初始化
    if (refresh) { //true 进入 refresh 办法
        refresh(); // 所有单实例创立实现}
}

5.refresh()解析

synchronized (this.startupShutdownMonitor) {
    // 筹备此上下文以进行刷新
    prepareRefresh();

    // spring 解析 xml 配置文件,将要创立的所有 bean 的配置信息保存起来
    ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

5.1 ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory()解析

5.1.1 obtainFreshBeanFactory();

    protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {refreshBeanFactory(); // 刷新 beanFactory
        return getBeanFactory();}

5.1.2 refreshBeanFactory();

// 对上下文的底层 bean 工厂执行一次理论刷新,敞开上一个 bean 工厂 (如果有的话) 和为上下文生命周期的下一阶段初始化 afreshbean 工厂。if (hasBeanFactory()) { // 判断是否曾经创立 beanFactory 如果存在 beanFactory 则销毁并敞开
    destroyBeans();
    closeBeanFactory();}
try {DefaultListableBeanFactory beanFactory = createBeanFactory();// 创立一个新的 beanFactory
    beanFactory.setSerializationId(getId());
    customizeBeanFactory(beanFactory);
    loadBeanDefinitions(beanFactory);// 通过 beanFactory 加载 beanDefinition
    synchronized (this.beanFactoryMonitor) {this.beanFactory = beanFactory;// 对 beanFactory 进行从新赋值。}
}
catch (IOException ex) {throw new ApplicationContextException("I/O error parsing bean definition source for" + getDisplayName(), ex);
}

5.1.3 createBeanFactory()

protected DefaultListableBeanFactory createBeanFactory() {// 传入这个 context 的父 context 外部的 beanFactory
   
return new DefaultListableBeanFactory(getInternalParentBeanFactory());// 由此可见这个 context 外部的 beanFacotry

5.2 解析完 xml 后

    // 筹备在此上下文中应用的 bean 工厂
    prepareBeanFactory(beanFactory);

    try {
        // 容许在上下文子类中对 bean 工厂进行后处理
        postProcessBeanFactory(beanFactory);

        // 调用上下文中注册为 bean 的工厂处理器
        invokeBeanFactoryPostProcessors(beanFactory);

        // 注册拦挡 bean 创立的 bean 处理器
        registerBeanPostProcessors(beanFactory);

        // 初始化消息来源, 用来反对国际化性能
        initMessageSource();

        // 初始化多事件派发器
        initApplicationEventMulticaster();

        // 初始化特定上下文子类中的其余专门化 bean,专门留给子类的办法
        onRefresh();

        // 查看侦听器 bean 并注册它们注册监听器
        registerListeners();
        
        // 实例化所有残余的 (非提早 -init) 单例
        finishBeanFactoryInitialization(beanFactory);

5.3 finishBeanFactoryInitialization(beanFactory)实现单实例 bean 创立;

protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
    // 初始化类型转换的组件服务(次要用于自定义类型转化)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));
    }
    // 如果没有 bean 前期解决
    //(例如 PropertyPlaceholderConfigurer bean)之前注册任何:
    // 此时,次要用于解析正文属性值。if (!beanFactory.hasEmbeddedValueResolver()) {beanFactory.addEmbeddedValueResolver(strVal ->   getEnvironment().resolvePlaceholders(strVal));
    }

    // 初始化加载时的 Autowire
    String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
    for (String weaverAwareName : weaverAwareNames) {getBean(weaverAwareName);
    }

    // 停止使用长期类加载器进行类型匹配
    beanFactory.setTempClassLoader(null);
    
    // 容许缓存所有 bean 定义元数据,不冀望进一步更改
    beanFactory.freezeConfiguration();

    // 实例化所有残余的 (非提早 -init) 单单实例 bean
    beanFactory.preInstantiateSingletons();}

beanFactory.preInstantiateSingletons()解析

// 日志记录
    if (logger.isTraceEnabled()) {logger.trace("Pre-instantiating singletons in" + this);
    }

    // 迭代一个正本以容许 init 办法注册新的 bean 定义。// 尽管这可能不是惯例工厂疏导程序的一部分,但它在其余方面工作得很好。//beanDefinitionNames 所有定义 bean 的名字
    List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);

    // 按程序初始化所有非懒加载的单实例 bean
    for (String beanName : beanNames) {
        // 依据 bean 的 id 获取到 bean 的具体定义信息
        RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
        // 判断 bean 不是一个形象的、非懒加载的单实例 bean,所有这是 ioc 中 bean 的默认个性
        if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
            // 如果该 bean 是工厂 bean,实现了 FactoryBean 接口的 bean,就进入判断
            if (isFactoryBean(beanName)) {Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
                if (bean instanceof FactoryBean) {final FactoryBean<?> factory = (FactoryBean<?>) bean;
                    boolean isEagerInit;
                    if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {isEagerInit = AccessController.doPrivileged((PrivilegedAction<Boolean>)
                                        ((SmartFactoryBean<?>) factory)::isEagerInit,
                                getAccessControlContext());
                    }
                    else {
                        isEagerInit = (factory instanceof SmartFactoryBean &&
                                ((SmartFactoryBean<?>) factory).isEagerInit());
                    }
                    if (isEagerInit) {getBean(beanName);
                    }
                }
            }
            else {getBean(beanName); // 因为我并没有应用工厂创立 bean,所以来到这行
            }
        }
    }

getBean(beanName):bean 创立的细节

    @Override
    public Object getBean(String name) throws BeansException {
        // 重写 BeanFactory 的办法
        return doGetBean(name, null, null, false); // 调用了 doGetBean 接口
    }

doGetBean(name, null, null, false):bean 创立的细节(重点)

参数

参数名 参数值
name bean 对象名称
requiredType null
args null
typeCheckOnly false
    protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
            @Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
        // 先拿到要创立的 bean 名称
        final String beanName = transformedBeanName(name);
        Object bean;

        // 从曾经注册的单实例 bean 外面查看有没有这个 bean
        Object sharedInstance = getSingleton(beanName);
        if (sharedInstance != null && args == null) { // 缓存里有则走上面的代码
            if (logger.isTraceEnabled()) {if (isSingletonCurrentlyInCreation(beanName)) {
                    logger.trace("Returning eagerly cached instance of singleton bean'" + beanName +
                            "'that is not fully initialized yet - a consequence of a circular reference");
                }
                else {logger.trace("Returning cached instance of singleton bean'" + beanName + "'");
                }
            }
            bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
        }

        else { // 第一次创立 bean 是没有的,所有走到这
            // 如果咱们曾经创立了这个 bean 实例,则会失败: 咱们假如在循环援用中。if (isPrototypeCurrentlyInCreation(beanName)) {throw new BeanCurrentlyInCreationException(beanName);
            }

            // 查看父工厂中是否存在 bean 定义。BeanFactory parentBeanFactory = getParentBeanFactory();
            if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
                // Not found -> check parent.
                String nameToLookup = originalBeanName(name);
                if (parentBeanFactory instanceof AbstractBeanFactory) {return ((AbstractBeanFactory) parentBeanFactory).doGetBean(nameToLookup, requiredType, args, typeCheckOnly);
                }
                else if (args != null) {
                    // Delegation to parent with explicit args.
                    return (T) parentBeanFactory.getBean(nameToLookup, args);
                }
                else if (requiredType != null) {
                    // No args -> delegate to standard getBean method.
                    return parentBeanFactory.getBean(nameToLookup, requiredType);
                }
                else {return (T) parentBeanFactory.getBean(nameToLookup);
                }
            }

            if (!typeCheckOnly) { //typeCheckOnly false
                // 标记 bean 的状态为曾经被创立,为了避免多线程并发创立 bean
                markBeanAsCreated(beanName); 
            }

            try {
                // 获取合并后的 bean 的定义信息
                final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
                checkMergedBeanDefinition(mbd, beanName, args);

                // 拿到以后 bean 创立之前所须要提前创立创立的 bean,depends-on 属性,调用依赖关系设置,如果有此属性就循环创立 deponds-on 属性依赖的 bean
                String[] dependsOn = mbd.getDependsOn();
                if (dependsOn != null) {for (String dep : dependsOn) {if (isDependent(beanName, dep)) {throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                    "Circular depends-on relationship between'" + beanName + "'and'" + dep + "'");
                        }
                        registerDependentBean(dep, beanName);
                        try {getBean(dep);
                        }
                        catch (NoSuchBeanDefinitionException ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName,
                                    "'"+ beanName +"' depends on missing bean '"+ dep +"'", ex);
                        }
                    }
                }

                // 如果是单例,创立 bean
                if (mbd.isSingleton()) {
                // getSington 办法里创立 bean
                    sharedInstance = getSingleton(beanName, () -> {
                        try {
                        //bean 的创立次要办法
                            return createBean(beanName, mbd, args);
                        }
                        catch (BeansException ex) {
                            // Explicitly remove instance from singleton cache: It might have been put there
                            // eagerly by the creation process, to allow for circular reference resolution.
                            // Also remove any beans that received a temporary reference to the bean.
                            destroySingleton(beanName);
                            throw ex;
                        }
                    });
                    bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
                }

getSingleton(beanName, () -> {} 细节

public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {Assert.notNull(beanName, "Bean name must not be null");
    synchronized (this.singletonObjects) {
        // 先从单实例对象中将这些 beanget 进去
        Object singletonObject = this.singletonObjects.get(beanName);
        

singletonObjects 是一个 hashMap

// 依照对象的名字和对象实例缓存所有单实例 bean
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);
        if (singletonObject == null) { // 第一次创立为 null。进入上面的办法
            if (this.singletonsCurrentlyInDestruction) {
                throw new BeanCreationNotAllowedException(beanName,
                        "Singleton bean creation not allowed while singletons of this factory are in destruction" +
                        "(Do not request a bean from a BeanFactory in a destroy method implementation!)");
            }
            if (logger.isDebugEnabled()) {logger.debug("Creating shared instance of singleton bean'" + beanName + "'");
            }
            beforeSingletonCreation(beanName);
            boolean newSingleton = false;
            boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
            if (recordSuppressedExceptions) {this.suppressedExceptions = new LinkedHashSet<>();
            }
            try {
                // 在这里利用了反射创立了单实例 bean
                singletonObject = singletonFactory.getObject();             
                newSingleton = true;
            }
            catch (IllegalStateException ex) {
                // 让单例对象同时隐式地呈现
                // 如果是,持续,因为异样表明状态。>               singletonObject = this.singletonObjects.get(beanName);
                if (singletonObject == null) {throw ex;}
            }
            catch (BeanCreationException ex) {if (recordSuppressedExceptions) {for (Exception suppressedException : this.suppressedExceptions) {ex.addRelatedCause(suppressedException);
                    }
                }
                throw ex;
            }
            finally {if (recordSuppressedExceptions) {this.suppressedExceptions = null;}
                afterSingletonCreation(beanName);
            }
            if (newSingleton) { 
                // 创立完单实例 bean 后调用此办法
                addSingleton(beanName, singletonObject);
                

addSingleton(beanName, singletonObject)细节

    protected void addSingleton(String beanName, Object singletonObject) {synchronized (this.singletonObjects) { 
        //singletonObjects 之前创立的 map 汇合,将 bean 对象退出到 map 中
        // 所以,创立好的对象最终会保留在这个 map 中
            this.singletonObjects.put(beanName, singletonObject);
            this.singletonFactories.remove(beanName);
            this.earlySingletonObjects.remove(beanName);
            this.registeredSingletons.add(beanName);
        }
    }
            }
        }
        return singletonObject;
    }
}
                else if (mbd.isPrototype()) {
                    // It's a prototype -> create a new instance.
                    Object prototypeInstance = null;
                    try {beforePrototypeCreation(beanName);
                        prototypeInstance = createBean(beanName, mbd, args);
                    }
                    finally {afterPrototypeCreation(beanName);
                    }
                    bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
                }

                else {String scopeName = mbd.getScope();
                    final Scope scope = this.scopes.get(scopeName);
                    if (scope == null) {throw new IllegalStateException("No Scope registered for scope name'" + scopeName + "'");
                    }
                    try {Object scopedInstance = scope.get(beanName, () -> {beforePrototypeCreation(beanName);
                            try {return createBean(beanName, mbd, args);
                            }
                            finally {afterPrototypeCreation(beanName);
                            }
                        });
                        bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
                    }
                    catch (IllegalStateException ex) {
                        throw new BeanCreationException(beanName,
                                "Scope'" + scopeName + "'is not active for the current thread; consider" +
                                "defining a scoped proxy for this bean if you intend to refer to it from a singleton",
                                ex);
                    }
                }
            }
            catch (BeansException ex) {cleanupAfterBeanCreationFailure(beanName);
                throw ex;
            }
        }

        // Check if required type matches the type of the actual bean instance.
        if (requiredType != null && !requiredType.isInstance(bean)) {
            try {T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType);
                if (convertedBean == null) {throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
                }
                return convertedBean;
            }
            catch (TypeMismatchException ex) {if (logger.isTraceEnabled()) {
                    logger.trace("Failed to convert bean'" + name + "'to required type'" +
                            ClassUtils.getQualifiedName(requiredType) + "'", ex);
                }
                throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
            }
        }
        return (T) bean;
    }
    // Trigger post-initialization callback for all applicable beans...
    for (String beanName : beanNames) {Object singletonInstance = getSingleton(beanName);
        if (singletonInstance instanceof SmartInitializingSingleton) {final SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
            if (System.getSecurityManager() != null) {AccessController.doPrivileged((PrivilegedAction<Object>) () -> {smartSingleton.afterSingletonsInstantiated();
                    return null;
                }, getAccessControlContext());
            }
            else {smartSingleton.afterSingletonsInstantiated();
            }
        }
    }
}

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

总结

最终单实例 bean 对象保留 DefaultSingletonBeanRegistry 类中的 singletonObjects 属性
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);

退出移动版