SpringBoot2.0 使用AOP统一处理Web请求日志(完整版)
一,加入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
二,在src/main/java下的某个包中新建类
:
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
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;
/**
* @Auther: cookie
* @Date: 2018/7/27 10:17
* @Description: 使用AOP统一处理Web请求日志
*/
@Aspect
@Component
public class WebControllerAop {
/**
* 指定切点
* 匹配 com.example.demo.controller包及其子包下的所有类的所有方法
*/
@Pointcut("execution(public * com.example.demo.controller.*.*(..))")
public void webLog(){
}
/**
* 前置通知,方法调用前被调用
* @param joinPoint
*/
@Before("webLog()")
public void doBefore(JoinPoint joinPoint){
System.out.println("我是前置通知!!!");
//获取目标方法的参数信息
Object[] obj = joinPoint.getArgs();
Signature signature = joinPoint.getSignature();
//代理的是哪一个方法
System.out.println("方法:"+signature.getName());
//AOP代理类的名字
System.out.println("方法所在包:"+signature.getDeclaringTypeName());
//AOP代理类的类(class)信息
signature.getDeclaringType();
MethodSignature methodSignature = (MethodSignature) signature;
String[] strings = methodSignature.getParameterNames();
System.out.println("参数名:"+Arrays.toString(strings));
System.out.println("参数值ARGS : " + Arrays.toString(joinPoint.getArgs()));
// 接收到请求,记录请求内容
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest req = attributes.getRequest();
// 记录下请求内容
System.out.println("请求URL : " + req.getRequestURL().toString());
System.out.println("HTTP_METHOD : " + req.getMethod());
System.out.println("IP : " + req.getRemoteAddr());
System.out.println("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
}
/**
* 处理完请求返回内容
* @param ret
* @throws Throwable
*/
@AfterReturning(returning = "ret", pointcut = "webLog()")
public void doAfterReturning(Object ret) throws Throwable {
// 处理完请求,返回内容
System.out.println("方法的返回值 : " + ret);
}
/**
* 后置异常通知
* @param jp
*/
@AfterThrowing("webLog()")
public void throwss(JoinPoint jp){
System.out.println("方法异常时执行.....");
}
/**
* 后置最终通知,final增强,不管是抛出异常或者正常退出都会执行
* @param jp
*/
@After("webLog()")
public void after(JoinPoint jp){
}
/**
* 环绕通知,环绕增强,相当于MethodInterceptor
* @param pjp
* @return
*/
@Around("webLog()")
public Object arround(ProceedingJoinPoint pjp) {
try {
Object o = pjp.proceed();
return o;
} catch (Throwable e) {
e.printStackTrace();
return null;
}
}
}
测试
在controller中随便做的测试:
@GetMapping("/user/{id}")
public User getUserById(@PathVariable String id){
User user = userService.selectByPrimaryKey(id);
return user;
}
访问地址:http://localhost:8080/user/1
控制台打印结果
单击查看大图:
SpringBoot2.0 使用AOP统一处理Web请求日志(完整版)的更多相关文章
- Springboot中使用AOP统一处理Web请求日志
title: Springboot中使用AOP统一处理Web请求日志 date: 2017-04-26 16:30:48 tags: ['Spring Boot','AOP'] categories: ...
- 46. Spring Boot中使用AOP统一处理Web请求日志
在之前一系列的文章中都是提供了全部的代码,在之后的文章中就提供核心的代码进行讲解.有什么问题大家可以给我留言或者加我QQ,进行咨询. AOP为Aspect Oriented Programming的缩 ...
- Spring Boot中使用AOP统一处理Web请求日志
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是Spring框架中的一个重要内容,它通 ...
- (转)Spring Boot中使用AOP统一处理Web请求日志
AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是Spring框架中的一个重要内容,它通 ...
- 转:Spring Boot中使用AOP统一处理Web请求日志
在spring boot中,简单几步,使用spring AOP实现一个拦截器: 1.引入依赖: <dependency> <groupId>org.springframewor ...
- spring Boot使用AOP统一处理Web请求日志记录
1.使用spring boot实现一个拦截器 1.引入依赖: <dependency> <groupId>org.springframework.boot</grou ...
- springboot Aop 统一处理Web请求日志
1.增加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- AOP统一处理Web请求日志
<!--aop--> <dependency> <groupId>org.springframework.boot</groupId> <arti ...
- spring boot使用AOP统一处理web请求
为了保证服务的高可用,及时发现问题,迅速解决问题,为应用添加log是必不可少的. 但是随着项目的增大,方法增多,每个方法加单独加日志处理会有很多冗余 那在SpringBoot项目中如何统一的处理Web ...
随机推荐
- Lambada. 计算和
Lambada. 计算和 import java.util.Arrays; import java.util.List; public class ListLambada { public stati ...
- Duplicate a whole line in Vim
yy or Y to copy the line or dd to delete (cutting) the line then p to paste the copied or deleted te ...
- phpstorm配置Xdebug进行调试PHP教程_php技巧_脚本之家
运行环境: PHPSTORM版本 : 8.0.1 PHP版本 : 5.6.2 xdebug版本:php_xdebug-2.2.5-5.6-vc11-x86_64.dll ps : php版本和xdeb ...
- 为什么要用Spring的依赖注入
最近写了一些Spring项目,用到了依赖注入,但是不知道为甚么要用,后来在知乎上看了一些大家的回答,觉得很精辟,遂简单总结一下. 主要是实现类之间的解耦,假如A类依赖B类,在实例化A类的话也要new一 ...
- 大数据ETL详解
ETL是BI项目最重要的一个环节,通常情况下ETL会花掉整个项目的1/3的时间,ETL设计的好坏直接关接到BI项目的成败.ETL也是一个长期的过程,只有不断的发现问题并解决问题,才能使ETL运行效率更 ...
- Spring_总结
spring配置Bean 配置形式 基于XML文件的方式 属性注入 构造注入 泛型依赖注入 基于注解的方式 配置方式 全类名(反射) 通过工厂方法 FactoryBean 字面值 <![CDAT ...
- ServletConfig详解 (转载)
ServletConfig详解 (转载) 容器初始化一个servlet时,会为这个servlet建一个唯一的ServletConfig.容器从DD读出Servlet初始化参数,并把这些参数交给S ...
- bzoj1191 超级英雄
Description 现在电视台有一种节目叫做超级英雄,大概的流程就是每位选手到台上回答主持人的几个问题,然后根据回答问题的多少获得不同数目的奖品或奖金.主持人问题准备了若干道题目,只有当选手正确回 ...
- 《第一行代码》之——1.Android简介
Android简介 Android系统架构 (图片源自维基百科) Android大致分为四层架构,五块区域. Linux内核层 Android系统基于Linux2.6,这一层为Android设备的各种 ...
- CNN对位移、尺度和旋转不变性的讨论
CNN得益于全局共享权值和pool操作,具有平移不变性. 对于尺度不变性,是没有或者说具有一定的不变性(尺度变化不大),实验中小目标的检测是难点,需要采用FPN或者其他的方式单独处理. 对于旋转不变性 ...