spring-boot使用AOP统一处理日志
作者:贺小五 时间:2023-06-09 05:12:35
AOP我想大家都很清楚,有时候我们需要处理一些请求日志,或者对某些方法进行一些监控,如果出现例外情况应该进行怎么样的处理,现在,我们从spring-boot中引入AOP.
[开发环境:jdk版本号为1.8,spring
boot的版本号为1.4.1]{style=”background-color:#FF0000”}
首先,我们先引入jar包,
POM文件添加如下内容:
<!--引用AOP-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!--引用GSON,用于打印-->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.7</version>
</dependency>
引入jar包后,我们在boot的启动方法,增加两个简单的请求处理方法:
@SpringBootApplication(scanBasePackages = {"com"})
@RestController
public class DemoApplication{
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
//测试无参的get请求
@RequestMapping(value = "/testAspect",method = RequestMethod.GET)
public UserVo test(){
UserVo userVo = new UserVo();
userVo.setAge("23");
userVo.setName("贺小五");
userVo.setSex("男");
return userVo;
}
//测试有参的get请求,让aop打印参数内容
@RequestMapping(value = "/testAspectArgs",method = RequestMethod.GET)
public UserVo test(String name,String age,String sex){
UserVo userVo = new UserVo();
userVo.setName(name);
userVo.setAge(age);
userVo.setSex(sex);
return userVo;
}
增加了两个简单的处理请求方法后,我们来增加我们的aop
/**
* 项目名:SpringBootDemo
* 创建人:贺小五
* 创建时间:16/12/4 下午7:05
* 类名:AspectDemo
* 类描述:
*/
//申明是个切面
@Aspect
//申明是个spring管理的bean
@Component
@Order(1)
public class AspectDemo {
private Logger log = Logger.getLogger(getClass());
private Gson gson = new Gson();
//申明一个切点 里面是 execution表达式
@Pointcut("execution(public * com.example.DemoApplication.*(..))")
private void controllerAspect(){}
//请求method前打印内容
@Before(value = "controllerAspect()")
public void methodBefore(JoinPoint joinPoint){
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
//打印请求内容
log.info("===============请求内容===============");
log.info("请求地址:"+request.getRequestURL().toString());
log.info("请求方式:"+request.getMethod());
log.info("请求类方法:"+joinPoint.getSignature());
log.info("请求类方法参数:"+ Arrays.toString(joinPoint.getArgs()));
log.info("===============请求内容===============");
}
//在方法执行完结后打印返回内容
@AfterReturning(returning = "o",pointcut = "controllerAspect()")
public void methodAfterReturing(Object o ){
log.info("--------------返回内容----------------");
log.info("Response内容:"+gson.toJson(o));
log.info("--------------返回内容----------------");
}
两者配置好之后,接下来,我们请求,然后查看打印日志,首先,启动我们的容器,然后我们先请求有参的处理请求方法的,打印日志如下:
{width=”1232”
height=”132”}
可以发现,请求的url,method,以及args参数的值,还有类型,以及返回的内容都打印出来了,说明,这是AOP拦截成功了.
接下来,我们测试下请求无参的处理请求方法,打印日志如下:
{width=”1100”
height=”130”}
我们可以发现,这个方法,打印的方法参数是个空数组,是因为方法不需要参数传递.
来源:https://my.oschina.net/u/2278977/blog/799786
标签:springboot,AOP,日志
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#自定义函数NetxtString生成随机字符串
2022-06-18 20:43:39
![](https://img.aspxhome.com/file/2023/3/104313_0s.png)
Android ListView适配器(Adapter)优化方法详解
2023-08-09 21:44:32
![](https://img.aspxhome.com/file/2023/6/97856_0s.jpg)
Java反射机制详解
2023-10-01 12:18:04
教你轻松制作Android音乐播放器
2022-09-28 01:19:28
![](https://img.aspxhome.com/file/2023/1/111741_0s.png)
JavaBean和SpringBean的区别及创建SpringBean方式
2022-05-23 03:32:14
springboot中validator数据校验功能的实现
2021-07-31 17:43:50
![](https://img.aspxhome.com/file/2023/2/69322_0s.png)
详解如何使用maven生成可以执行的jar
2023-08-17 00:18:43
VS2010中lib与dll文件的生成与使用方法
2023-01-23 11:32:50
C#抽象类和接口的区别分析
2023-11-09 13:56:21
Java通过索引值实现约瑟夫环算法
2021-09-28 22:33:48
Spring Boot整合JWT的实现步骤
2022-07-17 04:48:58
![](https://img.aspxhome.com/file/2023/3/132073_0s.png)
SpringBoot整合activemq的案例代码
2023-11-06 18:41:37
![](https://img.aspxhome.com/file/2023/9/58589_0s.png)
C#中在WebClient中使用post发送数据实现方法
2023-05-01 00:03:54
SpringBoot JSON全局日期格式转换器实现方式
2021-10-03 10:07:28
![](https://img.aspxhome.com/file/2023/4/132204_0s.png)
Java如何从json字符串中获取某个值详解
2023-11-18 12:18:12
![](https://img.aspxhome.com/file/2023/6/108386_0s.png)
Android Studio中Logcat写入和查看日志
2021-08-05 12:37:18
![](https://img.aspxhome.com/file/2023/9/106019_0s.png)
浅谈MyBatis 如何执行一条 SQL语句
2023-10-17 19:36:03
![](https://img.aspxhome.com/file/2023/3/68213_0s.png)
Java多线程ThreadPoolExecutor详解
2023-11-23 18:39:32
解决nacos项目启动报错:Connection refused: no further informa问题
2023-08-16 16:55:41
![](https://img.aspxhome.com/file/2023/1/65201_0s.jpg)
WPF实现动画效果(六)之路径动画
2022-02-05 01:42:15