前提条件:

除了spring相关jar包外,还需要引入aspectj包。

  1. <dependency>
  2. <groupId>org.aspectj</groupId>
  3. <artifactId>aspectjweaver</artifactId>
  4. <version>1.7.2</version>
  5. </dependency>

要实现此功能,必须完成以下几步:

1.在springmvc-servlet.xml中实现对AOP的支持

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:aop="http://www.springframework.org/schema/aop"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:mvc="http://www.springframework.org/schema/mvc"
  7. xsi:schemaLocation="
  8. http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context-4.0.xsd
  12. http://www.springframework.org/schema/mvc
  13. http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  14. http://www.springframework.org/schema/aop
  15. http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
  16. <aop:aspectj-autoproxy proxy-target-class="true"/>
  17. <bean class="com.yusj.interceptor.LogAspect" />
  18. .
  19. .
  20. .
  21. .
  22. </beans>

2.注解的方法实现Aspect

  1. package com.yusj.core.interceptor;
  2. import java.text.SimpleDateFormat;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import javax.servlet.http.HttpServletRequest;
  6. import org.aspectj.lang.JoinPoint;
  7. import org.aspectj.lang.ProceedingJoinPoint;
  8. import org.aspectj.lang.annotation.After;
  9. import org.aspectj.lang.annotation.Around;
  10. import org.aspectj.lang.annotation.Aspect;
  11. import org.aspectj.lang.annotation.Before;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.springframework.web.context.request.RequestAttributes;
  15. import org.springframework.web.context.request.RequestContextHolder;
  16. import org.springframework.web.context.request.ServletRequestAttributes;
  17. import com.google.gson.Gson;
  18. /**
  19. *
  20. * @ClassName: LogAspect
  21. * @Description: 日志记录AOP实现
  22. * @author shaojian.yu
  23. * @date 2014年11月3日 下午1:51:59
  24. *
  25. */
  26. @Aspect
  27. public class LogAspect {
  28. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  29. private String requestPath = null ; // 请求地址
  30. private String userName = null ; // 用户名
  31. private Map<?,?> inputParamMap = null ; // 传入参数
  32. private Map<String, Object> outputParamMap = null; // 存放输出结果
  33. private long startTimeMillis = 0; // 开始时间
  34. private long endTimeMillis = 0; // 结束时间
  35. /**
  36. *
  37. * @Title:doBeforeInServiceLayer
  38. * @Description: 方法调用前触发
  39. *  记录开始时间
  40. * @author shaojian.yu
  41. * @date 2014年11月2日 下午4:45:53
  42. * @param joinPoint
  43. */
  44. @Before("execution(* com.yusj.controller..*.*(..))")
  45. public void doBeforeInServiceLayer(JoinPoint joinPoint) {
  46. startTimeMillis = System.currentTimeMillis(); // 记录方法开始执行的时间
  47. }
  48. /**
  49. *
  50. * @Title:doAfterInServiceLayer
  51. * @Description: 方法调用后触发
  52. *  记录结束时间
  53. * @author shaojian.yu
  54. * @date 2014年11月2日 下午4:46:21
  55. * @param joinPoint
  56. */
  57. @After("execution(* com.yusj.controller..*.*(..))")
  58. public void doAfterInServiceLayer(JoinPoint joinPoint) {
  59. endTimeMillis = System.currentTimeMillis(); // 记录方法执行完成的时间
  60. this.printOptLog();
  61. }
  62. /**
  63. *
  64. * @Title:doAround
  65. * @Description: 环绕触发
  66. * @author shaojian.yu
  67. * @date 2014年11月3日 下午1:58:45
  68. * @param pjp
  69. * @return
  70. * @throws Throwable
  71. */
  72. @Around("execution(* com.yusj.controller..*.*(..))")
  73. public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
  74. /**
  75. * 1.获取request信息
  76. * 2.根据request获取session
  77. * 3.从session中取出登录用户信息
  78. */
  79. RequestAttributes ra = RequestContextHolder.getRequestAttributes();
  80. ServletRequestAttributes sra = (ServletRequestAttributes)ra;
  81. HttpServletRequest request = sra.getRequest();
  82. // 从session中获取用户信息
  83. String loginInfo = (String) session.getAttribute("username");
  84. if(loginInfo != null && !"".equals(loginInfo)){
  85. userName = operLoginModel.getLogin_Name();
  86. }else{
  87. userName = "用户未登录" ;
  88. }
  89. // 获取输入参数
  90. inputParamMap = request.getParameterMap();
  91. // 获取请求地址
  92. requestPath = request.getRequestURI();
  93. // 执行完方法的返回值:调用proceed()方法,就会触发切入点方法执行
  94. outputParamMap = new HashMap<String, Object>();
  95. Object result = pjp.proceed();// result的值就是被拦截方法的返回值
  96. outputParamMap.put("result", result);
  97. return result;
  98. }
  99. /**
  100. *
  101. * @Title:printOptLog
  102. * @Description: 输出日志
  103. * @author shaojian.yu
  104. * @date 2014年11月2日 下午4:47:09
  105. */
  106. private void printOptLog() {
  107. Gson gson = new Gson(); // 需要用到google的gson解析包
  108. String optTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startTimeMillis);
  109. logger.info("\n user:"+userName
  110. +"  url:"+requestPath+"; op_time:" + optTime + " pro_time:" + (endTimeMillis - startTimeMillis) + "ms ;"
  111. +" param:"+gson.toJson(inputParamMap)+";"+"\n result:"+gson.toJson(outputParamMap));
  112. }
  113. }

利用spring AOP实现每个请求的日志输出的更多相关文章

  1. 利用Spring AOP和自定义注解实现日志功能

    Spring AOP的主要功能相信大家都知道,日志记录.权限校验等等. 用法就是定义一个切入点(Pointcut),定义一个通知(Advice),然后设置通知在该切入点上执行的方式(前置.后置.环绕等 ...

  2. 运用Spring Aop,一个注解实现日志记录

    运用Spring Aop,一个注解实现日志记录 1. 介绍 我们都知道Spring框架的两大特性分别是 IOC (控制反转)和 AOP (面向切面),这个是每一个Spring学习视频里面一开始都会提到 ...

  3. spring AOP自定义注解方式实现日志管理

    今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...

  4. 化繁就简,如何利用Spring AOP快速实现系统日志

    1.引言 有关Spring AOP的概念就不细讲了,网上这样的文章一大堆,要讲我也不会比别人讲得更好,所以就不啰嗦了. 为什么要用Spring AOP呢?少写代码.专注自身业务逻辑实现(关注本身的业务 ...

  5. 我使用Spring AOP实现了用户操作日志功能

    我使用Spring AOP实现了用户操作日志功能 今天答辩完了,复盘了一下系统,发现还是有一些东西值得拿出来和大家分享一下. 需求分析 系统需要对用户的操作进行记录,方便未来溯源 首先想到的就是在每个 ...

  6. 利用Spring AOP自定义注解解决日志和签名校验

    转载:http://www.cnblogs.com/shipengzhi/articles/2716004.html 一.需解决的问题 部分API有签名参数(signature),Passport首先 ...

  7. (转)利用Spring AOP自定义注解解决日志和签名校验

    一.需解决的问题 部分API有签名参数(signature),Passport首先对签名进行校验,校验通过才会执行实现方法. 第一种实现方式(Origin):在需要签名校验的接口里写校验的代码,例如: ...

  8. [编码实践]SpringBoot实战:利用Spring AOP实现操作日志审计管理

    设计原则和思路: 元注解方式结合AOP,灵活记录操作日志 能够记录详细错误日志为运营以及审计提供支持 日志记录尽可能减少性能影响 操作描述参数支持动态获取,其他参数自动记录. 1.定义日志记录元注解, ...

  9. spring:利用Spring AOP 使日志输入与方法分离

    对方法进行日志输出是一种很常见的功能.传统的做法是把输出语句写在方法体的内部,在调用该方法时,用输入语句输出信息来记录方法的执行! 1.先写一个普通类: package com.importnew; ...

随机推荐

  1. 普通权限拿webshell

    普通权限拿webshell:   1.0day拿webshell:这个不多说.可以去网上搜索一些, 比如你找到你搞的网站cms是discz的,你可以搜索一些相 关0day直接拿   2.修改网站上传类 ...

  2. 1.3 PCI总线的存储器读写总线事务

    总线的基本任务是实现数据传送,将一组数据从一个设备传送到另一个设备,当然总线也可以将一个设备的数据广播到多个设备.在处理器系统中,这些数据传送都要依赖一定的规则,PCI总线并不例外. PCI总线使用单 ...

  3. PCIe设备的配置空间

    关于PCI设备的配置空间网上已经有很多资料了,如下图就是PCI设备必须支持的64个字节的配置空间,范围为0x00-0x3f. 很多PCI设备仅仅支持者64字节的配置空间.PCI和PCIe配置空间的区别 ...

  4. Windows PowerShell漫谈-win7下没有超级终端

    Windows PowerShell是我在研究win7新特性的时候发现的新工具,起初没有对它产生太大的兴趣,只是简单看看了有关它的介绍.简单使用了一下,感觉上它和cmd.exe没有本质区别.对它产生兴 ...

  5. 为Hi3531添加4串口支持

    修改文件为 linux-3.0.y\arch\arm\mach-godnet\core.c linux-3.0.y\arch\arm\mach-godnet\include\mach\irqs.h 修 ...

  6. API接口签名校验

    在开发app中,我们经常要为app提供接口.但是为了保证数据的安全,我们通常会对接口的参数进行加密. 1.不验证的接口api api接口请求,"http://www.xx.com/getUs ...

  7. Error Code: 1054. Unknown column 'age' in 'user'

    1.错误描述 10:28:20 alter table user modify age int(3) after sex Error Code: 1054. Unknown column 'age' ...

  8. Angular21 动态绑定CSS样式

    1 需求 在前端开发中通常需要动态对某些元素的样式进行动态设定,传统的CSS绑定分为CSS类绑定和Style样式绑定:在Angular中利用相关指令同样可以完成CSS类绑定和CSS样式绑定 2 内置指 ...

  9. httpclient的主要业务代码逻辑(图解)

    一,主要代码逻辑(图解) 二,两个案例的对比(图解) 三,详细案例 3.1,博文一 httppost的用法(NameValuePair(简单名称值对节点类型)核心对象) 3.2,博文二 httpcli ...

  10. python爬取youtube视频 多线程 非中文自动翻译

    声明:我写的所有文章都是发在博客园的,我看到其他复制粘贴过去的 连个出处也不写,直接打上自己的水印...真是没的说了. 前言:前段时间搞了一些爬视频的项目,代码都写好了,这里写文章那就在来重新分析一遍 ...