spring源碼解讀--context

前言 : 關於本次涉及代碼https://github.com/yunzhi98/springcontext.git

一,調試環境搭建:

1,maven3.4+jdk8
2,爲了方便每一步的調試,所以直接用了main讀取xml來調試進程。

 public static void main(String[] args) {
        AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext("crawlwab\\src\\main\\webapp\\application.xml");

        UserService userService = (UserService)applicationContext.getBean("userService");
        UserMapper userMapper = userService.getUserByName("xuyun");
        System.out.println("-----------userService="+userMapper.getPassWord());
    }

具體的xml配置,及測試用的bean代碼,可以下載源碼查看。

3,對於context抽象實現ClassPathXmlApplicationContext 先查看下他的繼承關係,及每個繼承抽象所實現的功能。
在這裏插入圖片描述
可以看到,
3.1 ClassPathXmlApplicationContext 是對xml解讀來創建容器環境的,那我們具體去看下他提供了哪些方法。

private Resource[] configResources;

    public ClassPathXmlApplicationContext() {
    }

    public ClassPathXmlApplicationContext(ApplicationContext parent) {
        super(parent);
    }

    public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
        this(new String[]{configLocation}, true, (ApplicationContext)null);
    }

    public ClassPathXmlApplicationContext(String... configLocations) throws BeansException {
        this(configLocations, true, (ApplicationContext)null);
    }

    public ClassPathXmlApplicationContext(String[] configLocations, @Nullable ApplicationContext parent) throws BeansException {
        this(configLocations, true, parent);
    }

    public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh) throws BeansException {
        this(configLocations, refresh, (ApplicationContext)null);
    }

    public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, @Nullable ApplicationContext parent) throws BeansException {
        super(parent);
        this.setConfigLocations(configLocations);
        if (refresh) {
            this.refresh();
        }

    }

    public ClassPathXmlApplicationContext(String path, Class<?> clazz) throws BeansException {
        this(new String[]{path}, clazz);
    }

    public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz) throws BeansException {
        this(paths, clazz, (ApplicationContext)null);
    }

可以看到,都是創建的構造方法以及提供了一個對於容器內容的get方法。

   protected Resource[] getConfigResources() {
        return this.configResources;
    }

我們用的是ClassPathXmlApplicationContext(String configLocation)方法做的測試,所以就看下這個構造方法的實現情況。

3.2 具體代碼

     public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, @Nullable ApplicationContext parent) throws BeansException {
        super(parent);
        this.setConfigLocations(configLocations);
        if (refresh) {
            this.refresh();
        }

    }

3.21 可以看到是先執行了個父類的 super(parent) 方法,傳進去的是ApplicationContext對象,因爲我們是在創建,這個是null,但每個父類創建都會執行這個方法,所以這是個有趣的東西,他有兩個作用,一是對現有的進行創建 二是對已創建的進行刷新。 然後我們又想到了單例,因爲容器對象在邊界範圍內只有一個的,這個這裏就不展開了,繼續往下看。

3.22 this.setConfigLocations(configLocations)方法。這裏是我們最開始的xml路徑設置進去的地址,可以想到是對xml 配置文件的解讀及內容加載。
裏面最主要的方法是 public ConfigurableEnvironment getEnvironment() 可以看到是對xml中節點的的解讀及加載,斷點進去看看我們測試獲取到的內容。
this.configLocations = “crawlwab\src\main\webapp\application.xml”;

3.23 this.refresh();上面的結束了後,直接執行刷新操作。

 public void refresh() throws BeansException, IllegalStateException {
        synchronized(this.startupShutdownMonitor) {
            this.prepareRefresh();
            ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
            this.prepareBeanFactory(beanFactory);

            try {
                this.postProcessBeanFactory(beanFactory);
                this.invokeBeanFactoryPostProcessors(beanFactory);
                this.registerBeanPostProcessors(beanFactory);
                this.initMessageSource();
                this.initApplicationEventMulticaster();
                this.onRefresh();
                this.registerListeners();
                this.finishBeanFactoryInitialization(beanFactory);
                this.finishRefresh();
            } catch (BeansException var9) {
                if (this.logger.isWarnEnabled()) {
                    this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var9);
                }

                this.destroyBeans();
                this.cancelRefresh(var9);
                throw var9;
            } finally {
                this.resetCommonCaches();
            }

        }
    }

3.231 可以看到裏面有個synchronized鎖,功能就是在創建的時候防止重複生成。這裏也因該有個問題,就是同時創建的時候,一個人鎖住了,其他人都異常了,那二次創建的時候會發生什麼,被覆蓋嗎,從現有是這樣的,但之後的衍生問題看怎麼解決。
3.232 synchronized(this.startupShutdownMonitor) 裏面鎖傳的class對象是
private final Object startupShutdownMonitor;
這是個final對象,很巧妙啊。
3.233 protected void prepareRefresh() 方法。
在 this.prepareRefresh() 方法裏面
1,有個 this.startupDate = System.currentTimeMillis();獲取時間毫秒數的。,
2, this.closed.set(false); 這個是在AbstractApplicationContext設置了個AtomicBoolean closed 對象的false,可以想到這個是容器裏的設置,是啓用還是關閉的設置。
3,this.active.set(true); 這個是把private final AtomicBoolean active; 設置爲true,同樣的那麼這個就是啓動的一個標識了。
4,this.logger.isDebugEnabled() 這是對於日誌的一個設置,會在日誌裏記錄當前執行class的一個信息。
5, this.earlyApplicationListeners = new LinkedHashSet(this.applicationListeners)
這裏是一個監聽的設置。

3.24, ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
這裏是獲取配置的bean工廠的。
3.241 對bean工廠類ConfigurableListableBeanFactory看下,裏面的參數信息。

public interface ConfigurableListableBeanFactory extends ListableBeanFactory, AutowireCapableBeanFactory, ConfigurableBeanFactory {
 void ignoreDependencyType(Class<?> var1);

 void ignoreDependencyInterface(Class<?> var1);

 void registerResolvableDependency(Class<?> var1, @Nullable Object var2);

 boolean isAutowireCandidate(String var1, DependencyDescriptor var2) throws NoSuchBeanDefinitionException;

 BeanDefinition getBeanDefinition(String var1) throws NoSuchBeanDefinitionException;

 Iterator<String> getBeanNamesIterator();

 void clearMetadataCache();

 void freezeConfiguration();

 boolean isConfigurationFrozen();

 void preInstantiateSingletons() throws BeansException;
}

可以看到繼承了三個接口,我們分別看下三個接口有哪些抽象功能。
1,ListableBeanFactory 獲取bean工廠的獲取方法提供。

     public interface ListableBeanFactory extends BeanFactory {
    boolean containsBeanDefinition(String var1);

    int getBeanDefinitionCount();

    String[] getBeanDefinitionNames();

    String[] getBeanNamesForType(ResolvableType var1);

    String[] getBeanNamesForType(@Nullable Class<?> var1);

    String[] getBeanNamesForType(@Nullable Class<?> var1, boolean var2, boolean var3);

    <T> Map<String, T> getBeansOfType(@Nullable Class<T> var1) throws BeansException;

    <T> Map<String, T> getBeansOfType(@Nullable Class<T> var1, boolean var2, boolean var3) throws BeansException;

    String[] getBeanNamesForAnnotation(Class<? extends Annotation> var1);

    Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> var1) throws BeansException;

    @Nullable
    <A extends Annotation> A findAnnotationOnBean(String var1, Class<A> var2) throws NoSuchBeanDefinitionException;
}

}

2,AutowireCapableBeanFactory 提供bean的寫入和操作抽象。
3,ConfigurableBeanFactory 提供bean創建的配置方法抽象。

其中一個protected abstract void refreshBeanFactory()抽象方法被AbstractRefreshableApplicationContext類實現,返回DefaultListableBeanFactory對象。這有個問題,抽象類被兩個實現,怎麼指定裝配這個呢?

  
    @Nullable
    protected BeanFactory getInternalParentBeanFactory() {
        return (BeanFactory)(this.getParent() instanceof ConfigurableApplicationContext ? ((ConfigurableApplicationContext)this.getParent()).getBeanFactory() : this.getParent());
    }

這裏的一個三目運算判定this.getParent() instanceof ConfigurableApplicationContext 用這種是否繼承的寫法。
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory)方法,bean對象賦值AbstractRefreshableApplicationContext。

3.42 來看下被創建的AbstractRefreshableApplicationContext對象有哪些內容。

整個繼承關係:
在這裏插入圖片描述

歡迎使用Markdown編輯器

你好! 這是你第一次使用 Markdown編輯器 所展示的歡迎頁。如果你想學習如何使用Markdown編輯器, 可以仔細閱讀這篇文章,瞭解一下Markdown的基本語法知識。

新的改變

我們對Markdown編輯器進行了一些功能拓展與語法支持,除了標準的Markdown編輯器功能,我們增加了如下幾點新功能,幫助你用它寫博客:

  1. 全新的界面設計 ,將會帶來全新的寫作體驗;
  2. 在創作中心設置你喜愛的代碼高亮樣式,Markdown 將代碼片顯示選擇的高亮樣式 進行展示;
  3. 增加了 圖片拖拽 功能,你可以將本地的圖片直接拖拽到編輯區域直接展示;
  4. 全新的 KaTeX數學公式 語法;
  5. 增加了支持甘特圖的mermaid語法1 功能;
  6. 增加了 多屏幕編輯 Markdown文章功能;
  7. 增加了 焦點寫作模式、預覽模式、簡潔寫作模式、左右區域同步滾輪設置 等功能,功能按鈕位於編輯區域與預覽區域中間;
  8. 增加了 檢查列表 功能。

功能快捷鍵

撤銷:Ctrl/Command + Z
重做:Ctrl/Command + Y
加粗:Ctrl/Command + B
斜體:Ctrl/Command + I
標題:Ctrl/Command + Shift + H
無序列表:Ctrl/Command + Shift + U
有序列表:Ctrl/Command + Shift + O
檢查列表:Ctrl/Command + Shift + C
插入代碼:Ctrl/Command + Shift + K
插入鏈接:Ctrl/Command + Shift + L
插入圖片:Ctrl/Command + Shift + G
查找:Ctrl/Command + F
替換:Ctrl/Command + G

合理的創建標題,有助於目錄的生成

直接輸入1次#,並按下space後,將生成1級標題。
輸入2次#,並按下space後,將生成2級標題。
以此類推,我們支持6級標題。有助於使用TOC語法後生成一個完美的目錄。

如何改變文本的樣式

強調文本 強調文本

加粗文本 加粗文本

標記文本

刪除文本

引用文本

H2O is是液體。

210 運算結果是 1024.

插入鏈接與圖片

鏈接: link.

圖片: Alt

帶尺寸的圖片: Alt

居中的圖片: Alt

居中並且帶尺寸的圖片: Alt

當然,我們爲了讓用戶更加便捷,我們增加了圖片拖拽功能。

如何插入一段漂亮的代碼片

博客設置頁面,選擇一款你喜歡的代碼片高亮樣式,下面展示同樣高亮的 代碼片.

// An highlighted block
var foo = 'bar';

生成一個適合你的列表

  • 項目
    • 項目
      • 項目
  1. 項目1
  2. 項目2
  3. 項目3
  • 計劃任務
  • 完成任務

創建一個表格

一個簡單的表格是這麼創建的:

項目 Value
電腦 $1600
手機 $12
導管 $1

設定內容居中、居左、居右

使用:---------:居中
使用:----------居左
使用----------:居右

第一列 第二列 第三列
第一列文本居中 第二列文本居右 第三列文本居左

SmartyPants

SmartyPants將ASCII標點字符轉換爲“智能”印刷標點HTML實體。例如:

TYPE ASCII HTML
Single backticks 'Isn't this fun?' ‘Isn’t this fun?’
Quotes "Isn't this fun?" “Isn’t this fun?”
Dashes -- is en-dash, --- is em-dash – is en-dash, — is em-dash

創建一個自定義列表

Markdown
Text-to-HTML conversion tool
Authors
John
Luke

如何創建一個註腳

一個具有註腳的文本。2

註釋也是必不可少的

Markdown將文本轉換爲 HTML

KaTeX數學公式

您可以使用渲染LaTeX數學表達式 KaTeX:

Gamma公式展示 Γ(n)=(n1)!nN\Gamma(n) = (n-1)!\quad\forall n\in\mathbb N 是通過歐拉積分

Γ(z)=0tz1etdt. \Gamma(z) = \int_0^\infty t^{z-1}e^{-t}dt\,.

你可以找到更多關於的信息 LaTeX 數學表達式here.

新的甘特圖功能,豐富你的文章

Mon 06Mon 13Mon 20已完成 進行中 計劃一 計劃二 現有任務Adding GANTT diagram functionality to mermaid
  • 關於 甘特圖 語法,參考 這兒,

UML 圖表

可以使用UML圖表進行渲染。 Mermaid. 例如下面產生的一個序列圖:

張三李四王五你好!李四, 最近怎麼樣?你最近怎麼樣,王五?我很好,謝謝!我很好,謝謝!李四想了很長時間,文字太長了不適合放在一行.打量着王五...很好... 王五, 你怎麼樣?張三李四王五

這將產生一個流程圖。:

鏈接
長方形
圓角長方形
菱形
  • 關於 Mermaid 語法,參考 這兒,

FLowchart流程圖

我們依舊會支持flowchart的流程圖:

Created with Raphaël 2.2.0開始我的操作確認?結束yesno
  • 關於 Flowchart流程圖 語法,參考 這兒.

導出與導入

導出

如果你想嘗試使用此編輯器, 你可以在此篇文章任意編輯。當你完成了一篇文章的寫作, 在上方工具欄找到 文章導出 ,生成一個.md文件或者.html文件進行本地保存。

導入

如果你想加載一篇你寫過的.md文件,在上方工具欄可以選擇導入功能進行對應擴展名的文件導入,
繼續你的創作。


  1. mermaid語法說明 ↩︎

  2. 註腳的解釋 ↩︎

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章