addr是什么软件?自定义注解+AOP记录访问日志addr是什么程序,ADD是什么软件

1、问题描述AOP拦截请求,记录请求参数、返回参数及响应时间,以前已经介绍过了,但是发现有个小问题,就是有些心跳或者仅仅是查询字典下拉框的请求,本身不用记录,排除的话,需要一个一个排除,有点麻烦,就用自定义注解对aop记录请求与返回做了个下改进,简单记录下。2、解决方案简单说就是新建个自定义注解标签,然后对需要记录日志的的方法加上标签就可以了。2.1 自定义标签import java.lang.annotation.*; @documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) public @interface WebLogAnotation { } 简单说明: @Target({ElementType.METHOD}),这个是定义使用范围;@Target:注解的作用目标         @Target(ElementType.TYPE) //接口、类、枚举、注解 @Target(ElementType.FIELD) //字段、枚举的常量 @Target(ElementType.METHOD) //方法 @Target(ElementType.PARAMETER) //方法参数 @Target(ElementType.CONSTRUCTOR) //构造函数 @Target(ElementType.LOCAL_VARIABLE)//局部变量 @Target(ElementType.annotation_TYPE)//注解 @Target(ElementType.PACKAGE) ///包 2.2 修改AOP中的拦截方法import com.alibaba.fastjson.JSON; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; 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.lang.reflect.Method; @Aspect @Component public class WebLogAspect { //@Pointcut("execution(public * com.spring.wx.oauth.conntroller.*.*(..))") @Pointcut("@annotation(com.spring.wx.oauth.utils.WebLogAnotation)") public void webLog(){ } //@Around:环绕通知 @Around("webLog()") public Object saveSysLog(ProceedingJoinPoint proceedingJoinPoint) { System.out.println("环绕通知开始。。。。。"); MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature(); Method method = signature.getMethod(); String className = proceedingJoinPoint.getTarget().getClass().getName(); String methodName = method.getName(); System.out.println(className); System.out.println(methodName); System.out.println(className + "." + methodName); //请求的参数 Object[] args = proceedingJoinPoint.getArgs(); String params = JSON.toJSonString(args); System.out.println(params); ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); // 记录下请求内容 System.out.println("URL : " + request.getRequestURL().toString()); System.out.println("HTTP_METHOD : " + request.getMethod()); System.out.println("IP : " + request.getRemoteAddr()); //记录时间 long start = System.currentTimeMillis(); Object result =null; try { result = proceedingJoinPoint.proceed(); System.out.println(result.toString()); } catch (Throwable throwable) { throwable.printStackTrace(); System.out.println(throwable.getMessage()); } Long time = System.currentTimeMillis() - start; System.out.println(time); System.out.println("环绕通知结束。。。。。"); return result; } } AOP拦截的前面已经介绍过好几次,就不多说了,修改以下这行,其中pointcut里面是楼上定义的注解方法; @Pointcut("@annotation(com.spring.wx.oauth.utils.WebLogAnotation)") public void webLog(){ } 2.3 Controller使用自定义注解import com.spring.wx.oauth.utils.WebLogAnotation; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/rest") public class RestConntroller { @WebLogAnotation @RequestMapping("/weblog") public String weblog() { return "软件老王欢迎你!"; } } 其中关键是 @WebLogAnotation这个注解,只有添加了这个注解的请求才会走到AOP记录日志方法;2.4 验证当通过页面请求weblog方法的时候,就会进入AOP拦截方法记录日志;
更多信息请关注@软件老王,关注不迷路,软件老王和他的IT朋友们,分享一些他们的技术见解和生活故事。

本文经授权 由答答网发布,转载联系作者并注明出处:http://www.dadazzz.com:6443/sh/show-83851.html

如对文章、图片、字体等版权有疑问,请联系我们。

相关推荐

企业微信
运营大叔公众号