https://blog.csdn.net/u010096717/article/details/82221263
package com.leetcode.leetcode.aoptest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
* @description:
* @author: licm
* @create: 2021-08-17 15:59
**/
@Aspect
@Component
@Slf4j
public class TestAspect {
//com.kzj.kzj_rabbitmq.controller 包中所有的类的所有方法切面
//@Pointcut("execution(public * com.kzj.kzj_rabbitmq.controller.*.*(..))")
//只针对 MessageController 类切面
//@Pointcut("execution(public * com.kzj.kzj_rabbitmq.controller.MessageController.*(..))")
//统一切点,对com.kzj.kzj_rabbitmq.controller及其子包中所有的类的所有方法切面
@Pointcut("execution(public * com.leetcode.leetcode.aoptest..*.*(..))")
public void Pointcut() {
}
//前置通知
@Before("Pointcut()")
public void beforeMethod(JoinPoint joinPoint){
log.info("调用了前置通知");
}
//@After: 后置通知
@After("Pointcut()")
public void afterMethod(JoinPoint joinPoint){
log.info("调用了后置通知");
}
//@AfterRunning: 返回通知 rsult为返回内容
@AfterReturning(value="Pointcut()",returning="result")
public void afterReturningMethod(JoinPoint joinPoint,Object result){
log.info("调用了返回通知");
}
//@AfterThrowing: 异常通知
@AfterThrowing(value="Pointcut()",throwing="e")
public void afterReturningMethod(JoinPoint joinPoint, Exception e){
log.info("调用了异常通知");
}
//@Around:环绕通知
@Around("Pointcut()")
public Object Around(ProceedingJoinPoint pjp) throws Throwable {
log.info("around执行方法之前");
Object object = pjp.proceed();
log.info("around执行方法之后--返回值:" +object);
return object;
}
}
测试
package com.leetcode.leetcode.aoptest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @description:
* @author: licm
* @create: 2021-08-17 15:37
**/
@RestController
@Slf4j
public class AopControllerTest {
@GetMapping(value = "/send_message", produces = "text/json;charset=UTF-8")
public String send_message(String str) throws Exception {
log.info("执行了controller.send_message方法");
System.out.println(str);
return str;
}
@GetMapping(value = "/send_message1", produces = "text/json;charset=UTF-8")
public String send_message1(String str) throws Exception {
log.info("执行了controller.send_message方法");
System.out.println(str);
return str;
}
}
这里可以获得更多方法参数
//@Around:环绕通知
@Around("Pointcut()")
public Object Around(ProceedingJoinPoint joinPoint) throws Throwable {
log.info("around执行方法之前");
//获取方法签名
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//获取切入方法的对象
Method method = signature.getMethod();
// //获取方法上的Aop注解
// MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
// //获取注解上的值如 : @MyAnnotation(key = "'param id is ' + #id")
// String keyEl = annotation.key();
//将注解的值中的El表达式部分进行替换
//创建解析器
// SpelExpressionParser parser = new SpelExpressionParser();
// //获取表达式
// Expression expression = parser.parseExpression(keyEl);
//设置解析上下文(有哪些占位符,以及每种占位符的值)
EvaluationContext context = new StandardEvaluationContext();
//获取参数值
Object[] args = joinPoint.getArgs();
//获取运行时参数的名称
DefaultParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer();
String[] parameterNames = discoverer.getParameterNames(method);
for (int i = 0; i < parameterNames.length; i++) {
//这块是将参数名和参数注入,你可以考虑直接把他们放到map里
context.setVariable(parameterNames[i], args[i].toString());
}
//解析,获取替换后的结果
// String result = expression.getValue(context).toString();
// System.out.println(result);
return null;
}