读spring源码记录(五)-- debug看清全流程

“莫道儒冠误此生,从来诗书不负人。”坚持就会有收获!

承接上篇记录,上篇主要是在说多读源码注解,而接下来都过程,更多是debug一步步观察对象变化,来进行阅读。一边阅读,一边某些变量拿捏不准的时候,进行debug。
下面这个图很重要,我觉得在读代码的时候帮助很大。


很重要的图

1.prepareRefresh();(准备上下文的刷新)

/**
     * Prepare this context for refreshing, setting its startup date and
     * active flag as well as performing any initialization of property sources.
     * 准备此上下文以进行刷新,设置其启动日期和活动标志以及执行属性源的任何初始化。
     */
    protected void prepareRefresh() {
        // Switch to active.
        this.startupDate = System.currentTimeMillis();
        this.closed.set(false);
        this.active.set(true);

        if (logger.isInfoEnabled()) {
            logger.info("Refreshing " + this);
        }

        // Initialize any placeholder property sources in the context environment.
        // 在上下文环境中初始化任何占位符属性源。
        initPropertySources();

        // Validate that all properties marked as required are resolvable:
        // see ConfigurablePropertyResolver#setRequiredProperties
        // 验证标记为必需的所有属性是否可解析:请参阅ConfigurablePropertyResolver #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.
            // 重置本地application listeners为预刷新状态
            this.applicationListeners.clear();
            this.applicationListeners.addAll(this.earlyApplicationListeners);
        }

        // Allow for the collection of early ApplicationEvents,
        // to be published once the multicaster is available...
        // 允许收集早期的ApplicationEvents,
        // 将在multicaster可用时发布...
        this.earlyApplicationEvents = new LinkedHashSet<>();
    }

这个是该方法的源码,翻译了部分注释。
前面几行,都是设置一些该对象的标识变量,initPropertySources()这个方法在当前类(AbstractApplicationContext)中是没有具体实现的,但是子类是有实现的,可以从下面的截图看到和最上方的图可以知道,并没有相关的类的实现这个方法,所以在当前过程中这个方法没有做任何处理,如果感觉不相信,就去每个实现类中打断点,看看会不会走到断点中。(愚蠢的我做了,确实没走进任何一个实现类中。)


接下来是getEnvironment().validateRequiredProperties()方法,getEnvironment()其实就是ConfigurableEnvironment对象,实际是new了一个StandardEnvironment(ConfigurableEnvironment的子孙类)返回来,这里最好debug看一下,代码很简单,但是变量的值可能拿捏不准。validateRequiredProperties()方法是一个接口,实现类有两个AbstractEnvironmentAbstractPropertyResolverStandardEnvironment也是AbstractEnvironment的子类,所以,选择查看AbstractEnvironment的方法,可以看到源码是调用了propertyResolver的方法,而propertyResolver又是一个ConfigurablePropertyResolver的对象,ConfigurablePropertyResolver的子类就是AbstractPropertyResolver(好家伙!又绕回来了,早知道就直接看AbstractPropertyResolver中的方法了)。

private final ConfigurablePropertyResolver propertyResolver =
            new PropertySourcesPropertyResolver(this.propertySources);

public void validateRequiredProperties() throws MissingRequiredPropertiesException {
        this.propertyResolver.validateRequiredProperties();
    }

直接查看AbstractPropertyResolver中的方法,这个用截图展示,在这里我打了一个断点,可以看出来,这requiredProperties的size为0,getEnvironment().validateRequiredProperties();整个方法在注释中解释为验证标记为必需的所有属性是否可解析。说明一切正常,那就继续往下看。

AbstractPropertyResolver

接下来是查看earlyApplicationListeners是否为null,为null就new一个,并将当前对象的applicationListeners全部放到容器中。


最后是初始化earlyApplicationEventsprepareRefresh()就结束了。

2.ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();(通知子类刷新内部的bean factory)

主要关注obtainFreshBeanFactory()方法的内容。当前所在类AbstractApplicationContext

    protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
        refreshBeanFactory();
        ConfigurableListableBeanFactory beanFactory = getBeanFactory();
        if (logger.isDebugEnabled()) {
            logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
        }
        return beanFactory;
    }

查看refreshBeanFactory()方法,它是一个接口方法,可以看到它的实现类分别为AbstractRefreshableApplicationContextGenericApplicationContext,根据最上面的继承图我们可以知道,我们需要看的是AbstractRefreshableApplicationContext中的实现方法。

protected final void refreshBeanFactory() throws BeansException {
        if (hasBeanFactory()) {
            destroyBeans();
            closeBeanFactory();
        }
        try {
            DefaultListableBeanFactory beanFactory = createBeanFactory();
            // 为了序列话指定id,如果需要的话,让这个BeanFactory从id反序列化到BeanFactory对象
            beanFactory.setSerializationId(getId());
            /**
             * 设置两个属性:
             * 1. 是否允许覆盖同名称的不同定义的对象
             * 2. 是否允许bean之间存在循环依赖
             */
            customizeBeanFactory(beanFactory);
            // 初始化DocumentReader,并进行XML文件读取和解析
            loadBeanDefinitions(beanFactory);
            synchronized (this.beanFactoryMonitor) {
                this.beanFactory = beanFactory;
            }
        }
        catch (IOException ex) {
            throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
        }
    }

首先是判断当前的beanFactory是否为null,不为null的话,就先释放掉资源,对于我们这种进行初始化的操作而言,肯定是null,debug验证也是null,所以直接看createBeanFactory ()
得到了一个DefaultListableBeanFactory对象,然后是设置序列化编号,customizeBeanFactory(beanFactory)从名字上来讲,是定制beanFactory,再看看代码,两行注释是我添加的,分别是allowBeanDefinitionOverridingallowCircularReferences两个Boolean变量的含义。

protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
        //是否允许覆盖同名称的不同定义的对象
        if (this.allowBeanDefinitionOverriding != null) {
            beanFactory.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
        }
        //是否允许bean之间存在循环依赖
        if (this.allowCircularReferences != null) {
            beanFactory.setAllowCircularReferences(this.allowCircularReferences);
        }
    }

这是一个定制beanFactory的方法,但是,这里实际的值都是为null,那么究竟何时才会设置这些值咧?


image.png

当然是子类中进行,举个栗子

public class MyClassPathXmlApplicationContext extends ClassPathXmlApplicationContext {

    ......
    
    @Override
    protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
        super.setAllowBeanDefinitionOverriding(false);
        super.setAllowCircularReferences(false);
        super.customizeBeanFactory(beanFactory);
    }
}

当然,也不是一定要设置这两个值,看spring自己的测试用例里面的代码,它就啥都没设置,这个得看自己的需求,不过一般的需求,是不会深入到要求我们自己设计一个ClassPathXmlApplicationContext这样的深度。

@Test
public void abstractRefreshableContextWithMisconfiguredBean() throws Exception {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext() {
            @Override
            protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
                super.customizeBeanFactory(beanFactory);
                registerMisconfiguredBeanDefinition(beanFactory);
            }
        };
        assertFactoryCountThroughoutLifecycle(ctx);
    }

这样customizeBeanFactory(beanFactory);也阅读完了,那么接下来是loadBeanDefinitions(beanFactory);初始化DocumentReader,并进行XML文件读取和解析,它是一个abstract的方法,实际是交给子类去实现的,它有四个实现,根据我们最上面的图可知道,我们应该看AbstractXmlApplicationContext中的实现。


代码如下:

private boolean validating = true;

protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
        // Create a new XmlBeanDefinitionReader for the given BeanFactory.
        // 为给定的BeanFactory创建一个新的XmlBeanDefinitionReader。
        XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);

        // Configure the bean definition reader with this context's
        // resource loading environment.
        //使用此上下文的资源加载环境配置bean定义阅读器。
        beanDefinitionReader.setEnvironment(this.getEnvironment());
        beanDefinitionReader.setResourceLoader(this);
        beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));

        // Allow a subclass to provide custom initialization of the reader,
        // then proceed with actually loading the bean definitions.
        // 允许子类提供阅读器的自定义初始化,
        //  然后继续实际加载bean定义。
        initBeanDefinitionReader(beanDefinitionReader);
        loadBeanDefinitions(beanDefinitionReader);
    }
protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader) {
        reader.setValidating(this.validating);
    }
protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {
        Resource[] configResources = getConfigResources();
        if (configResources != null) {
            reader.loadBeanDefinitions(configResources);
        }
        String[] configLocations = getConfigLocations();
        if (configLocations != null) {
            reader.loadBeanDefinitions(configLocations);
        }
    }

该方法是为了通过XmlBeanDefinitionReader加载bean定义。代码很简单,但是需要我们debug看一下具体的过程。我是基本上一行一行的debug,引用的方法内部也看了一边,重点关注loadBeanDefinitions(beanDefinitionReader);方法

1

2

3

4

可以进去看看loadBeanDefinitions方法,这里基本上是spring如何解析xml资源并且注册bean的过程,基本上完成了bean的解析,注册和IOC容器的实例化。这个代码值得一看,在整个过程中,spring用了很多方法的重载,这里面的内容实在太多了,我自己看了很久,里面内容很多,具体可以参照这篇文章的内容(https://www.imooc.com/article/13900),里面介绍了解析时使用的一些反射用法,提及了如何解析相关的xml,这里就就不多说了。

public int loadBeanDefinitions(String location, @Nullable Set<Resource> actualResources) throws BeanDefinitionStoreException {
        ResourceLoader resourceLoader = getResourceLoader();
        if (resourceLoader == null) {
            throw new BeanDefinitionStoreException(
                    "Cannot import bean definitions from location [" + location + "]: no ResourceLoader available");
        }

        if (resourceLoader instanceof ResourcePatternResolver) {
            // Resource pattern matching available.
            try {
                Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location);
                int loadCount = loadBeanDefinitions(resources);
                if (actualResources != null) {
                    for (Resource resource : resources) {
                        actualResources.add(resource);
                    }
                }
                if (logger.isDebugEnabled()) {
                    logger.debug("Loaded " + loadCount + " bean definitions from location pattern [" + location + "]");
                }
                return loadCount;
            }
            catch (IOException ex) {
                throw new BeanDefinitionStoreException(
                        "Could not resolve bean definition resource pattern [" + location + "]", ex);
            }
        }
        else {
            // Can only load single resources by absolute URL.
            Resource resource = resourceLoader.getResource(location);
            int loadCount = loadBeanDefinitions(resource);
            if (actualResources != null) {
                actualResources.add(resource);
            }
            if (logger.isDebugEnabled()) {
                logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]");
            }
            return loadCount;
        }
    }

基本上到这里ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();方法就结束,接下来该去看prepareBeanFactory(beanFactory);
看完整块obtainFreshBeanFactory方法,其实是需要花很多时间的,不是这里说的内容这么少,省略掉了bean的解析注册流程没有写,我自己看完都觉得很累了,读源码真的不是一件轻松的事情,能坚持读完的人是真的厉害的人。看完这个部分,我个人觉得spring前期的准备流程是很长的逻辑线,但是我们看代码,首先就能看到整体的脉络,然后再深入一条条脉络,写的方法,重用性很高,并且有很多代码技巧,eg,定义一个方法为loadBeanDefinitions,方法里面做完一些准备工作,再有doLoadBeanDefinitions去做真正的加载,而且很重要一点,所以的方法和变量命名都非常好,我们基本上可以从名字知道这个方法干什么,对底层封装的非常好。
会在下一篇文章继续记录第三步骤prepareBeanFactory(beanFactory);的解读。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容