加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java注解使用

发布时间:2020-12-15 07:25:28 所属栏目:Java 来源:网络整理
导读:package com.casstime.ec.cloud.cart.infrastructure.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import o
package com.casstime.ec.cloud.cart.infrastructure.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import com.casstime.commons.service.exception.HttpMessageException;
import com.casstime.commons.utils.JsonUtil;

import lombok.extern.log4j.Log4j2;

/**
* 监控配置
*/
@Aspect
@Component
@Log4j2
public class MonitoringActuator {

/**
* 打印含有@Log注解的方法的方法名、入参、出参、执行时长
*/
@Around("@annotation(com.casstime.ec.cloud.cart.infrastructure.aspect.Log)")
public Object log(ProceedingJoinPoint joinPoint) throws Throwable {

String method = joinPoint.getSignature().getName();

log.info("method:{},args:{}",
() -> method,
() -> JsonUtil.serializer(joinPoint.getArgs()));

long begin = System.currentTimeMillis();

Object result = joinPoint.proceed();

log.info("method:{},execute times:{},result:{}",
() -> System.currentTimeMillis() - begin,
() -> JsonUtil.serializer(result));

return result;
}

/**
* 统一处理接口异常
*/
@AfterThrowing(throwing = "e",pointcut = "execution(public * com.casstime..ec.cloud.cart.interfaces.facade.*.*(..))")
public void handleException(JoinPoint joinPoint,Throwable e) {

if (e instanceof HttpMessageException) {
log.warn("{} error,() -> joinPoint.getSignature().getName(),() -> JsonUtil.serializer(joinPoint.getArgs()),() -> e);
}
}
}


package com.casstime.ec.cloud.cart.infrastructure.aspect;import static java.lang.annotation.ElementType.METHOD;import static java.lang.annotation.RetentionPolicy.RUNTIME;import java.lang.annotation.Retention;import java.lang.annotation.Target;/** * 在方法上加上该注解,可以打印入参、出参和方法执行时长 */@Retention(RUNTIME)@Target(METHOD)public @interface Log {}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读