自定义注解aop实现访问接口log打印

maven

        <!-- aop依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        //配置
  aop:
    proxy-target-class: true

1.自定义注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {
    String value() default "";
}

2.aop实现类

@Slf4j
@Aspect
@Component
public class LogAspect {

    @Resource
    private BaseProperties dsProperties;
    @Resource
    private LogService logService;

    @Around("@annotation(com.gc.jgdq.cowork.common.annotation.Log)")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        Object result = null;
        // 获取 request
        HttpServletRequest request = HttpContextUtil.getHttpServletRequest();
        String uri = request.getRequestURI();
        String method = request.getMethod();
        log.info("********************【" + uri + "】 请求开始 **************************");
        log.info("【请求路径】>>>> {}", uri);
        log.info("【请求方式】>>>> {}", method);
        log.info("【方法参数】>>>> {}", getFieldsName((JoinPoint) point));
        long beginTime = System.currentTimeMillis();
        try {
            // 执行方法
            result = point.proceed();
        } catch (Throwable e) {
            log.error(e.getMessage());
            throw e;
        }

        // 设置 IP 地址
        String ip = IpUtil.getIpAddr(request);
        // 执行时长(毫秒)
        long time = System.currentTimeMillis() - beginTime;

        if (dsProperties.isOpenAopLog()) {
            // 保存日志
            String token = (String) SecurityUtils.getSubject().getPrincipal();
            String username = JWTUtil.getUsername(token);

            SysLog log = new SysLog();
            log.setId(UIdUtil.getInstance().id());
            log.setUsername(username);
            log.setIp(ip);
            log.setTime(time);
            logService.saveLog(point, log);
        }

        log.info("【请求耗时】>>>> {} ms", time);
//        log.info("【响应结果】>>>> {}", result);
        log.info("********************【" + uri + "】 请求结束 **************************");
        return result;
    }

    private Map<String, Object> getFieldsName(JoinPoint joinPoint) throws ClassNotFoundException, NoSuchMethodException {
        String classType = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        // 参数值
        Object[] args = joinPoint.getArgs();
        Class<?>[] classes = new Class[args.length];
        for (int k = 0; k < args.length; k++) {
            // 如果方法参数出现null,直接返回null 不做处理
            if (args[k] == null) {
                return null;
            }
            // 对于接受参数中含有MultipartFile,ServletRequest,ServletResponse类型的特殊处理,我这里是直接返回了null。(如果不对这三种类型判断,会报异常)
            if (args[k] instanceof MultipartFile || args[k] instanceof ServletRequest || args[k] instanceof ServletResponse) {
                return null;
            }
            if (!args[k].getClass().isPrimitive()) {
                // 当方法参数是封装类型
                Class s = args[k].getClass();

                classes[k] = s == null ? args[k].getClass() : s;
            }
        }
        ParameterNameDiscoverer pnd = new DefaultParameterNameDiscoverer();
        // 获取指定的方法,第二个参数可以不传,但是为了防止有重载的现象,还是需要传入参数的类型
        Method method = Class.forName(classType).getMethod(methodName, classes);
        // 参数名
        String[] parameterNames = pnd.getParameterNames(method);
        // 通过map封装参数和参数值
        HashMap<String, Object> paramMap = new HashMap<>();
        assert parameterNames != null;
        for (int i = 0; i < parameterNames.length; i++) {
            paramMap.put(parameterNames[i], args[i]);
        }
        return paramMap;
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOP可以使用AspectJ注解来实现接口重复调用的拦截。假设我们有一个接口`UserService`,其中有一个`saveUser`方法,我们希望在该方法被调用时进行拦截,防止重复调用。我们可以定义一个切面类`DuplicateInvokeAspect`,并为其添加一个`@Around`注解的方法,该方法将拦截`UserService`的`saveUser`方法,并在该方法中添加重复调用判断逻辑。 ``` @Aspect public class DuplicateInvokeAspect { private Map<String, Long> lastInvokeMap = new ConcurrentHashMap<>(); @Around("execution(* com.example.UserService.saveUser(..))") public Object checkDuplicateInvoke(ProceedingJoinPoint joinPoint) throws Throwable { String methodName = joinPoint.getSignature().getName(); if (lastInvokeMap.containsKey(methodName)) { long lastInvokeTime = lastInvokeMap.get(methodName); long currentTime = System.currentTimeMillis(); if (currentTime - lastInvokeTime < 5000) { // 限制5秒内不能重复调用 throw new RuntimeException("不能重复调用" + methodName); } } lastInvokeMap.put(methodName, System.currentTimeMillis()); return joinPoint.proceed(); } } ``` 在上述代码中,我们使用`ConcurrentHashMap`来存储方法名和上一次调用时间的映射关系。在`checkDuplicateInvoke`方法中,我们首先判断该方法是否已经在`lastInvokeMap`中存在,如果存在则判断距离上一次调用的时间是否已经超过了5秒,如果没有超过则抛出异常,否则将当前时间更新为上一次调用时间,并继续执行原有的方法逻辑。 最后,我们需要在Spring配置文件中启用该切面类: ``` <aop:aspectj-autoproxy/> <bean id="duplicateInvokeAspect" class="com.example.DuplicateInvokeAspect"/> ``` 这样就完成了接口重复调用的拦截功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值