旅行·在路上

Spring boot学习(六)Spring boot实现AOP

2019-04-26  本文已影响0人  Java架构学习者

前言

在实际的项目中,特别是管理系统中,对于那些重要的操作我们通常都会记录操作日志。比如对数据库的CRUD操作,我们都会对每一次重要的操作进行记录,通常的做法是向数据库指定的日志表中插入一条记录。这里就产生了一个问题,难道要我们每次在 CRUD的时候都手动的插入日志记录吗?这肯定是不合适的,这样的操作无疑是加大了开发量,而且不易维护,所以实际项目中总是利用AOP(Aspect Oriented Programming)即面向切面编程这一技术来记录系统中的操作日志。

日志分类

这里我把日志按照面向的对象不同分为两类:

面向用户的日志:用户是指使用系统的人,这一类日志通常记录在数据库里边,并且通常是记录对数据库的一些CRUD操作。

面向开发者的日志:查看这一类日志的一般都是开发人员,这类日志通常保存在文件或者在控制台打印(开发的时候在控制台,项目上线之后之后保存在文件中),这一类日志主要用于开发者开发时期和后期维护时期定位错误。

面向不同对象的日志,我们采用不同的策略去记录。很容易看出,对于面向用户的日志具有很强的灵活性,需要开发者控制用户的哪些操作需要向数据库记录日志,所以这一类保存在数据库的日志我们在使用 AOP记录时用自定义注解的方式去匹配;而面向开发者的日志我们则使用表达式去匹配就可以了(这里有可能叙述的有点模糊,看了下面去案例将会很清晰),下面分别介绍两种日志的实现。

实现AOP记录面向用户的日志

接下来分步骤介绍Spring boot中怎样实现通过AOP记录操作日志。

添加依赖

在pom.xml文件中添加如下依赖:

<!-- aop依赖 -->org.springframework.bootspring-boot-starter-aop

修改配置文件

在项目的application.properties文件中添加下面一句配置:

spring.aop.auto=true

这里特别说明下,这句话不加其实也可以,因为默认就是true,只要我们在pom.xml中添加了依赖就可以了,这里提出来是让大家知道有这个有这个配置。

自定义注解

上边介绍过了了,因为这类日志比较灵活,所以我们需要自定义一个注解,使用的时候在需要记录日志的方法上添加这个注解就可以了,首先在启动类的同级包下边新建一个config包,在这个报下边新建new一个名为Log的Annotation文件,文件内容如下:

packagecom.web.springbootaoplog.config;importjava.lang.annotation.ElementType;importjava.lang.annotation.Retention;importjava.lang.annotation.RetentionPolicy;importjava.lang.annotation.Target;/***@authorPromise*@createTime2018年12月18日 下午9:26:25*@description定义一个方法级别的@log注解*/@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public@interfaceLog { String value()default"";}

这里用到的是Java元注解的相关知识,不清楚相关概念的朋友可以去这篇博客get一下【传送门】。

准备数据库日志表以及实体类,sql接口,xml文件

既然是向数据库中插入记录,那么前提是需要创建一张记录日志的表,下面给出我的表sql,由于是写样例,我这里这张表设计的很简单,大家可以自行设计。

CREATETABLE`sys_log`(`id`int(11)NOTNULLAUTO_INCREMENTCOMMENT'主键',`user_id`int(11)NOTNULLCOMMENT'操作员id',`user_action`varchar(255)NOTNULLCOMMENT'用户操作',`create_time`datetimeDEFAULTNULLONUPDATECURRENT_TIMESTAMPCOMMENT'创建时间', PRIMARYKEY(`id`))ENGINE=InnoDBAUTO_INCREMENT=12DEFAULTCHARSET=utf8COMMENT='日志记录表';

通过上篇博客介绍的MBG生成相应的实体类,sql接口文件,以及xml文件,这里不再概述,不清楚的朋友请移步【传送门】

当然还需要创建service接口文件以及接口实现类,这里直接给出代码:

ISysLogServcie.java

package com.web.springbootaoplog.service;

import com.web.springbootaoplog.entity.SysLog;

/**

* @author Promise

* @createTime 2018年12月18日 下午9:29:48

* @description 日志接口

*/

public interface ISysLogService {

/**

* 插入日志

* @param entity

* @return

*/

int insertLog(SysLog entity);

}

SysLogServiceImpl.java

packagecom.web.springbootaoplog.service.impl;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importcom.web.springbootaoplog.config.Log;importcom.web.springbootaoplog.dao.SysLogMapper;importcom.web.springbootaoplog.entity.SysLog;importcom.web.springbootaoplog.service.ISysLogService;/***@authorPromise*@createTime2018年12月18日 下午9:30:57*@description*/@Service("sysLogService")publicclassSysLogServiceImplimplementsISysLogService{@AutowiredprivateSysLogMapper sysLogMapper;@OverridepublicintinsertLog(SysLog entity){// TODO Auto-generated method stubreturnsysLogMapper.insert(entity); }}

AOP的切面和切点

准备上边的相关文件后,下面来介绍重点--创建AOP切面实现类,同样我们这里将该类放在config包下,命名为LogAsPect.java,内容如下:

packagecom.web.springbootaoplog.config;importjava.lang.reflect.Method;importjava.util.Arrays;importjava.util.Date;importorg.aspectj.lang.JoinPoint;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.Around;importorg.aspectj.lang.annotation.Aspect;importorg.aspectj.lang.annotation.Before;importorg.aspectj.lang.annotation.Pointcut;importorg.aspectj.lang.reflect.MethodSignature;importorg.hibernate.validator.internal.util.logging.LoggerFactory;importorg.slf4j.Logger;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.core.LocalVariableTableParameterNameDiscoverer;importorg.springframework.stereotype.Component;importcom.web.springbootaoplog.entity.SysLog;importcom.web.springbootaoplog.service.ISysLogService;/**

* @author Promise

* @createTime 2018年12月18日 下午9:33:28

* @description 切面日志配置

*/@Aspect@Componentpublicclass LogAsPect {privatefinalstaticLoggerlog= org.slf4j.LoggerFactory.getLogger(LogAsPect.class); @AutowiredprivateISysLogService sysLogService;//表示匹配带有自定义注解的方法@Pointcut("@annotation(com.web.springbootaoplog.config.Log)")publicvoidpointcut() {}  @Around("pointcut()")publicObjectaround(ProceedingJoinPointpoint) {Objectresult =null;longbeginTime = System.currentTimeMillis();try{log.info("我在目标方法之前执行!"); result =point.proceed();longendTime = System.currentTimeMillis(); insertLog(point,endTime-beginTime); }catch(Throwable e) {// TODO Auto-generated catch block}returnresult; }privatevoidinsertLog(ProceedingJoinPointpoint,longtime) { MethodSignature signature = (MethodSignature)point.getSignature(); Method method = signature.getMethod(); SysLog sys_log =newSysLog();  Log userAction = method.getAnnotation(Log.class);if(userAction !=null) {// 注解上的描述sys_log.setUserAction(userAction.value()); }// 请求的类名StringclassName =point.getTarget().getClass().getName();// 请求的方法名StringmethodName = signature.getName();// 请求的方法参数值Stringargs = Arrays.toString(point.getArgs());//从session中获取当前登陆人id// Long useride = (Long)SecurityUtils.getSubject().getSession().getAttribute("userid");Long userid =1L;//应该从session中获取当前登录人的id,这里简单模拟下sys_log.setUserId(userid);  sys_log.setCreateTime(newjava.sql.Timestamp(newDate().getTime()));log.info("当前登陆人:{},类名:{},方法名:{},参数:{},执行时间:{}",userid, className, methodName, args, time);  sysLogService.insertLog(sys_log); }}

这里简单介绍下关于AOP的几个重要注解:

@Aspect:这个注解表示将当前类视为一个切面类

@Component:表示将当前类交由Spring管理。

@Pointcut:切点表达式,定义我们的匹配规则,上边我们使用@Pointcut("@annotation(com.web.springbootaoplog.config.Log)")表示匹配带有我们自定义注解的方法。

@Around:环绕通知,可以在目标方法执行前后执行一些操作,以及目标方法抛出异常时执行的操作。

我们用到的注解就这几个,当然还有其他的注解,这里我就不一一介绍了,想要深入了解AOP相关知识的朋友可以移步官方文档【传送门】

下面看一段关键的代码:

log.info("我在目标方法之前执行!");result = point.proceed();long endTime = System.currentTimeMillis();insertLog(point,endTime-beginTime);

其中result = point.proceed();这句话表示执行目标方法,可以看出我们在这段代码执行之前打印了一句日志,并在执行之后调用了insertLog()插入日志的方法,并且在方法中我们可以拿到目标方法所在的类名,方法名,参数等重要的信息。

测试控制器

在controller包下新建一个HomeCOntroller.java(名字大家随意),内容如下:

packagecom.web.springbootaoplog.controller;importjava.util.HashMap;importjava.util.Map;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestBody;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.ResponseBody;importcom.web.springbootaoplog.config.Log;importcom.web.springbootaoplog.entity.SysLog;importcom.web.springbootaoplog.service.ISysLogService;/***@authorPromise*@createTime2019年1月2日 下午10:35:30*@description测试controller*/@ControllerpublicclassHomeController{privatefinalstatic Logger log = org.slf4j.LoggerFactory.getLogger(HomeController.class);@AutowiredprivateISysLogService logService;@RequestMapping("/aop")@ResponseBody@Log("测试aoplog")publicObject aop(String name, String nick) { Map map =new HashMap<>(); log.info("我被执行了!"); map.put("res","ok");returnmap; }}

定义一个测试方法,带有两个参数,并且为该方法添加了我们自定义的@Log注解,启动项目,浏览器访问localhost:8080/aop?name=xfr&nick=eran,这时候查看eclipse控制台的部分输出信息如下:

2019-01-2422:02:17.682INFO3832---[nio-8080-exec-1]c.web.springbootaoplog.config.LogAsPect:我在目标方法之前执行!2019-01-2422:02:17.688INFO3832---[nio-8080-exec-1]c.w.s.controller.HomeController:我被执行了!2019-01-2422:02:17.689INFO3832---[nio-8080-exec-1]c.web.springbootaoplog.config.LogAsPect:当前登陆人:1,类名:com.web.springbootaoplog.controller.HomeController,方法名:aop,参数:[xfr,eran],执行时间:6

可以看到我们成功在目标方法执行前后插入了一些逻辑代码,现在再看数据库里边的数据:

在这里插入图片描述

成功记录了一条数据。

实现AOP记录面向开发者的日志

首先这里我列举一个使用该方式的应用场景,在项目中出现了bug,我们想要知道前台的请求是否进入了我们控制器中,以及参数的获取情况,下面开始介绍实现步骤。

其实原理跟上边是一样的,只是切点的匹配规则变了而已,而且不用将日志记录到数据库,打印出来即可。

首先在LogAsPect.java中定义一个新的切点表达式,如下:

@Pointcut("execution(public * com.web.springbootaoplog.controller..*.*(..))")

public void pointcutController() {}

@Pointcut("execution(public * com.web.springbootaoplog.controller..*.*(..))")表示匹配com.web.springbootaoplog.controller包及其子包下的所有公有方法。

关于这个表达式详细的使用方法可以移步这里,【传送门】

再添加匹配到方法时我们要做的操作:

@Before("pointcutController()")publicvoidaround2(JoinPointpoint) {//获取目标方法StringmethodNam =point.getSignature().getDeclaringTypeName() +"."+point.getSignature().getName();//获取方法参数Stringparams = Arrays.toString(point.getArgs());log.info("get in {} params :{}",methodNam,params);}

@Before:表示目标方法执行之前执行以下方法体的内容。

再在控制器中添加一个测试方法:

@RequestMapping("/testaop3")@ResponseBodypublicObjecttestAop3(Stringname,Stringnick) { Mapmap=newHashMap<>();map.put("res","ok");returnmap;}

可以看到这个方法我们并没有加上@Log注解,重启项目,浏览器访问localhost:8080/testaop3?name=xfr&nick=eran,这时候查看eclipse控制台的部分输出信息如下:

2019-01-2423:19:49.108INFO884---[nio-8080-exec-1]c.web.springbootaoplog.config.LogAsPect:getincom.web.springbootaoplog.controller.HomeController.testAop3params:[xfr, eran]

打印出了关键日志,这样我们就能知道是不是进入了该方法,参数获取是否正确等关键信息。

这里有的朋友或许会有疑问这样会不会与添加了@Log的方法重复了呢,的确会,所以在项目中我通常都将@Log注解用在了Service层的方法上,这样也更加合理。

结语

好了,关于Aop记录日志的内容就介绍这么多了,下一篇博客再见。bye~

上一篇下一篇

猜你喜欢

热点阅读