美文网首页
springboot 自定义注解实现

springboot 自定义注解实现

作者: 良人与我 | 来源:发表于2019-04-02 15:28 被阅读0次

自定义注解,一般在日志 或者 想要统计特定的接口信息时候 使用的比较多。

/**
 * @author river
 * @date 2019/4/2 10:35
 **/
@Documented
@Retention(RUNTIME)
@Target(ElementType.METHOD)
public @interface UserOprLog {
    String value() default "用户操作日志注解";
    int type() default 0;
}

添加 aop 处理添加了注解了请求

/**
 * 打印日志
 *
 * @author river
 */
@Slf4j
@Aspect
@Component
public class UserOprLogAop {

    @Around("@annotation(userOprLog)")
    public void controllerLog(ProceedingJoinPoint pjp, UserOprLog userOprLog) throws Throwable {

        pjp.proceed();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        System.out.println(new Gson().toJson(request.getParameterMap()));
        log.info("args = " + new Gson().toJson(pjp.getArgs()));

        System.out.println(request.getRequestURI());
        System.out.println(userOprLog.type());
        System.out.println(userOprLog.value());
    }
}

这样在需要输出日志的地方 添加 @UserOprLog 就可以了。

相关文章

网友评论

      本文标题:springboot 自定义注解实现

      本文链接:https://www.haomeiwen.com/subject/wkiwbqtx.html