当前位置: 首页 > news >正文

西安域名注册网站建设做旅游网站

西安域名注册网站建设,做旅游网站,公众号怎么制作长图,wordpress 加载常用注解 Aspect 切面类 Before 前置 AfterReturning 后置 Around 环绕 AfterThrowing 异常 切入点设置 execution(public * *(..)) 定义任意公共方法的执行 execution(* set*(..)) 定义任何一个以"set"开始的方法的执行 execution(* com.sys.service.UserService…

常用注解

@Aspect 切面类
@Before 前置
@AfterReturning 后置
@Around 环绕
@AfterThrowing 异常

切入点设置

execution(public * *(..)) 定义任意公共方法的执行
execution(* set*(..)) 定义任何一个以"set"开始的方法的执行
execution(* com.sys.service.UserService.*(..)) 定义UserService 接口的任意方法的执行
execution(* com.sys.service.*.*(..)) 定义在service包里的任意方法的执行
execution(* com.sys.service ..*.*(..)) 定义在service包和所有子包里的任意类的任意方法的执行
execution(* com.sys.pointcutexp…JoinPointObject.*(…)) 定义在pointcutexp包和所有子包里的JoinPointObject类的任意方法的执行

本次使用aop用途说明

使用@AfterReturning方法拿到请求接口的返回状态码,将需要处理的问题状态码及错误信息保存到数据库及输出到日志文件,方便通过数据库进行接口调用错误信息监控。通过配置切入点对controller的所有请求接口进行处理,就无需再在每一个请求方法中进行处理。
使用@AfterThrowing方法拿到请求接口方法异常调用信息,其他同上。

参考代码

package com.system.common.aop;import com.alibaba.fastjson.JSON;
import com.system.service.SysLogService;
import com.xlx.entity.BaseResponse;
import com.xlx.entity.bsc.system.DO.SystemLogInfoDO;
import com.xlx.utils.ExceptionUtil;
import com.xlx.utils.StringParseUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;import java.lang.reflect.Method;
import java.util.Arrays;/*** @version: java version 1.8* @Author: sagitario* @description: 异常请求记录到数据库及日志输出* @date: 2023-04-20 11:44*/
@Component
@Aspect
@Slf4j
public class ApiResponseAOP {@Autowiredprivate SysLogService sysLogService;private static String [] passUrl = {"过滤的请求url"};@Pointcut("execution(* com.*.controller.*.*(..))")private void methodAspect(){}//定义一个切入点@Before("methodAspect()")public void doAudit(JoinPoint joinPoint) {log.info("BeforeAdvice.............");}@AfterReturning(value = "methodAspect()",returning = "methodResult")public void afterReturning(JoinPoint joinPoint, Object methodResult) {MethodSignature ms = (MethodSignature) joinPoint.getSignature();Method method = ms.getMethod();String clazz = method.getDeclaringClass().getName();log.debug("请求类为:" + clazz);String mName = method.getName();log.debug("请求方法为:" + mName);if(Arrays.binarySearch(passUrl, mName)<0){BaseResponse baseResponse = (BaseResponse) methodResult;if(!baseResponse.isSuccess()){StringBuffer stringBuffer = new StringBuffer();for (int i = 0; i < joinPoint.getArgs().length; i++) {log.debug("请求参数为:" + JSON.toJSONString(joinPoint.getArgs()[i]));stringBuffer.append(JSON.toJSONString(joinPoint.getArgs()[i]));}log.debug("请求返回内容为:" + methodResult.toString());SystemLogInfoDO systemLogInfoDO = new SystemLogInfoDO();systemLogInfoDO.setInfo(clazz + "." + mName);systemLogInfoDO.setParam(stringBuffer.toString());systemLogInfoDO.setCode(baseResponse.getCode());systemLogInfoDO.setMessage(baseResponse.getMessage());if(baseResponse.getCode()==204){systemLogInfoDO.setLevel("INFO");}else{systemLogInfoDO.setLevel("ERROR");}sysLogService.insertSelective(systemLogInfoDO);}}}@AfterThrowing(value = "methodAspect()",throwing = "e")public void afterThrowing(JoinPoint joinPoint, Exception e) {log.info("AfterThrowing advice");MethodSignature ms = (MethodSignature) joinPoint.getSignature();Method method = ms.getMethod();String clazz = method.getDeclaringClass().getName();log.info("请求类为:" + clazz);String mName = method.getName();log.info("请求方法为:" + mName);SystemLogInfoDO systemLogInfoDO = new SystemLogInfoDO();try {StringBuffer stringBuffer = new StringBuffer();for (int i = 0; i < joinPoint.getArgs().length; i++) {log.info("请求参数为:" + JSON.toJSONString(joinPoint.getArgs()[i]));stringBuffer.append(JSON.toJSONString(joinPoint.getArgs()[i]));}systemLogInfoDO.setParam(stringBuffer.toString());}catch (Exception e1){log.info("请求方法参数无法解析:" + ExceptionUtil.getMessage(e1));systemLogInfoDO.setParam("请求方法参数无法解析");}String error = ExceptionUtil.getMessage(e);log.info("afterThrowing异常:{}",error);systemLogInfoDO.setInfo(clazz + "." + mName);systemLogInfoDO.setCode(500);systemLogInfoDO.setMessage(error);systemLogInfoDO.setLevel("EXCEPTION");sysLogService.insertSelective(systemLogInfoDO);}}

日志输出

2023-05-22 18:36:14 [http-nio-8831-exec-6] [com.loan.common.aop.ApiResponseAOP:82] INFO  com.loan.common.aop.ApiResponseAOP - 请求类为:com.loan.controller.RepayController
2023-05-22 18:36:14 [http-nio-8831-exec-6] [com.loan.common.aop.ApiResponseAOP:84] INFO  com.loan.common.aop.ApiResponseAOP - 请求方法为:repaymentRecord
2023-05-22 18:36:14 [http-nio-8831-exec-6] [com.loan.common.aop.ApiResponseAOP:89] INFO  com.loan.common.aop.ApiResponseAOP - 请求参数为:"110014"
2023-05-22 18:36:14 [http-nio-8831-exec-6] [com.xlx.exception.GlobalExceptionHandler:26] ERROR com.xlx.exception.GlobalExceptionHandler - feign.RetryableException: Read timed out executing GET http://external-platform-server/repay/repaymentRecordsat feign.FeignException.errorExecuting(FeignException.java:213)at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:115)......
2023-05-22 18:36:14 [http-nio-8831-exec-6] [com.loan.common.aop.ApiResponseAOP:98] INFO  com.loan.common.aop.ApiResponseAOP - afterThrowing异常:feign.RetryableException: Read timed out executing GET http://external-platform-server/repay/repaymentRecordsat feign.FeignException.errorExecuting(FeignException.java:213)at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:115)at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:80)at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:103)

其他

JDK动态代理相关可参考 一篇文章彻底搞懂java AOP、@Before、@After、@AfterReturning、@AfterThrowing、@Around的使用、Spring AOP详解

http://www.yayakq.cn/news/125815/

相关文章:

  • 企业做国外网站多少钱企业网站建设费用财务处理
  • 大型企业的微网站谁做电子商务网站建设规划报告
  • 网站如何做超链接微信管理中心
  • 乐山建设网站广州商砼建站规范
  • 无极修仙网站WordPress里h1跟p有什么
  • 官方网站链接如何做衡水企业做网站多少钱
  • 罗湖做网站的公司便宜自适应网站建设
  • 免费下载软件的网站wordpress主题屋
  • 有哪些做婚礼电子请柬的网站wordpress 程序员
  • 关于推进公司网站开发的请示免费建网站的步骤
  • 为企业做贡献演讲稿网站优化搜索排名
  • 外卖网站 模板企业年金交满多少年才能领取
  • 网站锚文本的内链建设太仓住房与城乡建设局网站
  • 做网络作家哪个网站好阿里云域名注册备案流程
  • 网站模块在线制作教程学php做网站
  • 网站登录页面模板 下载企业网站实名认证时间
  • 阿里巴巴申请网站怎么做上海静安网站制作
  • 制作网站比较大的几家公司wordpress免费注册
  • 厦门网站建设商家商丘互联网公司
  • 深圳宝安专业做网站公司分销平台用户协议
  • 班级网站建设开题报告湖南网站开发 d岚鸿
  • 一些可以做翻译的网站wordpress七牛sdk
  • 建设银行官方网站木材网站建设哪家好
  • 正规设计兼职网站有哪些张家港保税区建设局网站
  • 蓝田县住房与城乡建设局网站苏州优化亚当
  • 南宁网站开发价格英文网站源码下载
  • 网站建设及服务合同北京大厂网站建设
  • 用中文模版可以做英文网站吗wordpress添加新php页面
  • 机械技术支持 东莞网站建设江苏质量员证在哪个网站做的
  • 手机网站 html5小程序appid是什么