自定义注解-aop实现日志记录

关于注解,平时接触的可不少,像是 @Controller、@Service、@Autowried 等等,不知道你是否有过这种疑惑,使用 @Service 注解的类成为我们的业务类,使用 @Controller 注解的类就成了请求的控制器,使用 @Autowried 注解的类就会帮我们实现自动注入…

以前,我们只知道使用注解,今天我们要手写一个注解。

一、以日志记录为例
在没有使用注解实现记录日志之前,我们往往自己去调用日志记录的 Service,然后写入数据库表。

今天我们将从方法上添加自定义注解实现日志自动记录,如下:

在这里插入图片描述

二、了解关于注解知识
JDK 提供了 meta-annotation 用于自定义注解的时候使用,这四个注解为:@Target,@Retention,@Documented 和 @Inherited。

以 @Controller 为例,其源码也是如此:
在这里插入图片描述

我们来看一下上边提到的四个注解:
在这里插入图片描述

三、开始我们的自定义注解
两个类:
SystemLog:自定义注解类,用于标记到方法、类上,如@SystemLog
SystemLogAspect:AOP实现切点拦截。

关于AOP的补充:
关于AOP面向切面编程概念啥的就不啰嗦了,还不了解的可以自定百度了
描述AOP常用的一些术语有:
通知(Adivce)、连接点(Join point)、切点(Pointcut)、切面(Aspect)、引入(Introduction)、织入(Weaving)

关于术语的部分可参考:https://www.cnblogs.com/niceyoo/p/10162077.html

需要明确的核心概念:切面 = 切点 + 通知。

@Aspect 注解形式是 AOP 的一种实现,如下看一下我们要写的两个类吧。

1、@SystemLog
定义我们的自定义注解类

/**
 * 系统日志自定义注解
 */
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SystemLog {

        /**
         * 日志名称
         * @return
         */
        String description() default "";

        /**
         * 日志类型
         * @return
         */
        LogType type() default LogType.OPERATION;
}

2、@SystemLogAspect
AOP拦截@SystemLog注解

/**
 * Spring AOP实现日志管理
 */
@Aspect
@Component
@Slf4j
public class SystemLogAspect {

    private static final ThreadLocal<Date> beginTimeThreadLocal = new NamedThreadLocal<Date>("ThreadLocal beginTime");

    @Autowired
    private LogService logService;

    @Autowired
    private UserService userService;

    @Autowired(required = false)
    private HttpServletRequest request;

    /**
	 * 定义切面,只置入带 @SystemLog 注解的方法或类 
     * Controller层切点,注解方式
	 * @Pointcut("execution(* *..controller..*Controller*.*(..))")
     */
    @Pointcut("@annotation(club.sscai.common.annotation.SystemLog)")
    public void controllerAspect() {

    }

    /**
     * 前置通知 (在方法执行之前返回)用于拦截Controller层记录用户的操作的开始时间
     * @param joinPoint 切点
     * @throws InterruptedException
     */
    @Before("controllerAspect()")
    public void doBefore(JoinPoint joinPoint) throws InterruptedException{

        ##线程绑定变量(该数据只有当前请求的线程可见)
        Date beginTime=new Date();
        beginTimeThreadLocal.set(beginTime);
    }


    /**
     * 后置通知(在方法执行之后并返回数据) 用于拦截Controller层无异常的操作
     * @param joinPoint 切点
     */
    @AfterReturning("controllerAspect()")
    public void after(JoinPoint joinPoint){
        try {
            String username = "";
            String description = getControllerMethodInfo(joinPoint).get("description").toString();
            Map<String, String[]> logParams = request.getParameterMap();
            String principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
            ## 判断允许不用登录的注解
            if("anonymousUser".equals(principal)&&!description.contains("短信登录")){
                return;
            }
            if(!"anonymousUser".equals(principal)){
                UserDetails user = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                username = user.getUsername();
            }
            if(description.contains("短信登录")){
                if(logParams.get("mobile")!=null){
                    String mobile = logParams.get("mobile")[0];
                    username = userService.findByMobile(mobile).getUsername()+"("+mobile+")";
                }
            }
            
			Log log = new Log();

			##请求用户
			log.setUsername(username);
			##日志标题
			log.setName(description);
			##日志类型
			log.setLogType((int)getControllerMethodInfo(joinPoint).get("type"));
			##日志请求url
			log.setRequestUrl(request.getRequestURI());
			##请求方式
			log.setRequestType(request.getMethod());
			##请求参数
			log.setMapToParams(logParams);
			##请求开始时间
			Date logStartTime = beginTimeThreadLocal.get();

			long beginTime = beginTimeThreadLocal.get().getTime();
			long endTime = System.currentTimeMillis();
			##请求耗时
			Long logElapsedTime = endTime - beginTime;
			log.setCostTime(logElapsedTime.intValue());

			##调用线程保存至log表
			ThreadPoolUtil.getPool().execute(new SaveSystemLogThread(log, logService));
            
        } catch (Exception e) {
            log.error("AOP后置通知异常", e);
        }
    }


    /**
     * 保存日志至数据库
     */
    private static class SaveSystemLogThread implements Runnable {

        private Log log;
        private LogService logService;

        public SaveSystemLogThread(Log esLog, LogService logService) {
            this.log = esLog;
            this.logService = logService;
        }

        @Override
        public void run() {

            logService.save(log);
        }
    }

    /**
     * 获取注解中对方法的描述信息 用于Controller层注解
     * @param joinPoint 切点
     * @return 方法描述
     * @throws Exception
     */
    public static Map<String, Object> getControllerMethodInfo(JoinPoint joinPoint) throws Exception{

        Map<String, Object> map = new HashMap<String, Object>(16);
        ## 获取目标类名
        String targetName = joinPoint.getTarget().getClass().getName();
        ## 获取方法名
        String methodName = joinPoint.getSignature().getName();
        ## 获取相关参数
        Object[] arguments = joinPoint.getArgs();
        ## 生成类对象
        Class targetClass = Class.forName(targetName);
        ## 获取该类中的方法
        Method[] methods = targetClass.getMethods();

        String description = "";
        Integer type = null;

        for(Method method : methods) {
            if(!method.getName().equals(methodName)) {
                continue;
            }
            Class[] clazzs = method.getParameterTypes();
            if(clazzs.length != arguments.length) {
                ## 比较方法中参数个数与从切点中获取的参数个数是否相同,原因是方法可以重载
                continue;
            }
            description = method.getAnnotation(SystemLog.class).description();
            type = method.getAnnotation(SystemLog.class).type().ordinal();
            map.put("description", description);
            map.put("type", type);
        }
        return map;
    }

}

流程补充:
1.通过 @Pointcut 定义带有 @SystemLog 注解的方法或类为切入点,可以理解成,拦截所有带该注解的方法。
2.@Before 前置通知用于记录请求时的时间
3.@AfterReturning 用于获取返回值,主要使用 getControllerMethodInfo() 方法,采用类反射机制获取请求参数,最后调用 LogService 保存至数据库。

额外补充:

关于 SecurityContextHolder 的使用为 Spring Security 用于获取用户,实现记录请求用户的需求,可根据自己框架情况选择,如使用 shiro 获取当前用户为 SecurityUtils.getSubject().getPrincipal(); 等等。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,我们可以使用自定义注解实现AOP(面向切面编程)。AOP是一种编程范型,它允许开发者在程序运行时动态地将代码切入到已有代码的特定位置。 下面是一个简单的示例,演示如何使用自定义注解实现AOP。 首先,我们需要定义一个自定义注解: ``` @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Loggable { } ``` 这个注解用来标记需要记录日志的方法。它的@Target注解指定了它只能用于方法上,@Retention注解指定了它的生命周期是运行时。 接下来,我们创建一个切面类,用来实现AOP的逻辑: ``` @Aspect @Component public class LoggingAspect { @Before("@annotation(com.example.Loggable)") public void logMethodCall(JoinPoint joinPoint) { String methodName = joinPoint.getSignature().getName(); System.out.println("Method " + methodName + " called"); } } ``` 这个类使用Spring AOP框架提供的@Aspect注解来标记它是一个切面类。它的@Before注解指定了它要在被@Loggable注解标记的方法之前执行。JoinPoint参数包含了被拦截的方法的信息,我们可以从中获取方法名等信息。 最后,在需要记录日志的方法上加上@Loggable注解即可: ``` @Component public class MyService { @Loggable public void doSomething() { // do something } } ``` 当doSomething()方法被调用时,LoggingAspect中的logMethodCall()方法会被执行,记录方法调用信息。 这就是使用自定义注解实现AOP的基本步骤。当然,实际应用中会更加复杂,需要更多的切面逻辑和注解参数等。但是这个简单的示例可以帮助你理解如何使用自定义注解实现AOP

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值