Spring AOP的增强处理

就是@Before @Around @AfterReturning @AfterThrowing这几个标签的属性可以放到方法参数里面获取

例子

//正常操作
@Around("service()")
public void doAround(JoinPoint joinPoint) throws Throwable {
// 接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 记录下请求内容
System.out.println("URL : " + request.getRequestURL().toString());
System.out.println("HTTP_METHOD : " + request.getMethod());
System.out.println("IP : " + request.getRemoteAddr());
System.out.println("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
System.out.println("ARGS : " + Arrays.toString(joinPoint.getArgs()));
}


//获取annotation
@Around("@annotation(log)") public Object around(ProceedingJoinPoint pjp, Log log) { //获取注解里的值 System.out.println("annotation around:" + log.description()); try { return pjp.proceed(); } catch (Throwable throwable) { throwable.printStackTrace(); return null; } }


//获取异常
@AfterThrowing(pointcut = "within(com.lgp.aop..*) && @annotation(log)", throwing = "ex")
public void addAfterThrowingLogger(JoinPoint joinPoint, Log log, Exception ex) {
logger.error("执行 " + log.description() + " 异常,error={}", ex.getMessage(), ex);
}
 

注意,参数名字应该一致。

猜你喜欢

转载自www.cnblogs.com/ydymz/p/9542064.html
今日推荐