回覆列表
  • 1 # CoreCode

    具體方法如下,自行觀看。

    1 示例仍然使用上一節的"騎士和劍士"的例子,目標類Horseman和Swordman[java] view plain copy print?public class Horseman { public void rush(String enemy) { System.out.println(this.getClass().getSimpleName() + "衝刺攻擊" + enemy); } public void chop(String enemy) { System.out.println(this.getClass().getSimpleName() + "砍劈攻擊" + enemy); } } [java] view plain copy print?public class Swordman { public void block(String enemy) { System.out.println(this.getClass().getSimpleName() + "格擋" + enemy); } public void chop(String enemy) { System.out.println(this.getClass().getSimpleName() + "砍劈攻擊" + enemy); } } 2 註解說明2.1 @Aspect作用是把當前類標識為一個切面供容器讀取2.2 @Before標識一個前置增強方法,相當於BeforeAdvice的功能,相似功能的還有2.3 @AfterReturning後置增強,相當於AfterReturningAdvice,方法正常退出時執行2.4 @AfterThrowing異常丟擲增強,相當於ThrowsAdvice2.5 @Afterfinal增強,不管是丟擲異常或者正常退出都會執行2.6 @Around環繞增強,相當於MethodInterceptor2.7 @DeclareParents引介增強,相當於IntroductionInterceptor3 execution切點函式execution函式用於匹配方法執行的連線點,語法為:execution(方法修飾符(可選) 返回型別 方法名 引數 異常模式(可選))

  • 中秋節和大豐收的關聯?
  • 有哪些可愛的手賬本?