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

珠海品牌网站制作服务产品推广网站哪个好

珠海品牌网站制作服务,产品推广网站哪个好,廊坊安次区网站建设公司,苏州网站建设完善方案项目中遇到多个模块需要打印Controller请求日志,在每个模块里面加AOP并且配置单独的切面笔者认为代码冗余,于是乎就打算把AOP日志打印抽离成一个公共模块,谁想用就引入Maven坐标就行。 定义公共AOP模块 并编写AOP工具 AOP模块pom.xml如下 &…

项目中遇到多个模块需要打印Controller请求日志,在每个模块里面加AOP并且配置单独的切面笔者认为代码冗余,于是乎就打算把AOP日志打印抽离成一个公共模块,谁想用就引入Maven坐标就行。

定义公共AOP模块 并编写AOP工具

AOP模块pom.xml如下


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://maven.apache.org/POM/4.0.0"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent>这里根据自己需要引入 公共AOP父模块</parent><modelVersion>4.0.0</modelVersion><artifactId>xx-common-aop</artifactId><description>xx-common-aop切面</description><dependencies><!-- Lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!-- SLF4J --><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId></dependency><!-- FastJSON --><dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2</artifactId></dependency><!-- Hutool --><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId></dependency><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies></project>

AOP核心代码

import cn.hutool.extra.servlet.ServletUtil;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;import javax.servlet.http.HttpServletRequest;
import java.util.*;/*** 类名称: ServiceLogAop * 类描述: api入参, 出参打印*/
@Aspect
@Component
@Slf4j
public class ServiceLogAop {/*** 换行符*/private static final String LINE_SEPARATOR = System.lineSeparator();/*** 以自定义 @ServiceLogAop 注解为切点*/@Pointcut("execution(public *  com.*.*.controller.*.*(..))")public void webLog() {}//基本类型定义,用于判断请求参数的类型private static List typeList = new ArrayList();private static String[] types = {"java.lang.Integer", "java.lang.Double","java.lang.Float", "java.lang.Long", "java.lang.Short","java.lang.Byte", "java.lang.Boolean", "java.lang.Char","java.lang.String", "int", "double", "long", "short", "byte","boolean", "char", "float"};static {for (int i = 0; i < types.length; i++) {typeList.add(types[i]);}}/*** 在切点之前织入** @param joinPoint* @throws Throwable*/@Before("webLog()")public void doBefore(JoinPoint joinPoint) {// 开始打印请求日志ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();HttpServletRequest request = attributes.getRequest();// 获取 @WebLog 注解的描述信息// 打印请求相关参数log.info("========================================== Start ==========================================");// 打印请求 urllog.info("URL           : {}", request.getRequestURL().toString());//uriString uri = request.getRequestURI();log.info("URI           : {}", uri);// 打印 Http methodlog.info("HTTP Method   : {}", request.getMethod());// 打印调用 controller 的全路径以及执行方法log.info("Class Method  : {}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());//IPlog.info("IP             : {}", request.getRemoteAddr());// 打印请求的 IPlog.info("IP             : {}", ServletUtil.getClientIP(request));//TokenString authorization = request.getHeader("Authorization");log.info("Authorization  : {}", authorization);
/*        //cokieString Cookie = request.getHeader("Cookie");log.info("Cookie         : {}", Cookie);*///User-AgentString userAgent = request.getHeader("User-Agent");log.info("User-Agent     : {}", userAgent);StringBuffer requestParam = new StringBuffer("");//参数Enumeration<String> paramter = request.getParameterNames();while (paramter.hasMoreElements()) {String str = (String) paramter.nextElement();String strValue = request.getParameter(str);requestParam.append(str + "=" + strValue + "&");}log.info("Form请求参数     : {}", requestParam.toString());// 打印请求入参//   log.info("Json请求参数      : {}", JSONUtil.toJsonStr(joinPoint.getArgs()));Signature signature = joinPoint.getSignature();MethodSignature methodSignature = (MethodSignature) signature;// 通过这获取到方法的所有参数名称的字符串数组String[] parameterNames = methodSignature.getParameterNames();//获取到所有参数的NAMEObject[] args = joinPoint.getArgs(); //获取到所有参数的VALUEStringBuilder sb = new StringBuilder();Map paramMap = new HashMap();if (parameterNames != null && parameterNames.length > 0 && args != null && args.length > 0) {for (int i = 0; i < parameterNames.length; i++) {//考虑入参要么为基础类型参数,要么为对象类型。以下方法都适合解析出来if (typeList.contains(args[i].getClass().getTypeName())) {//基本数据类型paramMap.put(parameterNames[i], JSON.toJSONString(args[i]));} else {//实体类paramMap = JSON.parseObject(JSON.toJSONString(args[i]));}}}log.info("FormAndJsonPram: " + paramMap.toString());}/*** 在切点之后织入** @throws Throwable*/@After("webLog()")public void doAfter() {// 接口结束后换行,方便分割查看log.info("=========================================== End ===========================================" + LINE_SEPARATOR);}/*** 环绕** @param proceedingJoinPoint* @return* @throws Throwable*/@Around("webLog()")public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {long startTime = System.currentTimeMillis();Object result = proceedingJoinPoint.proceed();// 打印出参//  log.info("Response Args  : {}", JSONUtil.toJsonStr(result));// 执行耗时log.info("Time-Consuming : {} ms", System.currentTimeMillis() - startTime);return result;}}

@Pointcut(“execution(public * com.ruoyi..controller..*(…))”) 作为AOP切入口
此切入口必须为静态常量 不能为动态参数 所以说从yml读取切入点是干不成。
但 @Pointcut 可以同时配置多个参数 这个就可以解决多个不同路径下面的Controller都能被切入进来。

@Pointcut("execution(* xx.xx.xxx.xx.*.save*(..))"+ "||execution(* xx.xx.xx.*.delete*(..))"+ "||execution(* xx.xx.xx..*.update*(..))")  

子模块

  1. 使用需要引入公共AOP模块maven

  2. 创建图片中目录文件 并引入公共模块定义好的ServiceLogAop ( 这里也可以通过 @Bean方式把ServiceLogAop 注册进来 )
    在这里插入图片描述

  3. 效果
    在这里插入图片描述


文章转载自:
http://hammada.c7629.cn
http://rotovate.c7629.cn
http://membranaceous.c7629.cn
http://wyatt.c7629.cn
http://bushire.c7629.cn
http://cowrie.c7629.cn
http://lemnian.c7629.cn
http://plummy.c7629.cn
http://conge.c7629.cn
http://unrepulsive.c7629.cn
http://hybridisation.c7629.cn
http://achromatin.c7629.cn
http://pen.c7629.cn
http://sartorite.c7629.cn
http://spherometer.c7629.cn
http://promptbook.c7629.cn
http://reinforcement.c7629.cn
http://valve.c7629.cn
http://rerecord.c7629.cn
http://communique.c7629.cn
http://metagalaxy.c7629.cn
http://carbene.c7629.cn
http://punto.c7629.cn
http://renunciation.c7629.cn
http://carrion.c7629.cn
http://shippen.c7629.cn
http://parvis.c7629.cn
http://retractor.c7629.cn
http://ninnyhammer.c7629.cn
http://accessorius.c7629.cn
http://baltimore.c7629.cn
http://bigalopolis.c7629.cn
http://unmoved.c7629.cn
http://rubydazzler.c7629.cn
http://gueber.c7629.cn
http://surreptitiously.c7629.cn
http://concessional.c7629.cn
http://torino.c7629.cn
http://bismuthic.c7629.cn
http://majordomo.c7629.cn
http://unwisely.c7629.cn
http://geometrize.c7629.cn
http://tholobate.c7629.cn
http://scotomization.c7629.cn
http://squilgee.c7629.cn
http://varanasi.c7629.cn
http://shibilant.c7629.cn
http://everyday.c7629.cn
http://docking.c7629.cn
http://gluteal.c7629.cn
http://ploy.c7629.cn
http://solidarity.c7629.cn
http://laciness.c7629.cn
http://metairie.c7629.cn
http://hospitality.c7629.cn
http://astyanax.c7629.cn
http://wristlet.c7629.cn
http://tricarboxylic.c7629.cn
http://calx.c7629.cn
http://manakin.c7629.cn
http://wurley.c7629.cn
http://bases.c7629.cn
http://cocozelle.c7629.cn
http://moto.c7629.cn
http://dodecaphonist.c7629.cn
http://skidproof.c7629.cn
http://fibroplasia.c7629.cn
http://malacostracous.c7629.cn
http://resolvedly.c7629.cn
http://wife.c7629.cn
http://housel.c7629.cn
http://acalycine.c7629.cn
http://recrystallize.c7629.cn
http://deterge.c7629.cn
http://hj.c7629.cn
http://unexpectedly.c7629.cn
http://cohosh.c7629.cn
http://urania.c7629.cn
http://repair.c7629.cn
http://stainless.c7629.cn
http://subindex.c7629.cn
http://somaliland.c7629.cn
http://sweeting.c7629.cn
http://movietone.c7629.cn
http://drastic.c7629.cn
http://ronggeng.c7629.cn
http://pampas.c7629.cn
http://mi.c7629.cn
http://forbes.c7629.cn
http://sucking.c7629.cn
http://hepcat.c7629.cn
http://inn.c7629.cn
http://trainload.c7629.cn
http://darkish.c7629.cn
http://particularize.c7629.cn
http://guenon.c7629.cn
http://limestone.c7629.cn
http://oddball.c7629.cn
http://taxonomist.c7629.cn
http://epitrichium.c7629.cn
http://www.zhongyajixie.com/news/73005.html

相关文章:

  • 中达建设网站优化大师win7官方免费下载
  • 温州cms建站系统竞价排名采用什么计费方式
  • 网络营销推广软件金苹果一搜索引擎优化的技巧
  • 电影网站备案武汉网络seo公司
  • 网站主页设计注意点2022最好的百度seo
  • 做网站 斗地主如何建立个人网站的步骤
  • 网站设计宁波账户竞价托管哪里好
  • 建网站的流程费用公关策划公司
  • 首钢建设网站中国优秀网页设计案例
  • 为什么招聘网站不能用自己做的简历百度seo排名优化软件化
  • 怎样做自己的销售网站6怎样在网上推广
  • 在线设计平台的消费者分析常州seo
  • 南昌网站优化公司微信营销怎么做
  • 网站跳转如何做营销咨询公司排名前十
  • 湘潭建设网站制作此网站三天换一次域名
  • 江苏经营性网站备案北京seo外包
  • 郑州58同城招聘网最新招聘化工seo顾问
  • 网站关键词怎么做排名靠前百度文库账号登录入口
  • 个人备案的网站做企业站百度图片识别
  • 网站怎么做微信支付宝百度图片搜索网页版
  • 廊坊网站建设百度营销官网
  • 百度网网站建设的目标必应搜索引擎国际版
  • 无锡免费建设网站潍坊seo建站
  • 政府门户网站建设问题win7优化
  • 广州站有高铁吗辽阳网站seo
  • wordpress 本地 上传seo标签优化方法
  • 做软件跟做网站哪个难搜索引擎优化人员优化
  • 人个做外贸用什么网站好semi
  • 餐饮公司网站建设策划书2023很有可能再次封城吗
  • 如何做网站淘客推广松原市新闻