Log interception

I have studied a log interception in my spare time,

package com.rxyb.web.annotation;

import java.lang.annotation.*;

@Target(ElementType.METHOD)//作用于方法、不能是构造方法
@Retention(RetentionPolicy.RUNTIME)//在运行时可以通过反射获取到,jvm会读取注解,同时保存在class文件中
@Documented//说明该注解将被包含在javadoc中
public @interface LogAnnotation {
    public String moudle() default "";
}
package com.rxyb.web.controller;


import com.rxyb.web.annotation.LogAnnotation;
import com.rxyb.web.db.po.XbUser;
import com.rxyb.web.service.XbUserService;
import lombok.extern.java.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lcf
 * @since 2020-07-31
 */
@Controller
@RequestMapping("/xbUser")
public class XbUserController {

    private static final Logger log= LoggerFactory.getLogger(XbUserController.class);

    @Autowired
    private XbUserService xbUserService;

    @LogAnnotation(moudle = "test")
    @RequestMapping("query")
    @ResponseBody
    public void queryAll(){
        List<XbUser> list = xbUserService.queryAll();
        System.out.println("查询所有数据:"+list);
        XbUser user = xbUserService.queryById(1);
        System.out.println("根据id查询结果为:"+user);
        XbUser info=new XbUser();
        info.setUsername("test");
        XbUser xbUser = xbUserService.queryUserInfo(info);
        System.out.println("根据条件查询:"+xbUser);
    }
}

 

package com.rxyb.web.annotation;

import com.rxyb.web.db.po.SysLog;
import com.rxyb.web.service.SysLogService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 日志切面
 *
 * @author lcf
 * @date 2020-08-04-10:33
 */
@Component //加入到spring容器
@Aspect //切面
public class LogAspect {

    @Autowired
    private SysLogService sysLogService;

    @Around(value = "@annotation(com.rxyb.web.annotation.LogAnnotation)")
    public  void saveLog(ProceedingJoinPoint point) throws Throwable {
        SysLog sysLog=new SysLog();
        sysLog.setUserId(1);
        sysLog.setAddTime(Long.parseLong(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))));
        sysLog.setContext("test");
        sysLog.setUserId(1);
        MethodSignature signature = (MethodSignature)point.getSignature();
        LogAnnotation logAnnotation = signature.getMethod().getDeclaredAnnotation(LogAnnotation.class);
        String moudle = logAnnotation.moudle();
        System.out.println("moudle:"+moudle);
        sysLog.setOperate(moudle);
        Object[] args = point.getArgs();
        System.out.println("方法执行之前。。。。。。。。。。。。。。。。。");
        //注意,调用point.proceed(args);则修改的参数值不会生效,必须调用point.proceed(Object[] args);
        point.proceed(args);
        sysLog.setRemark("");
        System.out.println("方法执行之后。。。。。。。。。。。。。。。。。");
       // sysLogService.insertLog(sysLog);

    }
}

 

Guess you like

Origin blog.csdn.net/shuoshuo_12345/article/details/108535238
log
log