前提条件:
除了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)); } }
相关推荐
AopLogAopLog是基于SpringAop和ThreadLocal实现的一个对请求方法埋点记录与处理的日志工具包。设计目的和场景:使用Spring Aop拦截程序,基本上都是同一个小异,不想日后每个项目都柏林都写一份这样的Aop拦截处理...
在处理基于Spring的Web应用程序项目时,它很快变得烦人,为每个要创建的新控制器方法添加相同的日志语句。 在团队中工作也使人们忘记或添加未经格式化的日志变得不一致。 这是AOP(面向方面编程)的完美用例。 ...
容器——Spring包含并管理应用对象的配置和生命周期,在这个意义上它是一种容器,你可以配置你的每个bean如何被创建——基于一个可配置原型(prototype),你的bean可以创建一个单独的实例或者每次需要时都生成一个...
组成 Spring 框架的每个模块(或组件)都可以单独存在,或者与其他一个或多个模块联合实现。每个模块的功能如下: ☆ 核心容器:核心容器提供 Spring 框架的基本功能。核心容器的主要组件是 BeanFactory,它是工厂...
◆容器——Spring包含并管理应用对象的配置和生命周期,在这个意义上它是一种容器,你可以配置你的每个bean如何被创建——基于一个可配置原型(prototype),你的bean可以创建一个单独的实例或者每次需要时都生成一...
◆容器——Spring包含并管理应用对象的配置和生命周期,在这个意义上它是一种容器,你可以配置你的每个bean如何被创建——基于一个可配置原型(prototype),你的bean可以创建一个单独的实例或者每次需要时都生成一...
java毕设 SSM架构,是三层结合所成...可以配置每个bean如何被创建,基于一个可配置原型prototype,你的bean可以创建一个单独的实例或者每次需要时都生成一个新的实例。 支持AOP:spring提供对AOP的支持,它允许将一些
java学习 ...可以配置每个bean如何被创建,基于一个可配置原型prototype,你的bean可以创建一个单独的实例或者每次需要时都生成一个新的实例。 支持AOP:spring提供对AOP的支持,它允许将一些通用任务
可以配置每个bean如何被创建,基于一个可配置原型prototype,你的bean可以创建一个单独的实例或者每次需要时都生成一个新的实例。 支持AOP:spring提供对AOP的支持,它允许将一些通用任务,如安
可以配置每个bean如何被创建,基于一个可配置原型prototype,你的bean可以创建一个单独的实例或者每次需要时都生成一个新的实例。 支持AOP:spring提供对AOP的支持,它允许将一些通用任务,如安
为了实时监控系统的操作情况,项目中使用了spring的AOP实现了系统操作日志。为了增强用户的交互体验,本系统使用前端开源可视化库ECharts对人事信息通过图表的方式进行统计。 系统框图 系统结构图 系
Sosoo利用回调的方式实现了aop,用户可以通过回调的方式注入javaBean对象,以实现其外部功能 1.基本功能参数的设置。 基本功能指: roboter的原生类型参数(或String),例如:startURL,maxDepth等。 设置开始...
利用反射实现ASP.NET控件和数据实体之间的双向绑定,并且在客户端自动验证输入的内容是否合法 asp.net报表解决方法 SQLDMO类的使用 SQL过程自动C#封装,支持从表到基本存储过程生成 使用SQLDMO控制 SQL Server 使用SQL...
Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的RPC实现服务的输出和输入功能,可以和Spring 框架无缝集成。Dubbo主要核心部件:Remoting:网络通信框架,实现了sync-over-async和...