<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    stone2083

    Spring Data JPA 代碼分析

    背景
    接上文:Spring Data JPA 簡單介紹
    本文將從配置解析,Bean的創(chuàng)建,Repository執(zhí)行三個(gè)方面來簡單介紹下Spring Data JPA的代碼實(shí)現(xiàn)

    友情提醒:
    圖片均可放大

    配置解析
    1. parser類
    Spring通過Schema的方式進(jìn)行配置,通過AbstractRepositoryConfigDefinitionParser進(jìn)行解析。其中包含對(duì)NamedQuery的解析。
    解析的主要目的,是將配置文件中的repositories和repository元素信息分別解析成GlobalRepositoryConfigInformation和SingleRepositoryConfigInformation。
    詳見下圖
    2. Information

    CommonRepositoryConfigInformation:
    xml中repositories的通用配置,一般對(duì)應(yīng)其中的attributes
    SingleRepositoryConfigInformation:
    xml中repository的配置信息,對(duì)應(yīng)其中的attributes
    GlobalRepositoryCOnfigInformation:
    一組SingleRepositoryConfigInfomation信息,包含所有的Single信息
    在JPA實(shí)現(xiàn)中,針對(duì)Single,有兩份實(shí)現(xiàn),一份是自動(dòng)配置信息,一份是手動(dòng)配置信息,分別對(duì)應(yīng)圖中的Automatic和Manual。
    SimpleJpaRepositoryConfiguration是JPA中的所有配置信息,包含所有的Jpa中的SingleRepositoryConfigInformation。
    3. Query Lookup Strategy
    CreateQueryLookupStrategy:對(duì)應(yīng)repositories元素
    query-lookup-strategy的create值,主要針對(duì)method query方式
    DeclaredQueryLookupStrategy:對(duì)應(yīng)use-declared-query值,主要針對(duì)帶有@Query注解的查詢方式
    CreateIfNotFoundQueryLookupStrategy:對(duì)應(yīng)create-if-not-found值(default值),結(jié)合了上述兩種方式


    Bean的創(chuàng)建

    主要包含兩個(gè)類
    RepositoryFactoryBeanSupport, Spring Factory Bean,用于創(chuàng)建Reposiory代理類。其本身并不真正做代理的事情,只是接受Spring的配置,具體交由RepositoryFactorySupport進(jìn)行代理工作
    RepositoryFactorySupport, 真正做Repository代理工作,根據(jù)JpaRepositoryFactoryBean的定義找到TargetClass:SimpleJpaRepository實(shí)現(xiàn)類,中間加入3個(gè)攔截器,一個(gè)是異常翻譯,一個(gè)是事務(wù)管理,最后一個(gè)是QueryExecutorMethodInterceptor。
    QueryExecutorMethodInterceptor是個(gè)重點(diǎn),主要做特定的Query(查詢語句)的操作。

    Repository執(zhí)行
    1. 主要執(zhí)行類
    在看上面Bean定義的時(shí)候,其實(shí)已經(jīng)明白了執(zhí)行過程:
    1. 將JPA CRUD規(guī)范相關(guān)的方法交給SimpleJpaRepository這個(gè)類執(zhí)行
    2. 將特殊查詢相關(guān)的交給QueryExecutorMethodInterceptor執(zhí)行。主要做自定義實(shí)現(xiàn)的部分,method query部分和named query部分。
    具體查詢類詳見下圖。

    2. 查詢相關(guān)
    主要支持NamedQuery和JPA Query。


    主要執(zhí)行代碼
    QueryExecutorMethodInterceptor#invoke(MethodInvocation invocation)
     1 public Object invoke(MethodInvocation invocation) throws Throwable {
     2 
     3             Method method = invocation.getMethod();
     4 
     5             if (isCustomMethodInvocation(invocation)) {
     6                 Method actualMethod = repositoryInformation.getTargetClassMethod(method);
     7                 makeAccessible(actualMethod);
     8                 return executeMethodOn(customImplementation, actualMethod,
     9                         invocation.getArguments());
    10             }
    11 
    12             if (hasQueryFor(method)) {
    13                 return queries.get(method).execute(invocation.getArguments());
    14             }
    15 
    16             // Lookup actual method as it might be redeclared in the interface
    17             // and we have to use the repository instance nevertheless
    18             Method actualMethod = repositoryInformation.getTargetClassMethod(method);
    19             return executeMethodOn(target, actualMethod,
    20                     invocation.getArguments());
    21         }

    主要分3個(gè)步驟:
    1. 如果配置文件中執(zhí)行了接口類的實(shí)現(xiàn)類,則直接交給實(shí)現(xiàn)類處理
    2. 判斷是查詢方法的,交給RepositoryQuery實(shí)現(xiàn),具體又分:NamedQuery,SimpleJpaQuery,PartTreeJpaQuery
    3. 不屬于上述兩個(gè),則直接將其交給真正的targetClass執(zhí)行,在JPA中,就交給SimpleJpaRepository執(zhí)行。

    本文并沒有做詳細(xì)的分析,只是將核心的組件類一一點(diǎn)到,方便大家自行深入了解代碼。

    posted on 2011-08-25 16:28 stone2083 閱讀(5138) 評(píng)論(1)  編輯  收藏 所屬分類: java

    Feedback

    # re: Spring Data JPA 代碼分析 2015-07-02 17:40 zuidaima

    springdata demo教程源代碼下載 :http://zuidaima.com/share/kspringdata-p1-s1.htm  回復(fù)  更多評(píng)論   

    主站蜘蛛池模板: 猫咪www免费人成网站| 91亚洲性爱在线视频| 成人毛片免费视频| 香蕉高清免费永久在线视频| 超pen个人视频国产免费观看| 亚洲一区在线免费观看| 老子影院午夜伦不卡亚洲| 免费一级特黄特色大片| 免费国产黄网站在线观看可以下载| 成人午夜免费福利视频| 亚洲?v女人的天堂在线观看| 久久精品视频亚洲| 亚洲色大成网站www永久男同| 国产精品亚洲专区无码不卡| 久久久久国产免费| vvvv99日韩精品亚洲| 在线播放国产不卡免费视频| 亚洲国产精品免费观看| 亚洲中文字幕无码一区二区三区 | 国产成人综合久久精品亚洲| 日韩精品亚洲专区在线观看| 无码AV动漫精品一区二区免费 | 亚洲人成网站在线观看播放动漫| 无人在线直播免费观看| 亚洲日产乱码一二三区别| 免费观看男人吊女人视频| 日韩亚洲一区二区三区| 国产AV无码专区亚洲AV蜜芽 | 日本免费人成在线网站| 亚洲综合精品香蕉久久网| 免费A级毛片无码A∨中文字幕下载| 亚洲国产精品综合久久久| 久9这里精品免费视频| 亚洲最大免费视频网| 国产成人精品免费直播 | 日产乱码一卡二卡三免费| 亚洲视频一区在线| 99精品免费视品| 国产jizzjizz免费视频| 亚洲欧美国产国产综合一区| 亚洲福利精品电影在线观看|