跟我學spring源碼(一)

拉取源代碼

git clone https://github.com/goldGreatChen/spring-framework.git

其中g(shù)radle需要4版本
看它的import to idea文檔
Precompile spring-oxm with ./gradlew :spring-oxm:compileTestJava
對spring-oxm項目預先編譯 也可以使用gradle :spring-oxm:compileTestJava命令

Import into IntelliJ (File -> New -> Project from Existing Sources -> Navigate to directory -> Select build.gradle)
導入idea等待完成 很慢

When prompted exclude the spring-aspects module (or after the import via File-> Project Structure -> Modules)
排除spring-aspects項目 如下圖所示

1508828148(1).png

在其中新建自己的項目spring-demo

group 'org.springframework'
version '5.0.1.BUILD-SNAPSHOT'

apply plugin: 'java'
apply plugin: "groovy"

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile(project(":spring-context"))
    testCompile group: 'junit', name: 'junit', version: '4.12'
}
package com.chen;

import com.chen.service.ChenService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main(String[] args) {
        ApplicationContext context=new  ClassPathXmlApplicationContext("application.xml");
        ChenService chenService=context.getBean("chenService", ChenService.class);
        System.out.println(chenService.getInfo());
    }
}

成功運行 說明源代碼已經(jīng)編譯成功
ClassPathXmlApplicationContext("application.xml");
通過這句話看spring啟動過程

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

        super(parent);
        //configlocation 就是application.xml的路徑
        setConfigLocations(configLocations);
        if (refresh) {
            refresh();
        }
    }

看refresh

@Override
    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) {
                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();
            }
        }
    }

判斷是不是有beanFactory了 如果已經(jīng)有了 那么銷毀所有bean 關(guān)閉beanFactory 然后再獲取新的beanFactory

@Override
    protected final void refreshBeanFactory() throws BeansException {
        if (hasBeanFactory()) {
            destroyBeans();
            closeBeanFactory();
        }
        try {
            DefaultListableBeanFactory beanFactory = createBeanFactory();
            beanFactory.setSerializationId(getId());
            customizeBeanFactory(beanFactory);
            loadBeanDefinitions(beanFactory);
            synchronized (this.beanFactoryMonitor) {
                this.beanFactory = beanFactory;
            }
        }
        catch (IOException ex) {
            throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末享言,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子渗鬼,更是在濱河造成了極大的恐慌览露,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件譬胎,死亡現(xiàn)場離奇詭異差牛,居然都是意外死亡,警方通過查閱死者的電腦和手機堰乔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進店門偏化,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人镐侯,你說我怎么就攤上這事侦讨。” “怎么了?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵韵卤,是天一觀的道長骗污。 經(jīng)常有香客問我,道長怜俐,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任邓尤,我火速辦了婚禮拍鲤,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘汞扎。我一直安慰自己季稳,他們只是感情好,可當我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布澈魄。 她就那樣靜靜地躺著景鼠,像睡著了一般。 火紅的嫁衣襯著肌膚如雪痹扇。 梳的紋絲不亂的頭發(fā)上铛漓,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天,我揣著相機與錄音鲫构,去河邊找鬼浓恶。 笑死,一個胖子當著我的面吹牛结笨,可吹牛的內(nèi)容都是我干的包晰。 我是一名探鬼主播,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼炕吸,長吁一口氣:“原來是場噩夢啊……” “哼伐憾!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起赫模,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤树肃,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后瀑罗,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體扫外,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年廓脆,在試婚紗的時候發(fā)現(xiàn)自己被綠了筛谚。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡停忿,死狀恐怖驾讲,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤吮铭,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布时迫,位于F島的核電站,受9級特大地震影響谓晌,放射性物質(zhì)發(fā)生泄漏掠拳。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一纸肉、第九天 我趴在偏房一處隱蔽的房頂上張望溺欧。 院中可真熱鬧,春花似錦柏肪、人聲如沸姐刁。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽聂使。三九已至,卻和暖如春谬俄,著一層夾襖步出監(jiān)牢的瞬間柏靶,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工溃论, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留宿礁,地道東北人。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓蔬芥,卻偏偏與公主長得像梆靖,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子笔诵,可洞房花燭夜當晚...
    茶點故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內(nèi)容