//1、创建日志表syslog-------》创建日志的实体类---------》在web.xml中配置监听

<listener>
     <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

--------》书写LogAop组件,并封装到实体类中,保存到数据库

package com.hope.controller;

import com.hope.domain.SysLog;
import com.hope.service.ISysLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

/**
* @author newcityman
* @date 2019/12/14 - 22:44
*/
@Component
@Aspect
public class LogAop {

@Autowired
private HttpServletRequest request;

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

@Autowired
private ISysLogService sysLogService;

//前置通知 主要获取开始时间,执行的是哪个类,哪个方法
@Before("execution(* com.hope.controller.*.*(..))")
public void doBefore(JoinPoint jp) throws Exception {
visitTime = new Date(); //当前时间就是开始的时间
clazz = jp.getTarget().getClass(); //具体访问的类
String methodName = jp.getSignature().getName(); //获取的访问方法的名称
Object[] args = jp.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();
}
method = clazz.getMethod(methodName, classArgs);
}

}

@After("execution(* com.hope.controller.*.*(..))")
public void doAfter(JoinPoint jp) throws Exception {
executionTime = new Date().getTime() - visitTime.getTime(); //获取访问时长
String url = "";
//获取url
if (clazz != null && method != null && clazz != LogAop.class) {
//获取类上的@RequestMapping("/***")
RequestMapping clazzAnnotation = (RequestMapping) clazz.getAnnotation(RequestMapping.class);
if (clazzAnnotation != null) {
String[] clazzValue = clazzAnnotation.value();
//获取方法上@RequestMapping("/***")
RequestMapping methodAnnotation = method.getAnnotation(RequestMapping.class);
if (methodAnnotation != null) {
String[] methodValue = methodAnnotation.value();
url = clazzValue[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.setVisitTime(visitTime);
sysLog.setUsername(username);
sysLog.setIp(ip);
sysLog.setUrl(url);
sysLog.setExecutionTime(executionTime);
sysLog.setMethod("[类名] " + clazz.getName() + " [方法名] " + method.getName());

//调用SysLogSservice的方法
sysLogService.save(sysLog);

}

}

使用AOP思想实现日志的添加的更多相关文章

  1. Spring框架系列之AOP思想

    微信公众号:compassblog 欢迎关注.转发,互相学习,共同进步! 有任何问题,请后台留言联系! 1.AOP概述 (1).什么是 AOP AOP 为 Aspect Oriented Progra ...

  2. [ SSH框架 ] Spring框架学习之二(Bean的管理和AOP思想)

    一.Spring的Bean管理(注解方式) 1.1 什么是注解 要使用注解方式实现Spring的Bean管理,首先要明白什么是注解.通俗地讲,注解就是代码里的特殊标记,使用注解可以完成相应功能. 注解 ...

  3. 第四节:MVC中AOP思想的体现(四种过滤器)并结合项目案例说明过滤器的实际用法

    一. 简介 MVC中的过滤器可以说是MVC框架中的一种灵魂所在,它是MVC框架中AOP思想的具体体现,所以它以面向切面的形式无侵入式的作用于代码的业务逻辑,与业务逻辑代码分离,一经推出,广受开发者的喜 ...

  4. Spring aop 记录操作日志 Aspect

    前几天做系统日志记录的功能,一个操作调一次记录方法,每次还得去收集参数等等,太尼玛烦了.在程序员的世界里,当你的一个功能重复出现多次,就应该想想肯定有更简单的实现方法.于是果断搜索各种资料,终于搞定了 ...

  5. Unity应用架构设计(12)——AOP思想的实践

    想象一下,当程序所有的业务逻辑都完成的时候,你可能还来不及喘口气,紧张的测试即将来临.你的Boss告诉你,虽然程序没问题,但某些方法为什么执行这么慢,性能堪忧.领会了Boss的意图之后,漫长的排查问题 ...

  6. Spring框架学习之注解配置与AOP思想

         上篇我们介绍了Spring中有关高级依赖关系配置的内容,也可以调用任意方法的返回值作为属性注入的值,它解决了Spring配置文件的动态性不足的缺点.而本篇,我们将介绍Spring的又一大核心 ...

  7. 第一节: Timer的定时任务的复习、Quartz.Net的入门使用、Aop思想的体现

    一. 前奏-Timer类实现定时任务 在没有引入第三方开源的定时调度框架之前,我们处理一些简单的定时任务同时都是使用Timer类, DotNet中的Timer类有三个,分别位于不同的命名空间下,分别是 ...

  8. 使用SpringBoot AOP 记录操作日志、异常日志

    平时我们在做项目时经常需要对一些重要功能操作记录日志,方便以后跟踪是谁在操作此功能:我们在操作某些功能时也有可能会发生异常,但是每次发生异常要定位原因我们都要到服务器去查询日志才能找到,而且也不能对发 ...

  9. Spring的aop思想

    1.AOP思想 (1)在解决中文乱码的应用: 一般情况下如果在Servlet中要解决中文乱码问题需要在每一个Servlet中书写解决乱码的代码,但是,在运用了过滤器之后,就不再需要每一个Servlet ...

随机推荐

  1. 连接url

    celery broker redis with password broker_url = 'redis://user:password@redishost:6379/0' tooz zookeep ...

  2. Windows内核中的CPU架构-8-任务段TSS(task state segment)

    Windows内核中的CPU架构-8-任务段TSS(task state segment) 任务段tss(task state segment)是针对于CPU的一个概念. 举一个简单的例子,你一个电脑 ...

  3. 【JAVA】编程(2)---时间管理

    作业要求: 定义一个名为 MyTime 的类,其中私有属性包括天数,时,分,秒:定义一个可以初始化时,分,秒的构造方法,并对初始化数值加以限定,以防出现bug:定义一个方法,可以把第几天,时,分,秒打 ...

  4. java 邮件 接收与发送

    ... package com.e6soft; import java.io.BufferedReader; import java.io.FileOutputStream; import java. ...

  5. 直接插入100w数据报错

    ### Cause: com.mysql.cj.jdbc.exceptions.PacketTooBigException: Packet for query is too large (77,600 ...

  6. git使用小技巧

    1. 合并一个分支的某次提交到另一个分支上 例如 将dev的某次提交 asfdiwehfsalkdnva872383 合并到master # git checkout master # git che ...

  7. IE 跨域设置

    开发的时候会发现IE下跨域无法访问,报错: Failed to load resource: net::ERR_CONNECTION_REFUSED 解决方法有两种: 自己写代理服务,访问代理服务,代 ...

  8. vue3 高阶 API 大汇总,强到离谱

    高阶函数是什么呢? 高阶函数英文名叫:Higher Order function ,一个函数可以接收一个或多个函数作为输入,或者输出一个函数,至少满足上述条件之一的函数,叫做高阶函数. 前言 本篇内容 ...

  9. 59. Divide Two Integers

    Divide Two Integers My Submissions QuestionEditorial Solution Total Accepted: 66073 Total Submission ...

  10. Mac下source tree 下的安装

    安装时出现了以下错误,解决方法 git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=source ...