基于AOP的Log

基于AOP的Log日志开发

数据库

在这里插入图片描述

sql语句

CREATE TABLE sysLog( 
		id VARCHAR2(32) default SYS_GUID() PRIMARY KEY, 
		visitTime timestamp, username VARCHAR2(50), 
		ip VARCHAR2(30), 
		url VARCHAR2(50), 
		executionTime int, 
		method VARCHAR2(200) 
) 
public class SysLog {
    private String id;
    private Date visitTime;
    private String visitTimeStr;
    private String username;
    private String ip;
    private String url;
    private Long executionTime;
    private String method;
}

基于AOP日志处理

@Component
@Aspect
public class LogAop {

    @Autowired
    private SysLogService sysLogService;

    @Autowired
    private HttpServletRequest request;

    private Date visitTime;//开始时间
    private Class clazz;//方法的类
    private Method method;//访问的方法

    /**
     * 前置通知,获取开始的时间,执行的类是哪一个,执行的是哪一个方法
     *
     * @param joinPoint
     */
    @Before("execution(* controller.*.*(..))")
    public void doBefore(JoinPoint joinPoint) throws NoSuchMethodException {
        visitTime = new Date();
        clazz = joinPoint.getTarget().getClass();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();

        //获取具体执行的method对象
        if (args == null || args.length == 0) {
            method = clazz.getMethod(methodName);//只能获取无参数的方法
        } else {
            Class[] classArgs = new Class[args.length];
            for (int i = 0; i < args.length; i++) {
                classArgs[i] = args[i].getClass();
            }
            clazz.getMethod(methodName, classArgs);
        }
    }

    /**
     * 后置通知
     *
     * @param joinPoint
     */
    @After("execution(* controller.*.*(..))")
    public void doAfter(JoinPoint joinPoint) {
        long time = new Date().getTime() - visitTime.getTime();//获取访问时长

        String url = "";

        //获取url
        //就是这的问题
        if (clazz != null && method != null &&clazz != LogAop.class) {
            //1.获取类上的@RequestMapping
            RequestMapping annotations = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
            if (annotations != null) {
                //获取到了类的url
                String[] classValue = annotations.value();
                //获取方法上的RequestMapping
                RequestMapping methodAnnotation = method.getAnnotation(RequestMapping.class);
                if (methodAnnotation != null) {
                    String[] methodValue = methodAnnotation.value();
                    url = classValue[0] + methodValue[0];
                    //获取访问的ip地址
                    String ip = request.getRemoteAddr();
                    //获取当前操作的用户
                    SecurityContext context = SecurityContextHolder.getContext();
                    User user = (User) context.getAuthentication().getPrincipal();
                    String username = user.getUsername();
                    //将日志相关信息封装到SysLog
                    SysLog sysLog = new SysLog();
                    sysLog.setUsername(username);
                    sysLog.setExecutionTime(time);
                    sysLog.setIp(ip);
                    sysLog.setUrl(url);
                    sysLog.setMethod("[类名]" + clazz.getName() + "[方法名]" + method.getName());
                    sysLog.setVisitTime(visitTime);
                    //调用service
                    sysLogService.save(sysLog);
                }
            }
        }

    }
}

同时还要记得在spring-mvc的配置文件中

添加这样的配置文件

<!-- 配置spring开启注解AOP的支持 -->
<aop:aspectj-autoproxy/>
        }

    }
}

sysLogService

@Service
@Transactional
public class SysLogServiceImpl implements SysLogService {

    @Autowired
    private SysLogDao sysLogDao;

    @Override
    public void save(SysLog sysLog) {
        sysLogDao.save(sysLog);
    }
}    

SysLogDao

public interface SysLogDao {

    @Insert("insert into syslog(visitTime,username,ip,url,executionTime,method) values(#{visitTime},#{username},#{ip},#{url},#{executionTime},#{method})")
    public void save(SysLog sysLog);
}    

同时还要记得在spring-mvc的配置文件中

添加这样的配置文件

<!-- 配置spring开启注解AOP的支持 -->
<aop:aspectj-autoproxy/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值