Spring IOC容器的初始化过程

1.容器初始化过程

整个过程可以理解为容器的初始化过程。

容器的初始化是通过AbstractApplicationContext的refresh()实现的。

 2.Bean的创建过程

有两个问题

1.bean的创建时机

bean是在什么时候被创建的,有哪些规则

2.bean的创建过程

bean是怎么创建的,会选择哪个构造函数?依赖如何注入?InitializingBean的set方法什么时候被调用?实现ApplicationContextAware,BeanFactoryAware,BeanNameAware,这些接口的bean的set方法何时被调用?

问题1 Bean的创建时机

finishBeanFactoryInitialization()中调用preInstantiateSingletons()会完成单例对象的创建

@Override
    public void preInstantiateSingletons() throws BeansException {
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("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.
        List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);

        // Trigger initialization of all non-lazy singleton beans...
        for (String beanName : beanNames) {
            RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
            if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
                if (isFactoryBean(beanName)) {
                    final FactoryBean<?> factory = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName);
                    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);
                }
            }
        }

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

问题2  bean的创建过程

对于bean的创建过程其实都是通过调用工厂的getBean方法来完成的,这里面将会完成对构造函数的选择,依赖注入等。

无论预先创建还是延迟加载都是调用getBean实现,AbstactBeanFactory定义了getBean的过程。

getBean过程很复杂,将单独拿一篇来讲。

 

posted @ 2017-11-21 16:32  hxwang  阅读(3763)  评论(0编辑  收藏  举报