转载自:http://blog.csdn.net/yl_wh/article/details/8473510

[java]  view plain copy
  1. package com.spring.aop.test;  
  2.   
  3. public class My {  
  4.     public String testAop() throws Exception {  
  5.         System.out.println("test aop");  
  6.           
  7.         int i = 10;  
  8.         if (i > 1) {  
  9.             //throw new Exception("i > 1");  
  10.         }  
  11.         return "aop is ok";   
  12.     }  
  13. }  
  14.   
  15. package com.spring.aop.test;  
  16.   
  17. import org.aspectj.lang.JoinPoint;  
  18. import org.aspectj.lang.ProceedingJoinPoint;  
  19. import org.aspectj.lang.annotation.After;  
  20. import org.aspectj.lang.annotation.AfterReturning;  
  21. import org.aspectj.lang.annotation.AfterThrowing;  
  22. import org.aspectj.lang.annotation.Around;  
  23. import org.aspectj.lang.annotation.Aspect;  
  24. import org.aspectj.lang.annotation.Before;  
  25. import org.springframework.context.ApplicationContext;  
  26. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  27.   
  28. @Aspect  
  29. public class MyAspect {  
  30.       
  31.     /** 
  32.      * 调用前通知 
  33.      */  
  34.     @Before(value="execution(* com.spring.aop.test.*.*(..))")  
  35.     public void before(JoinPoint jp){   
  36.         System.out.println("aspect - befoer " + jp.toString());   
  37.     }   
  38.   
  39.     /** 
  40.      * 抛出异常后通知 
  41.      */  
  42.     @AfterThrowing(value="execution(* com.spring.aop.test.*.*(..))", throwing="ex")  
  43.     public void afterThrowing(Exception ex) {  
  44.         System.out.println("aspect - after throwing " + ex.getMessage());  
  45.     }  
  46.       
  47.     /** 
  48.      * 环绕通知 
  49.      */  
  50.     @Around(value="execution(* com.spring.aop.test.*.*(..))")  
  51.     public Object around(ProceedingJoinPoint pjp) throws Throwable {  
  52.           
  53.         System.out.println("aspect - around before");  
  54.         Object result = pjp.proceed();  
  55.         System.out.println("aspect - around after");  
  56.           
  57.         return result;  
  58.     }  
  59.       
  60.     /** 
  61.      * 调用后通知 
  62.      */  
  63.     @After(value="execution(* com.spring.aop.test.*.*(..))")  
  64.     public void after(JoinPoint jp) {  
  65.         System.out.println("aspect - after " + jp.toString());   
  66.     }  
  67.       
  68.     /** 
  69.      * 调用返回后通知 
  70.      */  
  71.     @AfterReturning(value="execution(* com.spring.aop.test.*.*(..))")  
  72.     public void afterReturn(JoinPoint jp) {  
  73.         System.out.println("aspect - after returning " + jp.toShortString());  
  74.     }  
  75.       
  76.     public static void main(String[] args) {   
  77.         ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});   
  78.         My my = (My)context.getBean("my");  
  79.         try {  
  80.             String s = my.testAop();   
  81.             System.out.println(s);   
  82.         } catch (Exception e) {  
  83.             System.out.println("最外层捕获异常:" + e.getMessage());  
  84.         }  
  85.     }   
  86.       
  87.       
  88. }  

 
 <bean id="my" class="com.spring.aop.test.My"></bean> 
<bean id="myAspect" class="com.spring.aop.test.MyAspect"></bean>
 
<!-- 使用CGLIB代理和@AspectJ自动代理支持--> 
<aop:aspectj-autoproxy proxy-target-class="true"/>