前言:日志在我们的日常开发当中是必定会用到的,在每个方法的上都会习惯性打上@Log注解,这样系统就会自动帮我们记录日志,整体的代码结构就会非常优雅,这边我自己搭建了一个demo去实现了一些这个项目当中必定会用的功能。
目录
一、 什么是Aop
二、Aop常用注解
三、execution表达式简介
四、代码实现
1、引入依赖
2、创建枚举类
3、创建自定义注解
4、切面类实现
5、创建Controller类
6、运行结果
五、Gitee源码
一、 什么是Aop
Aop是一种编程思想(面向切面编程),通俗的用自己的语言来讲便是在我们进入某个方法之前、做完某个方法之后,Aop还会帮我们执行一些代码块,比如记录日志和监控等等。
二、Aop常用注解
1、@Pointcut注解:统一配置切入点
@Pointcut("@annotation(com.example.aop.annotation.Log)")
public void pointCut() {}
2、@Around注解:环绕增强
@Around(value = "pointCut()")
public Object round(ProceedingJoinPoint joinPoint){Object obj = null;try {obj = joinPoint.proceed();} catch (Throwable throwable) {throwable.printStackTrace();}return obj;
}
3、@Before注解:在目标方法执行前触发
@Before(value = "pointCut()")
public void before(){}
4、@After注解:在目标方法执行完毕后执行
@After(value = "pointCut()")
public void after(){}
5、@AfterReturning:在将返回值返回时执行
@AfterReturning(value = "pointCut()", returning = "result")
public void afterReturning(Object result){}
它们之间的执行顺序如下:
1、首先进入Around
2、执行joinPoint.proceed()之前,触发Before
3、然后执行joinPoint.proceed()
4、执行joinPoint.proceed()之后,触发After
5、最后触发AfterReturning
三、execution表达式简介
举个栗子:execution(* com.example.demo.controller..*.*(..))
标识符 | 含义 |
execution() | 表达式的主体 |
第一个*号 | 表示任意返回类型 |
com.example.demo.controller | Aop所切的服务包名 |
包名后面的两个点(..) | 当前controller包下面的所有子包 |
第二个* | 表示所有类 |
.*(..) | 表示任何方法名,括号表示参数,两个点表示任意参数类型 |
四、代码实现
1、引入依赖
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--aop依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--常用工具类 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.9</version></dependency><!--阿里巴巴json--><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.73</version></dependency></dependencies>
2、创建枚举类
BusinessType枚举类
package com.example.aop.enumType;public enum BusinessType {/*** 其它*/OTHER,/*** 新增*/INSERT,/*** 修改*/UPDATE,/*** 删除*/DELETE,/*** 授权*/GRANT,/*** 导出*/EXPORT,/*** 导入*/IMPORT,/*** 强退*/FORCE,/*** 生成代码*/GENCODE,/*** 清空数据*/CLEAN,
}
OperatorType枚举类
package com.example.aop.enumType;public enum OperatorType {/*** 其它*/OTHER,/*** 后台用户*/MANAGE,/*** 手机端用户*/MOBILE
}
3、创建自定义注解
package com.example.aop.annotation;import com.example.aop.enumType.BusinessType;
import com.example.aop.enumType.OperatorType;import java.lang.annotation.*;@Target({ ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Log {/*** 模块*/public String title() default "";/*** 功能*/public BusinessType businessType() default BusinessType.OTHER;/*** 操作人类别*/public OperatorType operatorType() default OperatorType.MANAGE;/*** 是否保存请求的参数*/public boolean isSaveRequestData() default true;
}
4、切面类实现
package com.example.aop.aspect;import com.alibaba.fastjson.JSON;
import com.example.aop.annotation.Log;
import com.example.aop.enumType.HttpMethod;
import com.example.aop.utils.ServletUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.HandlerMapping;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;@Aspect
@Component
public class LogAspect {private static final Logger log = LoggerFactory.getLogger(LogAspect.class);@Autowiredprivate HttpServletRequest request;@Pointcut("@annotation(com.example.aop.annotation.Log)")public void logPointCut() {}@Before("logPointCut()")public void doBefore(JoinPoint joinPoint) {log.info("====doBefore方法进入了====");// 获取签名Signature signature = joinPoint.getSignature();// 获取切入的包名String declaringTypeName = signature.getDeclaringTypeName();// 获取即将执行的方法名String funcName = signature.getName();log.info("即将执行方法为: {},属于{}包", funcName, declaringTypeName);// 也可以用来记录一些信息,比如获取请求的 URL 和 IPServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = attributes.getRequest();// 获取请求 URLString url = request.getRequestURL().toString();// 获取请求 IPString ip = request.getRemoteAddr();log.info("用户请求的url为:{},ip地址为:{}", url, ip);}/*** 处理完请求后执行** @param joinPoint 切点*/@AfterReturning(pointcut = "logPointCut()", returning = "jsonResult")public void doAfterReturning(JoinPoint joinPoint, Object jsonResult) {handleLog(joinPoint, null, jsonResult);}/*** 拦截异常操作** @param joinPoint 切点* @param e 异常*/@AfterThrowing(value = "logPointCut()", throwing = "e")public void doAfterThrowing(JoinPoint joinPoint, Exception e) {handleLog(joinPoint, e, null);}protected void handleLog(final JoinPoint joinPoint, final Exception e, Object jsonResult) {try {// 获得注解Log controllerLog = getAnnotationLog(joinPoint);if (controllerLog == null) {return;}String className = joinPoint.getTarget().getClass().getName();String methodName = joinPoint.getSignature().getName();log.info("className:{}",className);log.info("methodName:{}",methodName);log.info("businessType:{}", controllerLog.businessType());log.info("operatorType:{}", controllerLog.operatorType());log.info("title:{}", controllerLog.title());// 处理设置注解上的参数getControllerMethodDescription(joinPoint, controllerLog);} catch (Exception exp) {// 记录本地异常日志log.error("==前置通知异常==");log.error("异常信息:{}", exp.getMessage());exp.printStackTrace();}}/*** 获取注解中对方法的描述信息 用于Controller层注解** @param log 日志* @throws Exception*/public void getControllerMethodDescription(JoinPoint joinPoint, Log log) throws Exception {// 是否需要保存request,参数和值if (log.isSaveRequestData()) {// 获取参数的信息,传入到数据库中。setRequestValue(joinPoint);}}/*** 获取请求的参数,放到log中* @throws Exception 异常*/private void setRequestValue(JoinPoint joinPoint) throws Exception {String requestMethod = ServletUtils.getRequest().getMethod();// TODO 2021年09月26日20:03:51 获取请求参数if (HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod)) {//可以获得POST JSON 格式的参数String params = argsArrayToString(joinPoint.getArgs());log.info("params -{}",params);} else {//仅支持 Get请求 用@PathVariable("id")接收的参数 例如:http://localhost:8081/oauth2/oauth2/test/get/{id}Map<?, ?> paramsMap = (Map<?, ?>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);log.info("paramsMap -{}",paramsMap);}}/*** 是否存在注解,如果存在就获取*/private Log getAnnotationLog(JoinPoint joinPoint) throws Exception {Signature signature = joinPoint.getSignature();MethodSignature methodSignature = (MethodSignature) signature;Method method = methodSignature.getMethod();if (method != null) {return method.getAnnotation(Log.class);}return null;}/*** 参数拼装*/private String argsArrayToString(Object[] paramsArray) {String params = "";if (paramsArray != null && paramsArray.length > 0) {for (int i = 0; i < paramsArray.length; i++) {if (!isFilterObject(paramsArray[i])) {Object jsonObj = JSON.toJSON(paramsArray[i]);params += jsonObj.toString() + " ";}}}return params.trim();}/*** 判断是否需要过滤的对象。** @param o 对象信息。* @return 如果是需要过滤的对象,则返回true;否则返回false。*/@SuppressWarnings("rawtypes")public boolean isFilterObject(final Object o) {Class<?> clazz = o.getClass();if (clazz.isArray()) {return clazz.getComponentType().isAssignableFrom(MultipartFile.class);} else if (Collection.class.isAssignableFrom(clazz)) {Collection collection = (Collection) o;for (Iterator iter = collection.iterator(); iter.hasNext();) {return iter.next() instanceof MultipartFile;}} else if (Map.class.isAssignableFrom(clazz)) {Map map = (Map) o;for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {Map.Entry entry = (Map.Entry) iter.next();return entry.getValue() instanceof MultipartFile;}}return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse|| o instanceof BindingResult;}}
5、创建Controller类
package com.example.aop.controller;import com.example.aop.annotation.Log;
import com.example.aop.enumType.BusinessType;
import com.example.aop.enumType.OperatorType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/user")
public class UserController {@GetMapping("/hello")@Log(title = "认证模块",businessType = BusinessType.INSERT,operatorType = OperatorType.MANAGE,isSaveRequestData = true)public String hello(){return "hello";}
}
6、运行结果
五、Gitee源码
项目中用到的一些工具类也都在里面了,因为太多博客只提供了一些关键代码
SpringBoot基于Aop实现自定义日志注解: 我自己搭建了一个demo去实现了这个功能