AOP

来源:互联网 发布:大数据在教育行业应用 编辑:程序博客网 时间:2024/06/06 12:50

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@Componentpublic 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/12017-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@4faa7d882017-08-03 17:18:26.561 [http-nio-8080-exec-4] INFO  com.rich.springboot.config.AOP - end! spend time: 5 ms!
原创粉丝点击