import java.lang.reflect.Modifier; import java.util.HashMap; import java.util.Map; import javassist.ClassClassPath; import javassist.ClassPool; import javassist.CtClass; import javassist.CtMethod; import javassist.bytecode.CodeAttribute; import javassist.bytecode.LocalVariableAttribute; import javassist.bytecode.MethodInfo; import javax.servlet.http.HttpServletRequest; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import com.glodon.bim5d.controller.BaseController; import com.glodon.bim5d.util.LoggerMsgUtil; @Aspect @Component public class LogAspect { @Pointcut("execution(public void xxx.xxx.xxx.controller.QRCodeController.download(..))") public void pointCut() { } @Before("pointCut()") public void before(JoinPoint joinPoint) throws Exception { //获得代理类 BaseController controller = (BaseController)joinPoint.getTarget(); //获得request HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); String uri = request.getRequestURI(); String queryString = request.getQueryString(); String projectId = getProjectId(joinPoint); try { HashMap<String, Object> inputParam = new HashMap<String, Object>(); inputParam.put("uri", uri); inputParam.put("projectId", projectId); inputParam.put("RequestBody", queryString); LoggerMsgUtil.enterMethodInfo(controller.getLogger(), inputParam); } catch (Exception e) { LoggerMsgUtil.Info(controller.getLogger(), "记录入口参数失败:{0}", e.getMessage()); } } /** * 从参数列表中获取projectId * */ private String getProjectId(JoinPoint joinPoint){ String projectId = null; try { //获取参数名称和值 Map<String,Object> nameAndArgs = getFieldsNameValueMap(joinPoint); projectId = nameAndArgs.get("projectId") == null ? null : String.valueOf(nameAndArgs.get("projectId")); } catch (Exception e) { } return projectId; } private Map<String,Object> getFieldsNameValueMap(JoinPoint joinPoint) throws Exception { Object[] args = joinPoint.getArgs(); String classType = joinPoint.getTarget().getClass().getName(); Class<?> clazz = Class.forName(classType); String clazzName = clazz.getName(); String methodName = joinPoint.getSignature().getName(); //获取方法名称 Map<String,Object > map=new HashMap<String,Object>(); ClassPool pool = ClassPool.getDefault(); ClassClassPath classPath = new ClassClassPath(this.getClass()); pool.insertClassPath(classPath); CtClass cc = pool.get(clazzName); CtMethod cm = cc.getDeclaredMethod(methodName); MethodInfo methodInfo = cm.getMethodInfo(); CodeAttribute codeAttribute = methodInfo.getCodeAttribute(); LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag); if (attr == null) { throw new RuntimeException(); } int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1; for (int i = 0; i < cm.getParameterTypes().length; i++){ map.put( attr.variableName(i + pos),args[i]);//paramNames即参数名 } return map; } }
spring aop 获得参数名称和值得map
转载文章标签 spring aop 获得参数名称和值得 文章分类 Java 后端开发
上一篇:element-io、vue简介
下一篇:post方式下载
-
aop中获取request aop获取请求参数和值
AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待。一 AOP的基本概念Aspect(切面):通常是一个类,里面可以定义切入点和通知JointPoint(连接点):程序执行过程中明确的点,一般是方法的调用Advice(通知):AOP在特定的切入点上执行的增强处理,有before,after,afterReturning,afterThrowing,aro
aop中获取request AOP springAOP SpringAOP处理request springAOP统计请求时间