利用spring AOP实现每个请求的日志输出
前提条件:
除了spring相关jar包外,还需要引入aspectj包。
- <dependency>
- <groupId>org.aspectj</groupId>
- <artifactId>aspectjweaver</artifactId>
- <version>1.7.2</version>
- </dependency>
要实现此功能,必须完成以下几步:
1.在springmvc-servlet.xml中实现对AOP的支持
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
- <aop:aspectj-autoproxy proxy-target-class="true"/>
- <bean class="com.yusj.interceptor.LogAspect" />
- .
- .
- .
- .
- </beans>
2.注解的方法实现Aspect
- package com.yusj.core.interceptor;
- import java.text.SimpleDateFormat;
- import java.util.HashMap;
- import java.util.Map;
- import javax.servlet.http.HttpServletRequest;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.annotation.After;
- import org.aspectj.lang.annotation.Around;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Before;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.web.context.request.RequestAttributes;
- import org.springframework.web.context.request.RequestContextHolder;
- import org.springframework.web.context.request.ServletRequestAttributes;
- import com.google.gson.Gson;
- /**
- *
- * @ClassName: LogAspect
- * @Description: 日志记录AOP实现
- * @author shaojian.yu
- * @date 2014年11月3日 下午1:51:59
- *
- */
- @Aspect
- public class LogAspect {
- private final Logger logger = LoggerFactory.getLogger(this.getClass());
- private String requestPath = null ; // 请求地址
- private String userName = null ; // 用户名
- private Map<?,?> inputParamMap = null ; // 传入参数
- private Map<String, Object> outputParamMap = null; // 存放输出结果
- private long startTimeMillis = 0; // 开始时间
- private long endTimeMillis = 0; // 结束时间
- /**
- *
- * @Title:doBeforeInServiceLayer
- * @Description: 方法调用前触发
- * 记录开始时间
- * @author shaojian.yu
- * @date 2014年11月2日 下午4:45:53
- * @param joinPoint
- */
- @Before("execution(* com.yusj.controller..*.*(..))")
- public void doBeforeInServiceLayer(JoinPoint joinPoint) {
- startTimeMillis = System.currentTimeMillis(); // 记录方法开始执行的时间
- }
- /**
- *
- * @Title:doAfterInServiceLayer
- * @Description: 方法调用后触发
- * 记录结束时间
- * @author shaojian.yu
- * @date 2014年11月2日 下午4:46:21
- * @param joinPoint
- */
- @After("execution(* com.yusj.controller..*.*(..))")
- public void doAfterInServiceLayer(JoinPoint joinPoint) {
- endTimeMillis = System.currentTimeMillis(); // 记录方法执行完成的时间
- this.printOptLog();
- }
- /**
- *
- * @Title:doAround
- * @Description: 环绕触发
- * @author shaojian.yu
- * @date 2014年11月3日 下午1:58:45
- * @param pjp
- * @return
- * @throws Throwable
- */
- @Around("execution(* com.yusj.controller..*.*(..))")
- public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
- /**
- * 1.获取request信息
- * 2.根据request获取session
- * 3.从session中取出登录用户信息
- */
- RequestAttributes ra = RequestContextHolder.getRequestAttributes();
- ServletRequestAttributes sra = (ServletRequestAttributes)ra;
- HttpServletRequest request = sra.getRequest();
- // 从session中获取用户信息
- String loginInfo = (String) session.getAttribute("username");
- if(loginInfo != null && !"".equals(loginInfo)){
- userName = operLoginModel.getLogin_Name();
- }else{
- userName = "用户未登录" ;
- }
- // 获取输入参数
- inputParamMap = request.getParameterMap();
- // 获取请求地址
- requestPath = request.getRequestURI();
- // 执行完方法的返回值:调用proceed()方法,就会触发切入点方法执行
- outputParamMap = new HashMap<String, Object>();
- Object result = pjp.proceed();// result的值就是被拦截方法的返回值
- outputParamMap.put("result", result);
- return result;
- }
- /**
- *
- * @Title:printOptLog
- * @Description: 输出日志
- * @author shaojian.yu
- * @date 2014年11月2日 下午4:47:09
- */
- private void printOptLog() {
- Gson gson = new Gson(); // 需要用到google的gson解析包
- String optTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startTimeMillis);
- logger.info("\n user:"+userName
- +" url:"+requestPath+"; op_time:" + optTime + " pro_time:" + (endTimeMillis - startTimeMillis) + "ms ;"
- +" param:"+gson.toJson(inputParamMap)+";"+"\n result:"+gson.toJson(outputParamMap));
- }
- }
利用spring AOP实现每个请求的日志输出的更多相关文章
- 利用Spring AOP和自定义注解实现日志功能
Spring AOP的主要功能相信大家都知道,日志记录.权限校验等等. 用法就是定义一个切入点(Pointcut),定义一个通知(Advice),然后设置通知在该切入点上执行的方式(前置.后置.环绕等 ...
- 运用Spring Aop,一个注解实现日志记录
运用Spring Aop,一个注解实现日志记录 1. 介绍 我们都知道Spring框架的两大特性分别是 IOC (控制反转)和 AOP (面向切面),这个是每一个Spring学习视频里面一开始都会提到 ...
- spring AOP自定义注解方式实现日志管理
今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...
- 化繁就简,如何利用Spring AOP快速实现系统日志
1.引言 有关Spring AOP的概念就不细讲了,网上这样的文章一大堆,要讲我也不会比别人讲得更好,所以就不啰嗦了. 为什么要用Spring AOP呢?少写代码.专注自身业务逻辑实现(关注本身的业务 ...
- 我使用Spring AOP实现了用户操作日志功能
我使用Spring AOP实现了用户操作日志功能 今天答辩完了,复盘了一下系统,发现还是有一些东西值得拿出来和大家分享一下. 需求分析 系统需要对用户的操作进行记录,方便未来溯源 首先想到的就是在每个 ...
- 利用Spring AOP自定义注解解决日志和签名校验
转载:http://www.cnblogs.com/shipengzhi/articles/2716004.html 一.需解决的问题 部分API有签名参数(signature),Passport首先 ...
- (转)利用Spring AOP自定义注解解决日志和签名校验
一.需解决的问题 部分API有签名参数(signature),Passport首先对签名进行校验,校验通过才会执行实现方法. 第一种实现方式(Origin):在需要签名校验的接口里写校验的代码,例如: ...
- [编码实践]SpringBoot实战:利用Spring AOP实现操作日志审计管理
设计原则和思路: 元注解方式结合AOP,灵活记录操作日志 能够记录详细错误日志为运营以及审计提供支持 日志记录尽可能减少性能影响 操作描述参数支持动态获取,其他参数自动记录. 1.定义日志记录元注解, ...
- spring:利用Spring AOP 使日志输入与方法分离
对方法进行日志输出是一种很常见的功能.传统的做法是把输出语句写在方法体的内部,在调用该方法时,用输入语句输出信息来记录方法的执行! 1.先写一个普通类: package com.importnew; ...
随机推荐
- 八爪鱼采集器︱加载更多、再显示20条图文教程(Xpatth、Ajax)
每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 由于代码布置采集器比较麻烦,又很早知道八爪鱼采 ...
- FusionCharts 3D帕累托图报错
今天我在设计3D帕累托图时,是由原来的2D帕累托图页面重命名而来,但是当我重命名后发现HTML文件打不开,而且还报错,真不知道是什么原因引起的.因此,我就将这个错误截图,保存下来,希望以后能够解决,或 ...
- Oracle SQL Developer 连接数据库如何对应数据库配置文件
Oracle SQL Developer 连接数据库如何对应数据库配置文件 1.数据库配置文件 hibernate.connection.url jdbc:oracle:thin:@146.56.35 ...
- 第一次C语言实验报告
一.实验题目,设计思路,实现方法 实验四4-2-9三个数由小到大输出,要求比较三数大小并按顺序输出.运用穷举法列举所有可能性再对应输出.运用多分支结构. 实验四4-2-4 三天打鱼两天晒网,运用循环结 ...
- MyISAM和InnoDB索引实现区别
首先来讲MyISAM: MyISAM引擎使用B+Tree作为索引结构,叶节点的data域存放的是数据记录的地址.下图是MyISAM索引的原理图: 这里设表一共有三列,假设我们以Col1为主键,则上图是 ...
- Readis For Windows安装及密码、IP限制
一.下载与安装Readis Github下载地址:https://github.com/MicrosoftArchive/redis/tags 下载.MSI后下一步安装即可 二.验证安装 1. 在&q ...
- mysql中的union用法以及子查询综合应用
union查询就是把2条或者多条sql语句的查询结果,合并成一个结果集. 如:sql1: N行,sql2: M行,sql1 union sql2 ---> N+M行 1.能否从2张表查询再uni ...
- 【BZOJ2442】修建草坪(动态规划,单调队列)
[BZOJ2442]修建草坪(动态规划,单调队列) 题面 权限题..洛谷 题解 设\(f[i]\)表示前\(i\)个里面选出来的最大值 转移应该比较显然 枚举一个断点的位置,转移一下就好 \(f[i] ...
- [AH/HNOI2017]单旋
这道题可以用LCT做,开set,LCT,二叉树 操作1:直接开set,找到它要插入的位置,一定是前驱,后缀中deep最大的(显然手玩) 操作2:set+LCT询问路径,直接手动提上去,因为树的形态不变 ...
- [BZOJ4195] [NOI2015] 程序自动分析 (并查集)
Description 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3,…代表程序中出现的变量,给定n个形如xi=xj或x ...