1. 程式人生 > >基於@Aspect的AOP配置

基於@Aspect的AOP配置

1、Spring除了支援Schema方式配置AOP,還支援註解方式:使用@Aspect來配置

2、Spring預設不支援@Aspect風格的切面宣告,通過如下配置開啟@Aspect支援:

<aop:aspectj-autoproxy/>  
3、通過以上配置,Spring就能發現用@Aspect註解的切面內並把它應用到目標物件上。

4、定義一個切面:
@Aspect  
public class AspectStyle {  
     @Before("execution(* com.sxit..*.*(..))")  
     public void before(){  
         System.out.println("方法執行前執行.....");  
     }  
 } 
 5、後置返回通知:

  @AfterReturning("execution(* com.sxit..*.*(..))")  
    public void afterReturning(){  
            System.out.println("方法執行完執行.....");  
    }  
 6、後置異常通知:
   
@AfterThrowing("execution(* com.sxit..*.*(..))")  
    public void throwss(){  
            System.out.println("方法異常時執行.....");  
    }  
 7、後置最終通知:

 @After("execution(* com.sxit..*.*(..))")  
    public void after(){  
            System.out.println("方法最後執行.....");  
    }  
 8、環繞通知:
   
 @Around("execution(* com.sxit..*.*(..))")  
    public Object around(ProceedingJoinPoint pjp){  
            System.out.println("方法環繞start.....");  
            try {  
                pjp.proceed();  
            } catch (Throwable e) {  
                e.printStackTrace();  
            }  
            System.out.println("方法環繞end.....");  
    }  
 9、按上面的每一個通知都要寫一個定義,其實這部分可以抽出來,定義個一個公共的切入點。

 package com.sxit;  
      
    import org.aspectj.lang.ProceedingJoinPoint;  
    import org.aspectj.lang.annotation.After;  
    import org.aspectj.lang.annotation.AfterReturning;  
    import org.aspectj.lang.annotation.AfterThrowing;  
    import org.aspectj.lang.annotation.Around;  
    import org.aspectj.lang.annotation.Aspect;  
    import org.aspectj.lang.annotation.Before;  
    import org.aspectj.lang.annotation.Pointcut;  
      
    @Aspect  
    public class AspectStyle {  
          
        @Pointcut("execution(* com.sxit..*.*(..))")  
        public void init(){  
              
        }  
      
        @Before(value="init()")  
        public void before(){  
            System.out.println("方法執行前執行.....");  
        }  
          
        @AfterReturning(value="init()")  
        public void afterReturning(){  
            System.out.println("方法執行完執行.....");  
        }  
          
        @AfterThrowing(value="init()")  
        public void throwss(){  
            System.out.println("方法異常時執行.....");  
        }  
          
        @After(value="init()")  
        public void after(){  
            System.out.println("方法最後執行.....");  
        }  
          
        @Around(value="init()")  
        public Object around(ProceedingJoinPoint pjp){  
            System.out.println("方法環繞start.....");  
            Object o = null;  
            try {  
                o = pjp.proceed();  
            } catch (Throwable e) {  
                e.printStackTrace();  
            }  
            System.out.println("方法環繞end.....");  
            return o;  
        }  
    }  
 10、列印資訊:

 方法before前執行.....  
    方法環繞start.....  
    我看.....................  
    方法after執行.....  
    方法環繞end.....  
    方法afterReurning執行.....  

參考自: