AOP

1.添加Maven依赖

        <!-- aop -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.8.8</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.8</version>
        </dependency>
        <!-- aop -->

2.AOP配置

package com.rich.springboot.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
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 javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @author rich.liuchude
 * @since 2017-08-03 17:09
 */
@Aspect
@Component
public class AOP {
    private static final Logger LOG = LoggerFactory.getLogger(AOP.class);

    /**
     * 使用空方法定义切点表达式
     */
    @Pointcut("execution(* com.rich.springboot.controller.**.*(..))")
    public void controller() {
    }

    /**
     * 环绕通知
     *
     * @param joinPoint 切入点
     * @return
     */
    @Around("controller()")
    public Object aroundController(ProceedingJoinPoint joinPoint) {
        Object result = null;
        String url;
        String user;
        String address;
        String httpMethod;
        String classMethod;
        String args;
        long startTime = 0L;
        long endTime;
        try {
            startTime = System.currentTimeMillis();
            // 接收到请求,记录请求内容
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            // 请求内容
            url = request.getRequestURL().toString();
            user = request.getRemoteUser();
            address = request.getRemoteAddr();
            httpMethod = request.getMethod();
            classMethod = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
            args = Arrays.toString(joinPoint.getArgs());
            LOG.info("start! class method: " + classMethod + ", method: " + httpMethod + ", user: " + user + ", address: " + address + ", url: " + url);
            LOG.info("request: " + args);
            // 有返回参数 则需返回值
            result = joinPoint.proceed();
            LOG.info("response: " + result);
        } catch (Throwable t) {
            LOG.error("error", t);
        } finally {
            endTime = System.currentTimeMillis();
            LOG.info("end! spend time: " + (endTime - startTime) + " ms!");
        }
        return result;
    }
}

3.请求Controller,查看日志信息

2017-08-03 17:18:17.983 [http-nio-8080-exec-1] INFO  o.a.c.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-08-03 17:18:26.557 [http-nio-8080-exec-4] INFO  com.rich.springboot.config.AOP - start! class method: com.rich.springboot.controller.WelcomeRestController.query, method: POST, user: null, address: 127.0.0.1, url: http://127.0.0.1:8080/query/1
2017-08-03 17:18:26.557 [http-nio-8080-exec-4] INFO  com.rich.springboot.config.AOP - request: [1, com.rich.springboot.domain.JsonResult@4faa7d88]
2017-08-03 17:18:26.560 [http-nio-8080-exec-4] INFO  com.rich.springboot.config.AOP - response: com.rich.springboot.domain.JsonResult@4faa7d88
2017-08-03 17:18:26.561 [http-nio-8080-exec-4] INFO  com.rich.springboot.config.AOP - end! spend time: 5 ms!
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值