LocalDateTime start = LocalDateTime.now(); object = pjp.proceed(); LocalDateTime end = LocalDateTime.now(); stringBuilder.append("方法开始时间:" + start + "ms"); stringBuilder.append("方法结束时间:" + end + "ms"); stringBuilder.append("运行耗时:" + (end - start) + "ms");
时间: 2024-02-26 10:55:15 浏览: 62
这段代码是什么意思呢?
这段代码是一个使用AOP编程的方法拦截器,在方法执行前记录方法开始时间,在方法执行后记录方法结束时间,然后计算方法运行的耗时,并将这些信息记录到一个StringBuilder对象中。其中,LocalDateTime是Java 8中新增的日期和时间API,用于表示本地日期和时间。pjp.proceed()是执行被拦截的方法,并返回结果。拦截器可以用于实现日志记录、性能统计、安全控制等功能。
相关问题
@Around("pointCutIAntiResubmit(iAntiResubmit)") public Object aroundAdvice(ProceedingJoinPoint pjp, IAntiResubmit iAntiResubmit) { String key = loadKey(iAntiResubmit.key(), pjp); String redisKey = RedisConstants.ANTI_RESUBMIT_REDIS_KEY + RedisConstants.SEPARATOR + getValue(pjp, iAntiResubmit) + "::" + key; RLock lock = redissonClient.getLock(redisKey); try { Object[] args = pjp.getArgs();// 得到方法执行所需的参数 lock.lock(); return pjp.proceed(args);// 明确调用切入点方法(切入点方法) } catch (SysException se) { throw se; } catch (Throwable e) { throw new SysException(e.getMessage()); } finally { lock.unlock(); } }这里的iAntiResubmit.key()是不是为空
如果IAntiResubmit注解被正确地使用,并且在使用该注解时为key成员变量显式地赋值,那么iAntiResubmit.key()应该是非空的。
在上面的代码中,通过iAntiResubmit.key()获取IAntiResubmit注解的key成员变量的值,并将其作为loadKey方法的第一个参数。如果IAntiResubmit注解的key成员变量没有显式地赋值,那么iAntiResubmit.key()将返回空字符串,但是在loadKey方法中,空字符串会被视为无效的key值,可能会导致程序出错。
因此,在使用IAntiResubmit注解时,应该为其key成员变量显式地赋值,以确保程序能够正确地执行。
下面这段代码是什么意思@Pointcut("@annotation(com.tplinknbu.paymentcenter.deprecated.annotation.LogRequired)") public void logPointcut() { // 切点定义 } @Around("logPointcut()") public Object handlerStat(ProceedingJoinPoint pjp) throws Throwable { MethodSignature signature = (MethodSignature) pjp.getSignature(); Method method = signature.getMethod(); ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); Set<Entry<String, String[]>> entries = getHttpRequestPrameterSet(requestAttributes); Map<Object, Object> parameterMap = new HashMap<>(); for (final Entry<String, String[]> entry : entries) { parameterMap.put(entry.getKey(), Arrays.toString(entry.getValue())); } Object response = null; long startAtNs = systemTime.getMilliseconds(); try { response = pjp.proceed(); } finally { List<Object> requestArgs = new ArrayList<>(); Arrays.asList(pjp.getArgs()).forEach(obj -> { if (obj instanceof MultipartFile) { // } else if (obj instanceof HttpServletRequest || obj instanceof HttpServletResponse || obj instanceof ServletRequest || obj instanceof ServletResponse) { // mapper.valueToTree 无法转化 HttpServletRequest } else { requestArgs.add(obj); } }); long durationInMills = systemTime.getMilliseconds() - startAtNs; toJsonPoint(method.getName(), requestArgs, parameterMap, response, durationInMills); } return response; }
这段代码是一个基于 Spring AOP 的切面,用于记录某些方法的运行时间、参数、返回结果等信息。首先,通过 @Pointcut 注解定义一个切点,该切点的条件是被 @LogRequired 注解标注的方法。然后,使用 @Around 注解定义一个环绕通知,该通知会在切点处切入,执行被切入的方法。在通知中,首先记录请求参数,然后执行被切入的方法,并记录执行时间和返回结果,最后将这些信息序列化成 JSON 格式,输出到日志中。需要注意的是,该切面对 HttpServletRequest、HttpServletResponse 等对象不做处理。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![java](https://img-home.csdnimg.cn/images/20250102104920.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)