記錄一下spring中AspectJ靜態代碼織入實現aop

java中寫了一個接口以及它的實現類,稍後就攔截這些方法

 

  1. package myspring.calculator;  
  2.  
  3. public interface IArithmeticCalculator {  
  4.  
  5.     public double add(double a, double b);  
  6.     public double sub(double a, double b);  
  7.     public double mul(double a, double b);  
  8.     public double div(double a, double b);  
  9. }  
  1. package myspring.calculator;  
  2.  
  3.  
  4.  
  5. public class ArithmeticCalculatorImp implements IArithmeticCalculator {  
  6.       
  7.     public double add(double a, double b) {  
  8.         double result = a + b;  
  9.         System.out.println(a + " + " + b + " = " + result);  
  10.         return result;  
  11.     }  
  12.  
  13.     public double sub(double a, double b) {  
  14.         double result = a - b;  
  15.         System.out.println(a + " - " + b + " = " + result);  
  16.         return result;  
  17.     }  
  18.  
  19.     public double mul(double a, double b) {  
  20.         double result = a * b;  
  21.         System.out.println(a + " * " + b + " = " + result);  
  22.         return result;  
  23.     }  
  24.  
  25.     public double div(double a, double b) {  
  26.         if (b == 0) {  
  27.             throw new IllegalArgumentException("Division by zero");  
  28.         }  
  29.         double result = a / b;  
  30.         System.out.println(a + " / " + b + " = " + result);  
  31.         return result;  
  32.     }  
  33.       
  34.       
  35. }  

aop.xml如下,先用ioc將接口與實現類建立關係

 

  1. <beans xmlns="http://www.springframework.org/schema/beans" 
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  3.     xmlns:aop="http://www.springframework.org/schema/aop" 
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  5.         http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  6.         http://www.springframework.org/schema/aop  
  7.         http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  8.         "> 
  9.           
  10.     <aop:aspectj-autoproxy /> 
  11.       
  12.     <bean id="arithmeticCalculator" class="myspring.calculator.ArithmeticCalculatorImp"></bean> 
  13.  
  14.     <bean class="myspring.aop.CalculatorLogAspect" /> 
  15.   
  16. </beans> 

在上面可以看到這句話:<bean class="myspring.aop.CalculatorLogAspect" /> ,這表示當調用arithmeticCalculator這個bean裏面的方法的時候,要按照CalculatorLogAspect裏的攔截配置進行攔截。

myspring.aop.CalculatorLogAspect如下:

  1. package myspring.aop;  
  2.  
  3. import java.util.Arrays;  
  4.  
  5. import org.aspectj.lang.JoinPoint;  
  6. import org.aspectj.lang.ProceedingJoinPoint;  
  7. import org.aspectj.lang.annotation.After;  
  8. import org.aspectj.lang.annotation.AfterReturning;  
  9. import org.aspectj.lang.annotation.AfterThrowing;  
  10. import org.aspectj.lang.annotation.Around;  
  11. import org.aspectj.lang.annotation.Aspect;  
  12. import org.aspectj.lang.annotation.Before;  
  13. import org.aspectj.lang.annotation.Pointcut;  
  14.  
  15. @Aspect 
  16. public class CalculatorLogAspect {  
  17.     private MyLogger logger = new MyLogger();//隨便寫的一個記錄日誌的類,只是向控制檯打印信息  
  18.  
  19.     @Pointcut("execution(* *.add(..))")  
  20.     public void logAdd(){}  
  21.  
  22.     @Before("logAdd()")//表示按照logAdd方法上標示的攔截表達式來攔截add方法  
  23.     public void logBefore(JoinPoint joinPoint)  
  24.     {  
  25.         logger.log("{Before} The method: "+  
  26.                         joinPoint.getSignature().getName()+  
  27.                         "        args:"+Arrays.toString(joinPoint.getArgs()));  
  28.     }  
  29.       
  30.     @AfterReturning(  
  31.             pointcut="execution(* *.*(..))", //表示所有方法return之後都要經過此攔截
  32.             returning="result")  
  33.     public void logAfterReturning(JoinPoint joinPoint, Object result)  
  34.     {  
  35.         logger.log("{AfterReturning} method:"+joinPoint.getSignature().getName()  
  36.                         + "     result:"+result);  
  37.     }  
  38.       
  39.     @AfterThrowing(  
  40.             pointcut="execution(* *.*(..))", //表示所有方法異常之後都要經過此攔截 
  41.             throwing="e")  
  42.     public void logAfterThrowing(JoinPoint joinPoint, IllegalArgumentException e)  
  43.     {  
  44.         logger.log("{AfterThrowing} method:"+  
  45.                         joinPoint.getSignature().getName()+  
  46.                         "     Exception:"+e.getMessage()  
  47.                         );  
  48.     }  
  49.       
  50.     @After("execution(* *.*(..))")  //表示所有方法執行完畢之後都要經過此攔截
  51.     public void logAfter(JoinPoint joinPoint)  
  52.     {  
  53.         logger.log("{After} method:"+joinPoint.getSignature().getName());  
  54.     }  
  55.       
  56.     @Around("execution(* *.*(..))")  //表示所有方法運行之前與之後都要經過此攔截
  57.     public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable  
  58.     {  
  59.         logger.log("{Around}Before Method:"+joinPoint.getSignature().getName());  
  60.           
  61.         try 
  62.         {  
  63.             //調用目標方法  
  64.             Object result = joinPoint.proceed();  
  65.               
  66.             logger.log("{Around}After Method:"+  
  67.                                 joinPoint.getSignature().getName()+  
  68.                                 "       result:"+result);  
  69.               
  70.             //返回目標方法的返回值  
  71.             return result;  
  72.         }  
  73.         catch(IllegalArgumentException e)  
  74.         {  
  75.             logger.log("{Around}Exception:Illegal Argument "+  
  76.                         Arrays.toString(joinPoint.getArgs()) );  
  77.               
  78.             throw e;  
  79.         }  
  80.     }  
  81.       
  82.       
  83.       
  84.       
  85. }  

logger類如下:

  1. package myspring.aop;  
  2.  
  3. public class MyLogger {  
  4.  
  5.     public void log(String info)  
  6.     {  
  7.         System.out.println("[Logger] "+info);  
  8.     }  
  9. }  

 編寫測試類進行測試:

 

  1. package myspring.test;  
  2.  
  3. import myspring.calculator.IArithmeticCalculator;  
  4. import myspring.calculator.IMaxCalculator;  
  5. import myspring.calculator.IMinCalculator;  
  6.  
  7. import org.junit.Test;  
  8. import org.springframework.context.ApplicationContext;  
  9. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  10.  
  11. public class TestAOP {  
  12.  
  13.     //AspectJ方式  
  14.     @Test 
  15.     public void test1()  
  16.     {  
  17.         ApplicationContext ac = new ClassPathXmlApplicationContext("aop.xml");  
  18.         IArithmeticCalculator cal = (IArithmeticCalculator)ac.getBean("arithmeticCalculator");  
  19.               
  20.         cal.add(23);  
  21.         cal.sub(53);  
  22.         cal.mul(34);  
  23.         cal.div(60);  
  24.     }   
  25.     }  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章