Spring Boot配置Aop环绕、切点
package com.demo.mkt.aop;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.yuhan.mkt.util.ResultUtil;
/**
*
* @ClassName:HttpAspect
* @Description AOP切面类,记录方法的调用,入参以及出参
* @author Zhang Wei
* @date 2018年8月23日上午10:49:09
*/
@Slf4j
@Aspect
@Component
public class HttpAspect {
private final static Logger LOGGER = LoggerFactory.getLogger(HttpAspect.class);
/**
* 指定切点: 匹配com.yuhan.mkt.controller包及其子包下的所有类的所有方法
*/
@Pointcut("execution(* com.yuhan.mkt.controller.*.*(..))")
public void log() {
}
/**
* 前置通知,方法调用前被调用
*
* @param joinPoint
*/
@Before("log()")
public void doBefore(JoinPoint joinPoint) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// url
LOGGER.info("url={}", request.getRequestURL());
// method
LOGGER.info("method={}", request.getMethod());
// ip
LOGGER.info("ip={}", request.getRemoteAddr());
// class_method
LOGGER.info("class_method={}",
joinPoint.getSignature().getDeclaringTypeName() + "," + joinPoint.getSignature().getName());
// args[]
LOGGER.info("args={}", joinPoint.getArgs());
// Signature
LOGGER.info("Signature={}", joinPoint.getSignature());
}
/**
* 环绕通知
*
* @param pjp
* @return
*/
@Around("log()")
public Object doAround(ProceedingJoinPoint pjp) {
try {
Object proceed = pjp.proceed();
return proceed;
} catch (Throwable e) {
LOGGER.error("运行异常,e={}", e);
return ResultUtil.error(e.getLocalizedMessage(), 500);
}
}
}
pom.xml配置依赖
<!--aop的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
网友评论