第四十章 SpringBoot AOP
来源:互联网 发布:java web书籍推荐 编辑:程序博客网 时间:2024/05/16 12:21
参考第四章 添加AOP支持,SpringBoot使用Aop略有不同
一、添加pom依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId></dependency>
二、编写AOP切面
package com.container.monitor;import java.util.Arrays;import javax.servlet.http.HttpServletRequest;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;import org.springframework.util.StringUtils;import org.springframework.web.context.request.RequestContextHolder;import org.springframework.web.context.request.ServletRequestAttributes;import com.alibaba.druid.support.json.JSONUtils;@Aspect@Componentpublic class ApiMonitor { private static final Logger logger = LoggerFactory.getLogger(ApiMonitor.class); ThreadLocal<Long> startTime = new ThreadLocal<Long>(); @Pointcut("execution(public * com.container..*.controller.*Controller.*(..))") @Order(1) public void controllerPoint() {} @Before("controllerPoint()") public void doBeforeControllerPoint(JoinPoint joinPoint) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); logger.info("<===================================================================="); logger.info("请求来源: => " + request.getRemoteAddr()); logger.info("请求URL: " + request.getRequestURL().toString()); logger.info("请求方式: " + request.getMethod()); logger.info("响应方法: " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()); logger.info("请求参数 : " + Arrays.toString(joinPoint.getArgs())); logger.info("---------------------------------------------------------------------"); startTime.set(System.currentTimeMillis()); } @AfterReturning(pointcut="controllerPoint()", returning="rst") public void doAfterControllerPoint(JoinPoint joinPoint, Object rst) { logger.info("耗时(毫秒) : " + (System.currentTimeMillis() - startTime.get())); logger.info("返回数据: {}", JSONUtils.toJSONString(rst)); logger.info("====================================================================>"); }}
0 0
- 第四十章 SpringBoot AOP
- springboot-AOP
- SpringBoot-AOP
- springBoot aop
- 第三章第四十题
- 第三章 第四十题
- 《道德经》第四十章
- springboot-aop访问拦截
- Springboot配置aop
- springboot集成AOP
- springboot使用AOP
- SpringBoot aop demo
- SpringBoot-10整合AOP
- springboot之AOP配置
- springboot-AOP
- SpringBoot-AOP处理请求
- SpringBoot(五):aop
- Springboot与Aop
- 在c++中,有哪4个与类型转换相关的关键字,这些关键字各有什么特点,应该在什么场合下使用?
- vue.js学习(二)
- C/C++中extern关键字详解
- 前缀、中缀、后缀表达式
- ss5客户端设置
- 第四十章 SpringBoot AOP
- Canvas 实现时钟
- Java并发编程的艺术(十)——线程池(1)
- HTTPS 为什么更安全
- FIRST TOUCH
- Servlet笔记
- android studio 混淆打包
- 算法学习之显著性检测
- 数据库端口3306被占用