4-02.AspectJ-基于注解的讲解

MyAspect.java
package com.exp.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; @Component @Aspect public class MyAspect { //声明一个公共的切入点 @Pointcut("execution(* com.exp.service.UserServiceImpl.*(..))") public void myPointcut(){} @Before("myPointcut()") public void myBefore(JoinPoint jp){ System.out.println("1.前置通知..." + jp.getSignature().getName());//连接点方法名 } /** * 后置通知获取service方法执行后的返回值 * Object retValue:service方法执行的返回值,如果写了返回值,需要在xml中配置returning * @param jp */ // <aop:after-returning method="myAfterReturning" pointcut-ref="myPointcut" returning="retValue"/> @AfterReturning(pointcut = "myPointcut()",returning = "retValue") public void myAfterReturning(JoinPoint jp,Object retValue){ System.out.println("3.后置通知..." + jp.getSignature().getName()); System.out.println("返回值:" + retValue); } @Around("myPointcut()") public Object myAround(ProceedingJoinPoint pjp) throws Throwable { System.out.println("2.环绕通知...开启事务..." + pjp.getSignature().getName()); //放行 Object retObj = pjp.proceed(); System.out.println("4.环绕通知....提交事务..."); return retObj; } @AfterThrowing(pointcut = "myPointcut()",throwing = "e") public void myAfterThrowing(JoinPoint jp,Throwable e){ System.out.println("异常通知..." + jp.getSignature().getName() + "===" + e.getMessage() ); } @After("myPointcut()") public void myAfter(JoinPoint jp){ System.out.println("最终通知..." + jp.getSignature().getName()); } }
bean02.xml
<?xml version="1.0" encoding="UTF-8"?> <!--xmlns xml namespace:xml命名空间--> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p ="http://www.springframework.org/schema/p" xmlns:context ="http://www.springframework.org/schema/context" xmlns:aop ="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 配置UserService--> <bean id="userService" class="com.exp.service.UserServiceImpl"></bean> <!-- 配置切面对象--> <bean id="myAspect" class="com.exp.aspect.MyAspect"></bean> <!-- 配置 aop --> <aop:config> <!-- aop:指定切面--> <aop:aspect ref="myAspect"> <!--定义一个切入点--> <aop:pointcut id="myPointcut" expression="execution(* com.exp.service.UserServiceImpl.*(..))"/> <!-- 配置前置通知...--> <aop:before method="myBefore" pointcut-ref="myPointcut" /> <!-- 配置后置通知...--> <aop:after-returning method="myAfterReturning" pointcut-ref="myPointcut" returning="retValue"/> <!--配置环绕通知--> <aop:around method="myAround" pointcut-ref="myPointcut"></aop:around> <!-- 配置异常通知 throwing="e" 值,是方法的参数名 --> <aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointcut" throwing="e"/> <!--配置最终通知:不管有没有异常,最终通知都会执行--> <aop:after method="myAfter" pointcut-ref="myPointcut"></aop:after> </aop:aspect> </aop:config> </beans>

浙公网安备 33010602011771号