SSM14-通过AOP实现日志记录
1.要求使用AOP思想,实现对每一个用户登陆后,将以下信息保存在数据库
1>登陆时间
2>退出时间
3>登录的IP地址
4>访问点URL(访问了那些Controller)
5>访问总时间
2.实体类存放需要的信息
@Data
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;
}
3.通过前置通知和后置通知实现该功能
@Component
@Aspect
public class LogAop { @Autowired
private HttpServletRequest request;
@Autowired
private ISysLogService sysLogService;
private Date startTime; // 访问时间
private Class executionClass;// 访问的类
private Method executionMethod; // 访问的方法 // 主要获取访问时间、访问的类、访问的方法
@Before("execution(* com.hdh.web.controller.*.*(..))")
public void doBefore(JoinPoint jp) throws NoSuchMethodException, SecurityException {
System.out.println("============================================="); startTime = new Date(); // 访问时间
// 获取访问的类
executionClass = jp.getTarget().getClass();
// 获取访问的方法
String methodName = jp.getSignature().getName();// 获取访问的方法的名称
Object[] args = jp.getArgs();// 获取访问的方法的参数
if (args == null || args.length == 0) {// 无参数
executionMethod = executionClass.getMethod(methodName); // 只能获取无参数方法
} else {
// 有参数,就将args中所有元素遍历,获取对应的Class,装入到一个Class[]
Class[] classArgs = new Class[args.length];
for (int i = 0; i < args.length; i++) {
classArgs[i] = args[i].getClass();
}
executionMethod = executionClass.getMethod(methodName, classArgs);// 获取有参数方法
System.out.println(executionMethod);
}
} // 主要获取日志中其它信息,时长、ip、url...
@After("execution(* com.hdh.web.controller.*.*(..))")
public void doAfter(JoinPoint jp) throws Exception {
// 获取类上的@RequestMapping对象
if (executionClass != SysLogController.class) {
RequestMapping classAnnotation = (RequestMapping) executionClass.getAnnotation(RequestMapping.class);
if (classAnnotation != null) {
// 获取方法上的@RequestMapping对象
RequestMapping methodAnnotation = executionMethod.getAnnotation(RequestMapping.class);
if (methodAnnotation != null) {
String url = ""; // 它的值应该是类上的@RequestMapping的value+方法上的@RequestMapping的value
url = classAnnotation.value()[0] + methodAnnotation.value()[0];
SysLog sysLog = new SysLog();
// 获取访问时长
Long executionTime = new Date().getTime() - startTime.getTime();
// 将sysLog对象属性封装
sysLog.setExecutionTime(executionTime);
sysLog.setUrl(url);
// 获取ip
String ip = request.getRemoteAddr();
sysLog.setIp(ip);
// 可以通过securityContext获取,也可以从request.getSession中获取
// SecurityContext context = SecurityContextHolder.getContext(); //request.getSession().getAttribute("SPRING_SECURITY_CONTEXT")
//String username = ((Employee) (context.getAuthentication().getPrincipal())).getUsername();
String username = (String) request.getSession().getAttribute("username");
sysLog.setUsername(username);
sysLog.setMethod("[类名]" + executionClass.getName() + "[方法名]" +
executionMethod.getName());
sysLog.setVisitTime(startTime);
// 调用Service,调用dao将sysLog insert数据库
sysLogService.save(sysLog);
}
}
}
}
}
4.支持AOP的注解支持,AOP底层使用代理技术
JDK动态代理,要求必须有接口
cglib代理,生成子类对象,proxy-target-class="true" 默认使用cglib的方式
在springmvc中加上:
<aop:aspectj-autoproxy proxy-target-class="true"/>
SSM14-通过AOP实现日志记录的更多相关文章
- Spring AOP 完成日志记录
Spring AOP 完成日志记录 http://hotstrong.iteye.com/blog/1330046
- Spring AOP进行日志记录
在java开发中日志的管理有很多种.我一般会使用过滤器,或者是Spring的拦截器进行日志的处理.如果是用过滤器比较简单,只要对所有的.do提交进行拦截,然后获取action的提交路径就可以获取对每个 ...
- Spring AOP进行日志记录,管理
在java开发中日志的管理有很多种.我一般会使用过滤器,或者是Spring的拦截器进行日志的处理.如果是用过滤器比较简单,只要对所有的.do提交进行拦截,然后获取action的提交路径就可以获取对每个 ...
- java使用动态代理来实现AOP(日志记录)
以下内容为原创,转载时请注明链接地址:http://www.cnblogs.com/tiantianbyconan/p/3336627.html AOP(面向方面)的思想,就是把项目共同的那部分功能分 ...
- spring boot集成aop实现日志记录
1.pom依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
- Spring AOP的日志记录
现在的项目是Spring+MyBatis,前段时间项目经理让我干了一个活,就是给所有的controller里的所有方法加上日志记录的代码,其实没有多少,也就300来个方法,也没有抱怨什么,一边打着瞌睡 ...
- 自定义注解-aop实现日志记录
关于注解,平时接触的可不少,像是 @Controller.@Service.@Autowried 等等,不知道你是否有过这种疑惑,使用 @Service 注解的类成为我们的业务类,使用 @Contro ...
- 记一次基于springboot+aop实现日志记录实战
1. 为什么要记录日志 好处: a. 可以对一些重要功能进行记录,方便以后跟踪是谁操作此功能的. b. 在操作某些功能时可能会发生异常,但每次出现异常我们想定位日志都要去服务器查看我们的日志.有了日志 ...
- .NetCore中使用AspectCore、ExceptionLess 实现AOP操作日志记录
结合前面封装的ExceptionLess,接下来使用 AspectCore 实现AOP日志处理 nuget导入AspectCore.Core .AspectCore.Extensions.Depend ...
- Spring 之Aop实现日志记录
Aop实现见代码,简单demo实现 package com.idcos.automate.config; import com.idcos.automate.dal.auto.dao.dcos.Dco ...
随机推荐
- Codeforces346D. Robot Control
D. Robot Control time limit per test 6 seconds memory limit per test 256 megabytes input standard in ...
- zmq利用protobuf通信
protobuf序列化之后为二进制数据,数据中可能包含 ‘\0’,直接转换为char *类型会导致发送数据不完整.解决方法: void buildProtobufMsg(const string&am ...
- 笔记32 SpringMVC中使用静态资源、处理中文乱码
一.静态资源的使用 在WebConfig.java中有如下代码段 @Override // 配置静态资源处理 public void configureDefaultServletHandling(D ...
- re.match与re.search的区别
re.match与re.search的区别 re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None:而re.search匹配整个字符串,直到找到一个匹配. 实 ...
- 使用threading模块创建线程
#_author:来童星#date:2019/12/17#使用threading模块创建线程import threading,timedef process(): for i in range(3): ...
- Exception一自定义异常
异常体系的根类是:Throwable Throwable: Error: 重大的问题,我们处理不了.也不需要编写代码处理.比如说内存溢出. Exception: 一般性的错误,是需要我们对编写 ...
- delphi 安卓开发常用
delphi 安卓开发有几个常用的pas: FMX.Helpers.Android, Androidapi.JNI.Net, Androidapi.JNI.GraphicsContentViewTex ...
- "\r\n"与"</br>"的区别
\n是换行,英文是New line,表示使光标到行首 \r是回车,英文是Carriage return,表示使光标下移一格 \r\n表示回车换行 \\ 反斜杠 \$ 美圆符 \" 双引 ...
- CodeForces-1215C-Swap Letters-思维
Monocarp has got two strings ss and tt having equal length. Both strings consist of lowercase Latin ...
- Ehcache3.x学习(一)入门
简介 Ehcache 是一个开源的高性能缓存,拥有很高的拓展性和伸缩性,广泛使用各种 Java 项目中(如 Hibernate 默认使用 Ehcache作为二级缓存),在目前基于 Java 的缓存方案 ...