小傻羊7.31学习笔记——AOP例子和日志的实现实例

7.31学习笔记——AOP例子和日志的实现实例

  • 转载小栗子

    https://blog.csdn.net/zhuzhezhuzhe1/article/details/80565067

  • 切口的实现,sysLogService.insertLog(sysOplog)调用;

    package com.cdeledu.yunqi.framework.aspectj;
    
    import com.alibaba.fastjson.JSON;
    import com.cdeledu.yunqi.common.utils.ServletUtils;
    import com.cdeledu.yunqi.framework.aspectj.lang.annotation.SysLog;
    import com.cdeledu.yunqi.framework.security.LoginUser;
    import com.cdeledu.yunqi.framework.security.service.TokenService;
    import com.cdeledu.yunqi.system.model.SysOplog;
    import com.cdeledu.yunqi.system.service.impl.SysLogService;
    import com.cdeledu.yunqi.system.model.SysUser;
    import lombok.extern.slf4j.Slf4j;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    import org.aspectj.lang.reflect.MethodSignature;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    /**
     * 系统日志切面
     *
     * @author linke
     */
    @Aspect  // 使用@Aspect注解声明一个切面
    @Component
    @Slf4j
    public class SysLogAspect {
    
        @Autowired
        private SysLogService sysLogService;
    
        @Autowired
        private TokenService tokenService;
    
    //    @Pointcut("@annotation(com.space.aspect.anno.SysLog)")
        @Pointcut("execution(* com.cdeledu.yunqi.biz.controller.*.*(..))")
        public void logPointCut() {}
    
        /**
         * 环绕通知 @Around
         * @param point
         * @return
         * @throws Throwable
         */
        @Around("logPointCut()")
        public Object around(ProceedingJoinPoint point) throws Throwable {
            long beginTime = System.currentTimeMillis();
            Date date = new Date(beginTime);
            Object result = point.proceed();
            long endtime = System.currentTimeMillis() ;
            long time =endtime-beginTime;
            try {
                saveLog(point, time, date);
            } catch (Exception e) {
            }
            return result;
        }
    
        /**
         * 保存日志
         * @param joinPoint
         * @param time
         */
        private void saveLog(ProceedingJoinPoint joinPoint, long time,Date date) {
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            SysUser user = loginUser.getUser();
            String userName = user.getUserName();
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            Method method = signature.getMethod();
            SysOplog sysOplog = new SysOplog();
            sysOplog.setUserName(userName);
            sysOplog.setCreateTime(new Date());
            sysOplog.setVisitTime(date);
            sysOplog.setOpDurtime(time);
    
            SysLog sysLog = method.getAnnotation(SysLog.class);
            if(sysLog != null){
                //注解上的描述
    //            sysLogdata.setRemark(sysLog.value());
                String message = sysLog.message();
                String s = sysLog.bizName();
                sysOplog.setBizName(s);
                sysOplog.setMessage(message);
            }
            //请求的 类名、方法名
            String className = joinPoint.getTarget().getClass().getName();
            String methodName = signature.getName();
            sysOplog.setClassName(className);
            sysOplog.setMethodName(methodName);
            //请求的参数
            Object[] args = joinPoint.getArgs();
            try{
                List<String> list = new ArrayList<String>();
                for (Object o : args) {
                    try{
                        list.add(JSON.toJSONString(o));
                    }catch (Exception e){
                        log.error("json解析异常",e);
                    }
                }
                sysOplog.setParams(list.toString());
    
                sysLogService.insertLog(sysOplog);
            }catch (Exception e){
                log.error("保存行为日志失败",e);
            }
    
        }
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值