springAop详解
springAop
springAop采用的是动态代理模式,而动态代理模式的最底成就是反射。也就是通过反射调用的,不熟悉反射的可以去看一下反射api。在这里,动态代理模式就不讲了,以后有机会再写
springAop传入的参数
一直都知道springAop,但是springAop一般用于切入日志和事物管理,也可用用于自定义注解。以前一直没有怎么注意springAop的使用,在观看文章后有些心的如下。注意,该案例copy无法使用,需要自行更改。
package com.chenhao;
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;
//把这个类声明为切面,需要把类放到IOC容器中、再声明为一个切面
@Aspect
@Component
class LoggingAspect {
// 1.使用AOP环绕通知拦截所有访问
/**
定义切入点 拦截所有的com.chenhao.AtithmeticCalculator下的(int ,int )的方法
*/
@Pointcut("execution(public * com.chenhao.AtithmeticCalculator.*(int ,int ))")
public void rlAop() {
}
//该方法是一个前置通知,在目标方法开始之前执行
@Before("rlAop()")
public void beforeMethod(JoinPoint joinPoint) {
/** 自己定义注解时使用
MethodSignature signature = (MethodSignature) point.getSignature();
ExtApiToken extApiToken = signature.getMethod().getDeclaredAnnotation(ExtApiToken.class); //获取当前方法是否使用ExtApiToken注解
if (extApiToken != null) {
// 可以放入到AOP代码 前置通知 使用注解,执行下列方法
}
*/
//获取当前执行的方法名
String methodName = joinPoint.getSignature().getName();
//获取当前运行的对象
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("前置通知:The method" + methodName + " begins with " + args);
}
//后置通知:在目标方法执行后(无论是否发生异常),执行的通知
//在后置通知中还不能访问目标方法执行的结果
@After("rlAop()")
public void afterMethod(JoinPoint joinPoint) {
//获取当前执行的方法名
String methodName = joinPoint.getSignature().getName();
System.out.println("后置通知:The method " + methodName + " ends");
}
//返回通知:在方法正常结束后反回的通知,可以访问到方法的返回值
//没怎么注意项目中使用
@AfterReturning(value = "rlAop()"),
returning = "result")
public void afterReturning(JoinPoint joinPoint, Object result) {
String methodName = joinPoint.getSignature().getName();
System.out.println("返回通知:The method " + methodName + " ends with " + result);
}
/**
* 在目标方法出现异常时会执行的代码
* 可以访问到异常对象;且可以指定在出现特定异常时在执行通知代码
*
* @param joinPoint
* @param ex
*/
//异常通知 一般用于撤销事物,获取记录异常日志
@AfterThrowing(value = "rlAop()")
throwing = "ex")
public void afterThrowing(JoinPoint joinPoint, Exception ex) {
String methodName = joinPoint.getSignature().getName();
System.out.println("异常通知:The method " + methodName + " occurs exception: " + ex);
}
/**
* 环绕通知需要携带 ProceedingJoinPoint 类型的参数
* 环绕通知类似于动态代理的全过程:ProceedingJoinPoint 类型的参数可以决定是否执行目标方法
* 且环绕通知必须有返回值,返回值必须为目标方法的返回值
* 用于日志记录和事物管理,可以用于注解开发
*/
@Around("execution(value = "rlAop())")
public Object aroundMethod(ProceedingJoinPoint pjd) {
Object result = null;
String methodName = pjd.getSignature().getName();
//执行目标方法
try {
//前置通知
System.out.println("环绕通知(前置):The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs()));
result = pjd.proceed();//环绕通知获取proceed,执行拦截的方法 获取结果即可放行
//后置通知
System.out.println("环绕通知(后置)The method ends with "+result);
} catch (Throwable e) {
e.printStackTrace();
System.out.println("环绕通知(异常)The method ocuurs exception: " + e );
throw new RuntimeException(e);
}
System.out.println("环绕通知(后置) The method ends");
return result;
}
}