一、前置通知
1 import org.aspectj.lang.JoinPoint; 2 import org.aspectj.lang.ProceedingJoinPoint; 3 import org.aspectj.lang.annotation.After; 4 import org.aspectj.lang.annotation.AfterReturning; 5 import org.aspectj.lang.annotation.AfterThrowing; 6 import org.aspectj.lang.annotation.Around; 7 import org.aspectj.lang.annotation.Aspect; 8 import org.aspectj.lang.annotation.Before; 9 import org.aspectj.lang.annotation.Pointcut; 10 11 @Aspect //表示当前类为切面 12 public class MyAspect { 13 @Before("execution(* *..ISomeService.doFirst(..))") 14 public void myBefore(){ 15 System.out.println("执行前置通知方法"); 16 } 17 18 @Before("execution(* *..ISomeService.doFirst(..))") 19 public void myBefore(JoinPoint jp){ 20 System.out.println("执行前置通知方法 jp="+jp); 21 } 22 23 @AfterReturning("execution(* *..ISomeService.doSecond(..))") 24 public void myAfterReturning(){ 25 System.out.println("执行后置通知方法"); 26 27 } 28 29 @AfterReturning(value="execution(* *..ISomeService.doSecond(..))",returning="result") 30 public void myAfterReturning(Object result){ 31 System.out.println("执行后置通知方法 result="+result); 32 33 } 34 35 @Around("execution(* *..ISomeService.doSecond(..))") 36 public Object myAround(ProceedingJoinPoint pjp) throws Throwable{ 37 System.out.println("执行环绕通知方法,目标方法执行之前"); 38 //执行目标方法 39 Object result = pjp.proceed(); 40 System.out.println("执行环绕通知方法,目标方法执行之后"); 41 if(result !=null){ 42 result=((String)result).toUpperCase(); 43 } 44 return result; 45 } 46 47 @AfterThrowing(value="execution(* *..ISomeService.doThird(..))",throwing="ex") 48 public void myAfterThrowing(Exception ex){ 49 System.out.println("执行异常通知方法ex="+ex.getMessage()); 50 } 51 52 @After("doThirdPointcut()") 53 public void myAfter(){ 54 System.out.println("执行最终通知方法"); 55 } 56 //定义了一个切入点,叫doThirdPointcut() 57 @Pointcut("execution(* *..ISomeService.doThird(..))") 58 public void doThirdPointcut(){} 59 }
<!-- 注册切面 --> <bean id="myAspect" class="com.bjpowernode.xml.MyAspect"></bean> <!-- 注册目标对象 --> <bean id="someService" class="com.bjpowernode.xml.SomeServiceImpl"></bean> <!-- AOP配置 --> <aop:config> <aop:pointcut expression="execution(* *..ISomeService.doFirst(..))" id="doFirstPointcut"/> <aop:pointcut expression="execution(* *..ISomeService.doSecond(..))" id="doSecond1Pointcut"/> <aop:pointcut expression="execution(* *..ISomeService.doThird(..))" id="doThirdPointcut"/> <aop:aspect ref="myAspect"> <aop:before method="myBefore" pointcut-ref="doFirstPointcut"/> <aop:before method="myBefore(org.aspectj.lang.JoinPoint)" pointcut-ref="doFirstPointcut"/> </aop:aspect> </aop:config>
二、后置通知
1 <aop:after-returning method="myAfterReturning" pointcut-ref="doSecondPointcut"/> 2 <aop:after-returning method="myAfterReturning(java.lang.Object)" pointcut-ref="doSecondPointcut" returning="result" />
三、环绕通知
<aop:around method="myAround" pointcut-ref="doSecondPointcut"/>
四、异常通知
<aop:after-throwing method="myAfterThrowing(java.lang.Exception)" pointcut-ref="doThirdPointcut" throwing="ex"/>
五、最终通知
<aop:after method="myAfter" pointcut-ref="doThirdPointcut"/>