1. 程式人生 > >【轉載】Spring AOP

【轉載】Spring AOP

6.3.7. 例子

讓我們來看看在 Section 6.2.7, “例子” 提過併發鎖失敗重試的例子,如果使用schema對這個例子進行重寫是什麼效果。

因為併發鎖的關係,有時候business services可能會失敗(例如,死鎖失敗)。 如果重新嘗試一下,很有可能就會成功。對於business services來說,重試幾次是很正常的(Idempotent操作不需要使用者參與,否則會得出矛盾的結論) 我們可能需要透明的重試操作以避免讓客戶看見PessimisticLockingFailureException 例外被丟擲。 很明顯,在一個橫切多層的情況下,這是非常有必要的,因此通過切面來實現是很理想的。

因為我們想要重試操作,我們會需要使用到環繞通知,這樣我們就可以多次呼叫proceed()方法。 下面是簡單的切面實現(只是一個schema支援的普通Java 類):

public class ConcurrentOperationExecutor implements Ordered {
   
   private static final int DEFAULT_MAX_RETRIES = 2;

   private int maxRetries = DEFAULT_MAX_RETRIES;
   private int order = 1;

   public void setMaxRetries(int maxRetries) {
      this.maxRetries = maxRetries;
   }
   
   public int getOrder() {
      return this.order;
   }
   
   public void setOrder(int order) {
      this.order = order;
   }
   
   public Object doConcurrentOperation(ProceedingJoinPoint pjp) throws Throwable { 
      int numAttempts = 0;
      PessimisticLockingFailureException lockFailureException;
      do {
         numAttempts++;
         try { 
            return pjp.proceed();
         }
         catch(PessimisticLockingFailureException ex) {
            lockFailureException = ex;
         }
      }
      while(numAttempts <= this.maxRetries);
      throw lockFailureException;
   }

}

請注意切面實現了 Ordered 介面,這樣我們就可以把切面的優先順序設定為高於事務通知(我們每次重試的時候都想要在一個全新的事務中進行)。 maxRetries 和 order 屬性都可以在Spring中配置。 主要的動作在 doConcurrentOperation 這個環繞通知中發生。 請注意這個時候我們所有的 businessService() 方法都會使用這個重試策略。 我們首先會嘗試處理,然後如果我們得到一個PessimisticLockingFailureException 異常,我們只需要簡單的重試,直到我們耗盡所有預設的重試次數。

這個類跟我們在@AspectJ的例子中使用的是相同的,只是沒有使用註解。

對應的Spring配置如下:

<aop:config>

  <aop:aspect id="concurrentOperationRetry" ref="concurrentOperationExecutor">

    <aop:pointcut id="idempotentOperation"
        expression="execution(* com.xyz.myapp.service.*.*(..))"/>
       
    <aop:around
       pointcut-ref="idempotentOperation"
       method="doConcurrentOperation"/>
  
  </aop:aspect>

</aop:config>

<bean id="concurrentOperationExecutor"
  class="com.xyz.myapp.service.impl.ConcurrentOperationExecutor">
     <property name="maxRetries" value="3"/>
     <property name="order" value="100"/>  
</bean>

請注意我們現在假設所有的bussiness services都是idempotent。如果不是這樣,我們可以改寫切面,加上 Idempotent 註解,讓它只調用idempotent:

@Retention(RetentionPolicy.RUNTIME)
public @interface Idempotent {
  // marker annotation
}

並且對service操作的實現進行註解。這樣如果你只希望改變切面使得idempotent的操作會嘗試多次,你只需要改寫切入點表示式,這樣只有@Idempotent 操作會匹配:

  <aop:pointcut id="idempotentOperation"
		expression="execution(* com.xyz.myapp.service.*.*(..)) and
					@annotation(com.xyz.myapp.service.Idempotent)"/>

相關推薦

轉載Spring AOP詳解 、 JDK動態代理、CGLib動態代理

rto 工廠 第一個 lec 僅支持 sel clas sleep gpo 原文地址:https://www.cnblogs.com/kukudelaomao/p/5897893.html AOP是Aspect Oriented Programing的簡稱,面向切面

轉載Spring AOP

6.3.7. 例子 讓我們來看看在 Section 6.2.7, “例子” 提過併發鎖失敗重試的例子,如果使用schema對這個例子進行重寫是什麼效果。 因為併發鎖的關係,有時候business services可能會失敗(例如,死鎖失敗)。 如果重新嘗試一下,很有可能就會

轉載spring-boot 專案跳轉到JSP頁面

原路徑:https://blog.csdn.net/qq_36820717/article/details/80008225 1、新建spring-boot專案  目錄結構如下 2、新建TestController.java檔案,內容如下 package com.example.contr

轉載spring boot 連結 虛擬機器(Linux) redis

原文:https://www.imooc.com/article/43279?block_id=tuijian_wz 前提是你已經安裝redis且支援遠端連線,redis的安裝這裡不再贅述,有需要的可以參考我的另一篇文章:centos 7.3上安裝redis。這裡主要講講如何判斷及設定redis支援遠端連線

轉載SpringSpring MVC包掃描

轉自:http://www.cnblogs.com/junzi2099/p/8042476.html 在Spring整體框架的核心概念中,容器是核心思想,就是用來管理Bean的整個生命週期的,而在一個專案中,容器不一定只有一個,Spring中可以包括多個容器,而且容器有上下層關係,目前最常見的一

轉載Spring MVC @Autowired註入問題

這就是我 參數 int .net rec except 方法註入 null https 背景在IDEA升級2017版後,發現以前使用的 @Autowired 出現了個警告 Field injection is not recommended。 @Autowired的不推薦用

轉載spring-session負載均衡原理分析

註明轉載:https://www.jianshu.com/p/beaf18704c3c 第一部分:我會用循序漸進的方式來展示原始碼,從大家最熟悉的地方入手,而不是直接從系統啟動來debug原始碼。直接debug原始碼看到後來大家都會一頭霧水。 本文先從request.getSession()開始

轉載Spring @Async 原始碼解讀。

正文 1.引子 開啟非同步任務使用方法: 1).方法上加@Async註解 2).啟動類或者配置類上@EnableAsync 2.原始碼解析 雖然spring5已經出來了,但是我們還是使用的spring4,本文就根據spring-context-4.3.14.RELEASE.jar來分析原

框架[Spring]AOP攔截-三種方式實現自動代理

這裡的自動代理,我講的是自動代理bean物件,其實就是在xml中讓我們不用配置代理工廠,也就是不用配置class為org.springframework.aop.framework.ProxyFactoryBean的bean。 總結了一下自己目前所學的知識

知識庫--spring aop 動態代理--inner private protected 方法失效(212)

私有方法或者保護的方法無效! AspectJ pointcut for annotated PRIVATE methods Due to the proxy-based nature of Spring's AOP framework, protected methods

原創Spring-AOP代理類繼承介面..

Computer.java  package org.rockie; public class Computer implements PcInterf{ private String pcName="rockie007"; private int pcPrice=5000;

轉載開發者眼中的Spring與Java EE

客戶端 實現 意義 代理 produces 有著 hiberna arm 激情   轉載自:http://www.infoq.com/cn/news/2015/07/spring-javaee 在Java社區中,Spring與Java EE之爭是個永恒的話題。在這場爭論中,

Java Spring 框架初步學習總結(一)簡單實現 IoC 和 AOP

1.0 其中 表示 只需要 第一篇 否則 info fin pojo   Spring 是一個開源的設計層面的輕量級框架,Spring 的好處網上有太多,這裏就不在贅述。   IoC 控制反轉和 AOP 面向切面編程是 Spring 的兩個重要特性。   IoC(Inver

學習筆記Spring AOP註解使用總結

trace -a tid nat 修改 with this throwable pid Spring AOP基本概念 是一種動態編譯期增強性AOP的實現 與IOC進行整合,不是全面的切面框架 與動態代理相輔相成 有兩種實現:基於jdk動態代理、cglib Spring

小家SpringSpring AOP的多種使用方式以及神一樣的AspectJ-AOP使用介紹

相關閱讀 【小家java】java5新特性(簡述十大新特性) 重要一躍 【小家java】java6新特性(簡述十大新特性) 雞肋升級 【小家java】java7新特性(簡述八大新特性) 不溫不火 【小家java】java8新特性(簡述十大新特性) 飽受讚譽 【小家java】java9

springSpring aop的實現原理

本文轉載自https://www.cnblogs.com/lcngu/p/5339555.html。 Spring aop的實現原理 簡介   前段時間寫的java設計模式--代理模式,最近在看Spring Aop的時候,覺得於代理模式應該有密切的聯絡,於是決定了解下Sprin

小家javaSpring AOP的多種使用方式以及神一樣的AspectJ-AOP使用介紹

相關閱讀 什麼時候AOP AOP(Aspect-OrientedProgramming,面向方面程式設計),可以說是OOP(Object-Oriented Programing,面向物件程式設計)的補充和完善。 AOP技它利用一種稱為“橫切”的技術,剖解開封

原創Spring-Cloud快速入門(一)微服務入門--轉載請註明出處

一、什麼是微服務? 有時候,會有的人存在誤解,所謂微服務就是SpringCloud。這種思想本身是不正確的,微服務是一種系統架構上面的設計風格,而SpringCloud則是一種較為適用於微服務架構的框架。 在java體系中,我們通常需要將一個大的類,拆分成若干個的小的類,每個類都具有自己獨立

原創Spring-boot快速入門(二)JPA資料來源--轉載請註明出處

Spring-boot快速入門(二)JPA資料來源 宣告:本篇部落格一切程式碼基於 Spring-boot快速入門(一)進行。 一、JPA介紹 Spring Data JPA,是一款直接整合了hibernate的資料庫資源訪問的Spring Data下的子專案,通過JPA對資料庫進

原創Spring-boot快速入門(一)HelloWord!--轉載請註明出處

Spring-boot快速入門(一)HelloWord! 一、Spring-boot簡介 1. Spring-boot介紹 Spring-boot是一款將Spring4.X版本Spring族群進行整合的一款框架,繼承了來自於Spring族群的絕大部分功能,在Spring4.