依赖:(应该是不用那么多jar包的)
<!-- 日志jar包 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>xxx</version>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.0</version>
</dependency>
<!--springaop支持-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>xxxx</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.4</version>
</dependency>
<!--<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.7.4</version>
</dependency>
--><dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.4</version>
</dependency>
在springmvc.xml中配置
<!--开启@AspectJ自动代理支持-->
<aop:aspectj-autoproxy proxy-target-class="true" />
<aop:config>
<!--定义切面,(我已经在切面类上加了@Component,就不用在配置文件中定义bean了)-->
<aop:aspect id="logAspect" ref="logInterceptor">
<!-- 定义切入点1 (配置在com.huawei.action下所有的类在调用之前都会被拦截)-->
<aop:pointcut expression="execution(* com.xxx.action.*.*(..))" id="logPointCut"/>
<!-- 定义切入点 2(配置在com.huawei.action以及Service下所有的类在调用之前都会被拦截)-->
<aop:pointcut expression="execution(* com.xxxx.serviceImp.*.*(..)) || execution(* com.xxx.action.*.*(..))"
id="logPointCutAll"/>
<!--方法执行之前被调用执行的-->
<aop:before method="before" pointcut-ref="logPointCut" /><!--一个切入点的引用-->
<!--方法执行之后被调用执行的-->
<aop:after method="after" pointcut-ref="logPointCut"/><!--一个切入点的引用-->
<!--获取到返回值之后执行,arg-names的值就是切面类中的方法入参的值的参数名,需要对应上-->
<aop:after-returning
pointcut-ref="logPointCut"
arg-names="joinPoint,retValue"
returning="retValue"
method="afterReturning"
/>
<!-- 异常处理, throwing="e",e就是切面类的方法afterThrowing入参Throwable的参数名,需要对应上-->
<aop:after-throwing method="afterThrowing"
pointcut-ref="logPointCutAll"
throwing="e" />
</aop:aspect>
</aop:config>
切面类
package com.huawei.log;
import java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@Component
public class LogInterceptor {
private long startTimeMillis = 0; // 开始时间
private long endTimeMillis = 0; // 结束时间
private HttpServletRequest request = null;
private SimpleDateFormat format = new SimpleDateFormat("yyyy--MM-dd hh:mm:ss");
public void before(JoinPoint joinPoint){
Object object[] = joinPoint.getArgs(); // 获取被切函数 的参数
request = getHttpServletRequest();
}
/**
* 后置通知 【即 被切函数执行完之后 执行该 函数】
* @param joinPoint
* @param retValue 被切函数的返回值
*/
public void afterReturning( JoinPoint joinPoint,Object retValue){
//Object object[] = joinPoint.getArgs(); // 获取被切函数 的参数
logger.info("------返回数据:"+retValue);
logger.info( "------请求结束时间:"+format.format(System.currentTimeMillis()));
logger.info("请求end!======================");
}
public void after(){
}
public void afterThrowing( Throwable e){
logger.error("afterThrowing: "+e.getMessage(), e);
}
/**
* @Description: 获取request
* @param
* @return HttpServletRequest
*/
public HttpServletRequest getHttpServletRequest(){
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes sra = (ServletRequestAttributes)ra;
HttpServletRequest request = sra.getRequest();
return request;
}
}