顯示具有 aspect namespace 標籤的文章。 顯示所有文章
顯示具有 aspect namespace 標籤的文章。 顯示所有文章

2017年12月24日 星期日

Java Spring - Spring AOP advice & AspectJ


  • 建立annotated aspects
    • AspectJ5的特性
    • 簡便的通過少量的annotation把任意class轉變為aspect
  • 定義aspect
    • 一場表演,觀眾很重要,但對演出功能本身而言,觀眾並不是必要。因此,在Performance上,觀眾是aspect,並將其應用到Performance
    • @Aspect
      public class Audience {
          @Before("execution(** concert.Performance.perform(..))") //perform()之前
          public void silenceCellPhones() {
              System.out.println("Silencing cell phones");
          }
      
          @Before("execution(** concert.Performance.perform(..))") //perform()之前
          public void takeSeats() {
              System.out.println("Taking seats");
          }
      
          @AfterReturning("execution(** concert.Performance.perform(..))") //perform()成功執行後
          public void applause() {
              System.out.println("CLAP CLAP CLAP!!!");
          }
      
          @AfterThrowing("execution(** concert.Performance.perform(..))") //表演失敗之後
      
          public void demandRefund() {
              System.out.println("Demanding a refund");
          }
      }