<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的創建,Repository執行三個方面來簡單介紹下Spring Data JPA的代碼實現

    友情提醒:
    圖片均可放大

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

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


    Bean的創建

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

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

    2. 查詢相關
    主要支持NamedQuery和JPA Query。


    主要執行代碼
    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個步驟:
    1. 如果配置文件中執行了接口類的實現類,則直接交給實現類處理
    2. 判斷是查詢方法的,交給RepositoryQuery實現,具體又分:NamedQuery,SimpleJpaQuery,PartTreeJpaQuery
    3. 不屬于上述兩個,則直接將其交給真正的targetClass執行,在JPA中,就交給SimpleJpaRepository執行。

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

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

    Feedback

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

    springdata demo教程源代碼下載 :http://zuidaima.com/share/kspringdata-p1-s1.htm  回復  更多評論   

    主站蜘蛛池模板: 亚洲一日韩欧美中文字幕在线| 亚洲综合国产精品| 亚洲精品成a人在线观看☆| 3344免费播放观看视频| 婷婷精品国产亚洲AV麻豆不片| 在线看片免费人成视频久网下载| 91麻豆精品国产自产在线观看亚洲| 免费精品视频在线| 亚洲Aⅴ无码一区二区二三区软件| 国产亚洲精品美女久久久久久下载| 国产青草视频免费观看97| 暖暖免费中文在线日本| 亚洲av无码乱码在线观看野外| 一区二区视频在线免费观看| 中文字幕第一页亚洲| 四虎影视无码永久免费| 久久久久亚洲AV成人无码网站| 99精品国产成人a∨免费看| 亚洲伊人精品综合在合线| 无码专区永久免费AV网站| 亚洲日韩中文字幕一区| 又大又黄又粗又爽的免费视频| 一级毛片正片免费视频手机看| 亚洲人成图片小说网站| 无码精品人妻一区二区三区免费看| 99久久亚洲综合精品成人网| 成人免费看吃奶视频网站| 看成年女人免费午夜视频| 亚洲成A人片在线观看无码不卡| 在线观看免费中文视频| 亚洲精品人成网线在线播放va| 亚洲福利在线播放| 午夜免费福利小电影| 中文字幕乱码亚洲精品一区| 亚洲精品成人区在线观看| 最近中文字幕mv免费高清在线| 色窝窝亚洲av网| 婷婷精品国产亚洲AV麻豆不片 | 妞干网免费视频观看| 一级女人18片毛片免费视频| 久久久久亚洲AV无码麻豆|