@Aspect
@Component
public class LogAspect {
private static final Logger log = LoggerFactory.getLogger(LogAspect.class);
@Pointcut("execution(public * com.example.demo.controller..*.*(..))") //切点
public void webLog() {
}
/**
*
* @param joinPoint
*/
@Before("webLog()") //切点之前执行
public void doBefore(JoinPoint joinPoint) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
log.info("请求的地址" + String.valueOf(request.getRequestURL()));
log.info("请求的IP" + String.valueOf(request.getRemoteAddr()));
}
@AfterReturning(returning = "ret", pointcut = "webLog()") //切点后返回值
public void doAfterReturning(Object ret) {
log.info(ret.toString());
}
}