Spring Cloud Spring Boot mybatis分布式微服务云架构(四十一)实现Web层的日志切面
2018-03-09 09:55
1131 查看
实现AOP的切面主要有以下几个要素:
使用
使用
根据需要在切入点不同位置的切入内容
使用
使用
使用
使用
使用
通过运行程序并访问:
那么我们是否可以在WebLogAspect切面中定义一个成员变量来给doBefore和doAfterReturning一起访问呢?是否会有同步问题呢?
的确,直接在这里定义基本类型会有同步问题,所以我们可以引入ThreadLocal对象,像下面这样进行记录:
所以,我们需要定义每个切面的优先级,我们需要
在
在
所以我们可以这样子总结:
在切入点前的操作,按order的值由小到大执行
在切入点后的操作,按order的值由大到小执行
源码来源
使用
@Aspect注解将一个java类定义为切面类
使用
@Pointcut定义一个切入点,可以是一个规则表达式,比如下例中某个package下的所有函数,也可以是一个注解等。
根据需要在切入点不同位置的切入内容
使用
@Before在切入点开始处切入内容
使用
@After在切入点结尾处切入内容
使用
@AfterReturning在切入点return内容之后切入内容(可以用来对处理返回值做一些加工处理)
使用
@Around在切入点前后切入内容,并自己控制何时执行切入点自身的内容
使用
@AfterThrowing用来处理当切入内容部分抛出异常之后的处理逻辑
@Aspect @Component public class WebLogAspect { private Logger logger = Logger.getLogger(getClass()); @Pointcut("execution(public * com.didispace.web..*.*(..))") public void webLog(){} @Before("webLog()") public void doBefore(JoinPoint joinPoint) throws Throwable { // 接收到请求,记录请求内容 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); // 记录下请求内容 logger.info("URL : " + request.getRequestURL().toString()); logger.info("HTTP_METHOD : " + request.getMethod()); logger.info("IP : " + request.getRemoteAddr()); logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()); logger.info("ARGS : " + Arrays.toString(joinPoint.getArgs())); } @AfterReturning(returning = "ret", pointcut = "webLog()") public void doAfterReturning(Object ret) throws Throwable { // 处理完请求,返回内容 logger.info("RESPONSE : " + ret); } }可以看上面的例子,通过
@Pointcut定义的切入点为
com.didispace.web包下的所有函数(对web层所有请求处理做切入点),然后通过
@Before实现,对请求内容的日志记录(本文只是说明过程,可以根据需要调整内容),最后通过
@AfterReturning记录请求返回的对象。
通过运行程序并访问:
http://localhost:8080/hello?name=didi,可以获得下面的日志输出
2016-05-19 13:42:13,156 INFO WebLogAspect:41 - URL : http://localhost:8080/hello 2016-05-19 13:42:13,156 INFO WebLogAspect:42 - HTTP_METHOD : http://localhost:8080/hello 2016-05-19 13:42:13,157 INFO WebLogAspect:43 - IP : 0:0:0:0:0:0:0:1 2016-05-19 13:42:13,160 INFO WebLogAspect:44 - CLASS_METHOD : com.didispace.web.HelloController.hello 2016-05-19 13:42:13,160 INFO WebLogAspect:45 - ARGS : [didi] 2016-05-19 13:42:13,170 INFO WebLogAspect:52 - RESPONSE:Hello didi
优化:AOP切面中的同步问题
在WebLogAspect切面中,分别通过doBefore和doAfterReturning两个独立函数实现了切点头部和切点返回后执行的内容,若我们想统计请求的处理时间,就需要在doBefore处记录时间,并在doAfterReturning处通过当前时间与开始处记录的时间计算得到请求处理的消耗时间。那么我们是否可以在WebLogAspect切面中定义一个成员变量来给doBefore和doAfterReturning一起访问呢?是否会有同步问题呢?
的确,直接在这里定义基本类型会有同步问题,所以我们可以引入ThreadLocal对象,像下面这样进行记录:
@Aspect @Component public class WebLogAspect { private Logger logger = Logger.getLogger(getClass()); ThreadLocal<Long> startTime = new ThreadLocal<>(); @Pointcut("execution(public * com.didispace.web..*.*(..))") public void webLog(){} @Before("webLog()") public void doBefore(JoinPoint joinPoint) throws Throwable { startTime.set(System.currentTimeMillis()); // 省略日志记录内容 } @AfterReturning(returning = "ret", pointcut = "webLog()") public void doAfterReturning(Object ret) throws Throwable { // 处理完请求,返回内容 logger.info("RESPONSE : " + ret); logger.info("SPEND TIME : " + (System.currentTimeMillis() - startTime.get())); } }
优化:AOP切面的优先级
由于通过AOP实现,程序得到了很好的解耦,但是也会带来一些问题,比如:我们可能会对Web层做多个切面,校验用户,校验头信息等等,这个时候经常会碰到切面的处理顺序问题。所以,我们需要定义每个切面的优先级,我们需要
@Order(i)注解来标识切面的优先级。i的值越小,优先级越高。假设我们还有一个切面是
CheckNameAspect用来校验name必须为didi,我们为其设置
@Order(10),而上文中WebLogAspect设置为
@Order(5),所以WebLogAspect有更高的优先级,这个时候执行顺序是这样的:
在
@Before中优先执行
@Order(5)的内容,再执行
@Order(10)的内容
在
@After和
@AfterReturning中优先执行
@Order(10)的内容,再执行
@Order(5)的内容
所以我们可以这样子总结:
在切入点前的操作,按order的值由小到大执行
在切入点后的操作,按order的值由大到小执行
源码来源
相关文章推荐
- Spring Cloud Spring Boot mybatis分布式微服务云架构(四十三)使用log4j实现http请求日志入mongodb(2)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(四十)使用AOP统一处理Web请求日志(1)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(四十二)使用log4j实现http请求日志入mongodb(1)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十一)Web应用的统一异常处理
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十一)Web应用的统一异常处理
- Spring Cloud Spring Boot mybatis分布式微服务云架构(四十五)动态修改日志级别(2)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(三十九)对log4j进行多环境不同日志级别的控制
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十一)Web应用的统一异常处理
- Spring Cloud Spring Boot mybatis分布式微服务云架构(四十四)动态修改日志级别(1)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(三十七)日志管理
- Spring Cloud Spring Boot mybatis分布式微服务云架构(三十八)使用log4j记录日志
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十五)通过JdbcTemplate编写数据访问
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十二)返回JSON格式
- Spring Cloud Spring Boot mybatis分布式微服务云架构(三)属性配置文件详解(1)
- Spring Cloud Spring Boot mybatis分布式微服务云架构(二)使用Intellij中的Spring Initializr来快速构建Spring Boot/Cloud工程
- Spring Cloud Spring Boot mybatis分布式微服务云架构(九)使用Swagger2构建强大的RESTful API文档(1)
- spring cloud + spring boot + springmvc+mybatis分布式微服务云架构
- Spring Cloud Spring Boot mybatis分布式微服务云架构(十七)使用Spring-data-jpa(1)
- spring cloud + spring boot + springmvc+mybatis分布式微服务云架构
- Spring Cloud Spring Boot mybatis分布式微服务云架构(八)开发Web应用(2)