Spring 源码分析

 2019-10-17 21:52  阅读(759)
文章分类:Spring boot

为了读懂spring源码,我是根据debug来分析的。
20191017100218\_1.png
一切的起点就是ApplicationContext。
根据构造函数的各种调用最终调用的是这个构造函数。

public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
                throws BeansException {

            super(parent);
            setConfigLocations(configLocations);
            if (refresh) {
                refresh();
            }
        }

到这里最终的方法就是refresh(),super只是做一些初始化工作,可以忽略。
以下是初始化发放做的事情:

DEBUG [main] - Adding [systemProperties] PropertySource with lowest search precedence
    DEBUG [main] - Adding [systemEnvironment] PropertySource with lowest search precedence
    DEBUG [main] - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]

没有任何有用的信息,直接忽略。
setConfiguration()这是一个简单的赋值操作。
而refresh()放最终调用的是AbstractApplicationContext的refresh()。

public void refresh() throws BeansException, IllegalStateException {
            synchronized (this.startupShutdownMonitor) {
                // Prepare this context for refreshing.
                prepareRefresh();

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

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

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

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

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

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

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

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

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

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

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

                catch (BeansException ex) {
                    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;
                }
            }
        }

preparRefresh()的输出

Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@26eeda37: startup date [Sun May 01 19:02:10 CST 2016]; root of context hierarchy

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

的输出:

DEBUG [main] - Loading bean definitions
    DEBUG [main] - Bean factory for org.springframework.context.support.ClassPathXmlApplicationContext@26eeda37: org.springframework.beans.factory.support.DefaultListableBeanFactory@2582e0fc: defining beans [countryService,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.support.PropertySourcesPlaceholderConfigurer#0,dataSource,sqlSessionFactory,tk.mybatis.spring.mapper.MapperScannerConfigurer#0]; root of factory hierarchy

我首先从这个方法开始分析:

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

调用注册在context里面的beanFactory中的方法


来源:[]()

点赞(0)
版权归原创作者所有,任何形式转载请联系作者; Java 技术驿站 >> Spring 源码分析

相关推荐