<!--开启自动扫描-->
<content:component-scan base-package="com.aop"></content:component-scan>
<!--使Aspject注解起作用,自动为匹配的类生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
package com.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
//@Component
//@Aspect
public class LoginAspect {
//定义一个方法,声明切入点表达式,该方法里面不需要写代码
// @Pointcut(“execution(* com.aop..(int ,int))”)
// public void JoinPoint(){}
// 前置通知
@Before(“execution(* com.aop..(int ,int))”)
public void beforeMethod(JoinPoint joinPoint) {
String methodName=joinPoint.getSignature().getName();
List list= Arrays.asList(joinPoint.getArgs());
System.out.println(“beforeMethod。。。”+methodName+“with”+list);
}
// //后置通知,是否异常都会执行
// @After(“execution(* com.aop..(int ,int))”)
// public void beforeMethod(JoinPoint joinPoint) {
// String methodName=joinPoint.getSignature().getName();
// List list= Arrays.asList(joinPoint.getArgs());
// System.out.println(“beforeMethod。。。”+methodName+“ends”+list);
// }
// //返回通知,可以获得执行方法的结果
// @AfterReturning(value = “execution(* com.aop..(int ,int))”,returning = “result”)
// public void AfterReturning(JoinPoint joinPoint,Object result) {
// String methodName=joinPoint.getSignature().getName();
// List list= Arrays.asList(joinPoint.getArgs());
// System.out.println(“AfterReturning。。。”+methodName+“ends”+list+“返回值:”+result);
// }
// //异常通知
// @AfterThrowing(value = “execution(* com.aop..(int ,int))”,throwing = “ex”)
// public void AfterThrowing(JoinPoint joinPoint,Exception ex) {
// String methodName=joinPoint.getSignature().getName();
// List list= Arrays.asList(joinPoint.getArgs());
// System.out.println(“AfterReturning。。。”+methodName+“ends”+list+“异常:”+ex);
// }
//环绕通知,必须要有ProceedingJoinPoint类型参数,
//要有返回值,返回值结果就是方法结果
// @Around( “JoinPoint()”)
public Object Around(ProceedingJoinPoint joinPoint) {
Object result=null;
String methodName=joinPoint.getSignature().getName();
try {
//前置通知
System.out.println("前置通知:"+Arrays.asList(joinPoint.getArgs()));
//执行目标方法
result=joinPoint.proceed();
System.out.println("后置通知:"+result);
} catch (Throwable e) {
System.out.println("异常通知"+e);
throw new RuntimeException(e);
}
return result;
}
}