springboot自定义注解带参,添加日志功能

自定义注解代码:

import java.lang.annotation.*;


@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Xxxx{
    /**
     * 模块名称
     *
     * @return
     */
    String modelName() default "";

    /**
     * 功能名称
     *
     * @return
     */
    String methodName() default "";
}

配合对接AOP代码:

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.Arrays;


@Aspect
@Component
@Slf4j
public class XxxxAop {

    @Pointcut("@annotation(自定义注解路径)")
    public void Xxxx() {
    }

    @Before("Xxxx()")
    public void doBefore(JoinPoint joinPoint) throws Exception {
        // 方法描述
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method methodFromClass = joinPoint.getSignature()
			.getDeclaringType()
			.getDeclaredMethod(joinPoint.getSignature().getName(), signature.getParameterTypes());
        TempLog des = methodFromClass.getDeclaredAnnotation(自定义注解名.class);
        String[] parameterNames = signature.getParameterNames();
        Object[] args = joinPoint.getArgs();
        String userId = "", ecCode = "", params = "";
        for (Object arg : args) {
            if (arg instanceof SessionMap) {
			    //解析用户信息
                userId = ((SessionMap) arg).getSsoTokenInfo().getUserId();
                ecCode = ((SessionMap) arg).getSsoTokenInfo().getEcCode();
                params += "用户信息,";
            } else if (arg instanceof MultipartFile) {
                params = params + ((MultipartFile) arg).getOriginalFilename() + ",";
            } else if (arg instanceof HttpServletResponse) {
                params = params + "HttpServletResponse ,";
            } else if (arg instanceof HttpServletRequest) {
                params = params + "HttpServletRequest ,";
            } else {
                if (arg == null || arg.equals("")) {
                    params = params + "null,";
                } else {
                    params = params + JSON.toJSON(arg) + ",";
                }
            }
        }
        log.info("|#{} |#{} |#用户 EC_CODE = {} , USER_ID = {} |# 参数名称 {} ,参数 [{}]"
                , des.modelName()
                , des.methodName()
                , ecCode
                , userId
                , Arrays.toString(parameterNames)
                , params);
    }

}

使用自定义注解:

    @TempLog(modelName = ".....", methodName = ".....")
    @RequestMapping(value = "/A", method = RequestMethod.POST)
    public Result A(@RequestBody SessionMap sessionMap) {
       
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值