Spring 源码分析

来源:互联网 发布:珠宝设计软件下载 编辑:程序博客网 时间:2024/06/05 04:35

为了读懂spring源码,我是根据debug来分析的。
这里写图片描述
一切的起点就是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 precedenceDEBUG [main] - Adding [systemEnvironment] PropertySource with lowest search precedenceDEBUG [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 definitionsDEBUG [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 0
原创粉丝点击