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


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

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

    一、以日志记录为例

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

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

    52e77c79b07d49c6554ff2a0185d7f02.png52e77c79b07d49c6554ff2a0185d7f02.png

    二、了解关于注解知识

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

    以 @Controller 为例,其源码也是如此:

    fc6b30adb15c78f562e0de8e503e6881.pngfc6b30adb15c78f562e0de8e503e6881.png

    我们来看一下上边提到的四个注解:

    注解说明
    @Target 用于描述注解的使用范围,即:被描述的注解可以用在什么地方
    @Retention 指定被描述的注解在什么范围内有效
    @Documented 是一个标记注解,木有成员,用于描述其它类型的annotation 应该被作为被标注的程序成员的公共 API,因此可以被例如javadoc此类的工具文档化
    @Inherited 元注解是一个标记注解,@Inherited 阐述了某个被标注的类型是被继承的。如果一个使用了 @Inherited 修饰的 annotation 类型被用于一个 class,则这个 annotation 将被用于该class的子类

    三、开始我们的自定义注解

    两个类:
    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.PARAMETERElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface SystemLog {

            /**
             * 日志名称
             * @return
             */

            String description() default "";

            /**
             * 日志类型
             * @return
             */

            LogType type() default LogType.OPERATION;
    }
    2、@SystemLogAspect

    AOP拦截@SystemLog注解

    /**
     * Spring AOP实现日志管理
     * @author Exrickx
     */

    @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(); 等等。

    如果文章有错的地方欢迎指正,大家互相留言交流。习惯在微信看技术文章,想要获取更多的Java资源的同学,可以关注微信公众号:niceyoo

  • 相关阅读:
    mysql 的远程链接字符
    SqlSessionFactoryUtil
    mysql 的链接字符
    web 项目运用通用的xml配置
    配置文件转意符使用
    new和newInstance的区别
    子选择器与后代选择器的区别
    Vivado_HLS 学习笔记1-数据类型
    Vivado_HLS 学习笔记3-循环的pipeline与展开
    Vivado_HLS 学习笔记2-接口综合
  • 原文地址:https://www.cnblogs.com/niceyoo/p/10907203.html
Copyright © 2020-2023  润新知