Spring Boot:AOP统一处理HTTP请求

版权声明:博主原创/资料整理,转载请注明出处!!

首先,AOP (Aspect Oriented Programming )指面向切面编程,通过预编译方式或者运行时刻对目标对象动态地添加功能。

一、Spring Boot中添加AOP依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

二、Spring Boot中AOP处理HTTP请求

  1. 项目下新建aspect包,aspect包下新建AspectTest类。AspectTest类上添加注解@Aspect和@Component。@Aspect让Spring容器知道这是一个AOP类。@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注,并把这些类纳入进spring容器中管理。
@Aspect
@Component
public class AspectTest {
}
  1. 使用静态Logger方法打印日志:声明Logger方法,
    private static Logger logger = LoggerFactory.getLogger(AspectTest.class);

使用ALT+ENTER快捷键,自动导入Logger包和LoggerFactory包,如下;

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
  1. 我们希望在HTTP请求到具体的controller之前记录下URL(请求路径)、method(请求方式)、ip(IP地址)、class_method(请求的类方法)和args(请求的参数)。
  2. 使用注解@Pointcut,表示为AOP的切入点,指定要切入的位置(比如某个controller);使用@Before(“Jointpoint jointpoint”)做一些HTTP请求到具体的controller之前的操作,这里JointPoint(连接点)表示程序执行过程中明确的点,一般是方法的调用;使用@After做一些HTTP请求到具体的controller之后的操作;使用@AfterReturning来获取HTTP请求的结果。

三、附上代码,仅供参考。

package com.quanweitech.firststudy.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
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;

@Aspect
@Component
public class AspectTest {
    private static Logger logger = LoggerFactory.getLogger(AspectTest.class);

    @Pointcut("execution(public * com.quanweitech.firststudy.controller.DepartmentInfoController.*(..))")
    public void AOPTest() {
    }

    @Before("AOPTest()")
    public void doBefore(JoinPoint joinPoint) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        //日志打印:url
        logger.info("url={}", request.getRequestURL()); //StringBuffer

        //日志打印:method
        logger.info("method={}", request.getMethod());

        //日志打印:ip
        logger.info("ip={}", request.getRemoteAddr());

        //日志打印:类方法
        logger.info("class_method={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());

        //日志打印:参数
        logger.info("args={}", joinPoint.getArgs());

    }

    @After("AOPTest()")
    public void doAfter() {
        logger.info("AOPTest() is ENDING!!!");
    }

    @AfterReturning(returning = "object", pointcut = "AOPTest()")
    public void doAfterReturning(Object object) {
        logger.info("response={}", object.toString());
    }

}

测试:使用GET请求方式查询id=18的数据,执行结果如下:

2018-12-13 14:20:39  INFO  [http-nio-8087-exec-2] Caller+0	 at com.quanweitech.firststudy.aspect.AspectTest.doBefore(AspectTest.java:33)
 - url=http://localhost:8087/springboot/dept/get/dept_info/18
2018-12-13 14:20:39  INFO  [http-nio-8087-exec-2] Caller+0	 at com.quanweitech.firststudy.aspect.AspectTest.doBefore(AspectTest.java:36)
 - method=GET
2018-12-13 14:20:39  INFO  [http-nio-8087-exec-2] Caller+0	 at com.quanweitech.firststudy.aspect.AspectTest.doBefore(AspectTest.java:39)
 - ip=0:0:0:0:0:0:0:1
2018-12-13 14:20:39  INFO  [http-nio-8087-exec-2] Caller+0	 at com.quanweitech.firststudy.aspect.AspectTest.doBefore(AspectTest.java:42)
 - class_method=com.quanweitech.firststudy.controller.DepartmentInfoController.departmentInfoFindOne
2018-12-13 14:20:39  INFO  [http-nio-8087-exec-2] Caller+0	 at com.quanweitech.firststudy.aspect.AspectTest.doBefore(AspectTest.java:45)
 - args=18
Hibernate: select department0_.id as id1_0_0_, department0_.create_time as create_t2_0_0_, department0_.update_time as update_t3_0_0_, department0_.business_id as business4_0_0_, department0_.delete_flag as delete_f5_0_0_, department0_.dept_code as dept_cod6_0_0_, department0_.dept_manager_list as dept_man7_0_0_, department0_.dept_name as dept_nam8_0_0_, department0_.ding_dept_id as ding_dep9_0_0_, department0_.parent_id as parent_10_0_0_, deptempinf1_.dept_id as dept_id4_1_1_, deptempinf1_.id as id1_1_1_, deptempinf1_.id as id1_1_2_, deptempinf1_.create_time as create_t2_1_2_, deptempinf1_.update_time as update_t3_1_2_, deptempinf1_.dept_id as dept_id4_1_2_, deptempinf1_.emp_id as emp_id5_1_2_ from department_info department0_ left outer join dept_emp_info deptempinf1_ on department0_.id=deptempinf1_.dept_id where department0_.id=?
2018-12-13 14:20:39  INFO  [http-nio-8087-exec-2] Caller+0	 at com.quanweitech.firststudy.aspect.AspectTest.doAfter(AspectTest.java:51)
 - AOPTest() is ENDING!!!
2018-12-13 14:20:39  INFO  [http-nio-8087-exec-2] Caller+0	 at com.quanweitech.firststudy.aspect.AspectTest.doAfterReturning(AspectTest.java:56)
 - response=DepartmentInfo(dingDeptId=921, deptName=达摩院, deptCode=0921, parentId=921, deptManagerList=0921, businessId=921, deleteFlag=2, deptEmpInfoList=[DeptEmpInfo(empId=2, deptId=18), DeptEmpInfo(empId=7, deptId=18)])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值