1.四种常用的通知类型
<aop:config>
<aop:aspect id="logAdvice" ref="logger">
<aop:before method="beforePrintLog" pointcut="execution(void com.itheima.service.impl.*.*(..))"></aop:before>
<aop:after-returning method="afterReturningPrintLog" pointcut="execution(void com.itheima.service.impl.*.*(..))"></aop:after-returning>
<aop:after-throwing method="afterThrowingPrintLog" pointcut="execution(void com.itheima.service.impl.*.*(..))"></aop:after-throwing>
<aop:after method="afterPrintLog" pointcut="execution(void com.itheima.service.impl.*.*(..))"></aop:after>
</aop:aspect>
</aop:config>
2.环绕通知
<aop:config>
<aop:pointcut id="pt1" expression="execution(* com.itheima.service.impl.*.*(..))"></aop:pointcut>
<aop:aspect id="logAdvice" ref="logger">
<aop:around method="aroundPrintLog" pointcut-ref="pt1"></aop:around>
</aop:aspect>
</aop:config>
package com.itheima.utils;
import org.aspectj.lang.ProceedingJoinPoint;
public class Logger {
public Object aroundPrintLog(ProceedingJoinPoint pjp){
Object rtValue=null;
try{
Object[] args = pjp.getArgs();
System.out.println("Logger类中的aroundPrintLog方法开始记录日志了。。。前置通知");
rtValue=pjp.proceed(args);
System.out.println("Logger类中的aroundPrintLog方法开始记录日志了。。。后置通知");
return rtValue;
}catch (Throwable t){
System.out.println("Logger类中的aroundPrintLog方法开始记录日志了。。。异常通知");
throw new RuntimeException(t);
}finally {
System.out.println("Logger类中的aroundPrintLog方法开始记录日志了。。。最终通知");
}
}
}