做这个功能之前 先讲一下AOP的环绕通知,因为这个功能我之前也想用springMVC的拦截器实现
环绕通知是所有通知类型中功能最为强大的, 能够全面地控制连接点. 甚至可以控制是否执行连接点.
对于环绕通知来说, 连接点的参数类型必须是 ProceedingJoinPoint . 它是 JoinPoint 的子接口, 允许控制何时执行, 是否执行连接点.
在环绕通知中需要明确调用 ProceedingJoinPoint 的 proceed() 方法来执行被代理的方法. 如果忘记这样做就会导致通知被执行了, 但目标方法没有被执行.
注意: 环绕通知的方法需要返回目标方法执行之后的结果, 即调用 joinPoint.proceed(); 的返回值, 否则会出现空指针异常
(1)环绕通知需要携带 ProceedingJoinPoint 类型的参数.
(2)环绕通知类似于动态代理的全过程: ProceedingJoinPoint 类型的参数可以决定是否执行目标方法.且环绕通知必须有返回值, 返回值即为目标方法的返回值
@Around("execution(* com..Spring4.AOP.*.*(..))")
public Object aroundMethod(ProceedingJoinPoint pjd){
Object result = null;
String methodName = pjd.getSignature().getName();
try {
//前置通知
System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs()));
//执行目标方法
result = pjd.proceed();
//返回通知
System.out.println("The method " + methodName + " ends with " + result);
} catch (Throwable e) {
//异常通知
System.out.println("The method " + methodName + " occurs exception:" + e);
throw new RuntimeException(e);
}
//后置通知
System.out.println("The method " + methodName + " ends");
return result;
参考自:http://blog.csdn.net/snow_7/article/details/52077770
RequestContextHolder顾名思义,持有上下文的Request容器.使用是很简单的,具体使用如下:
//两个方法在没有使用JSF的项目中是没有区别的
RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
// RequestContextHolder.getRequestAttributes();
//从session里面获取对应的值
String str = (String) requestAttributes.getAttribute("name",RequestAttributes.SCOPE_SESSION);
HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
HttpServletResponse response = ((ServletRequestAttributes)requestAttributes).getResponse();
看到这一般都会想到几个问题:
request和response怎么和当前请求挂钩?
request和response等是什么时候设置进去的?
2.1 request和response怎么和当前请求挂钩?
首先分析RequestContextHolder这个类,里面有两个ThreadLocal保存当前线程下的request,关于ThreadLocal可以参考这篇博文
http://www.jianshu.com/p/5675690b351e
后面详细的结果可以看http://blog.csdn.net/zzy7075/article/details/53559902
public class OperationRecordAspect {
private static final Logger logger = LoggerFactory.getLogger(OperationRecordAspect.class);
@Autowired
private IOperationRecordService OperationRecordService;
public Object aroundMethod(ProceedingJoinPoint pjd) {
Object result = null;
//String methodName = pjd.getSignature().getName();
//获取request
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
//请求url
String url = request.getRequestURI();
if(StrUtils.isNotNullOrBlank(url)){
try {
result = pjd.proceed();
if(url.contains("get")||url.contains("to")||url.contains("search")){
return result;
}
} catch (Throwable throwable) {
logger.error(throwable.toString());
}
}
logger.debug("拦截了员工从操作,url是:"+url);
logger.info("返回的结果是"+result);
//logger.info("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs()));
//返回通知
//logger.info("The method " + methodName + " ends with " + result);
//参数列表
String param = null;
Map parameterMap = request.getParameterMap();
if(parameterMap!=null&& parameterMap.size()>0 ){
param = JSON.toJSONString(parameterMap);
if(StrUtils.isNotNullOrBlank(param)){
logger.info("请求中的参数param"+param);
}
}
//操作人
Employee employee = (Employee)request.getSession().getAttribute("employee");
String employeeName = null;
String loginName = null;
if(employee!=null){
loginName = employee.getLoginName();
employeeName = employee.getEmployeeName();
logger.info("操作员工的登录名:"+loginName+"操作员工的姓名:"+employeeName);
}
OperationRecord operationRecord = new OperationRecord();
operationRecord.setLoginName(loginName);
operationRecord.setEmployeeName(employeeName);
operationRecord.setUrl(url);
operationRecord.setOperationTime(new Date());
operationRecord.setParam(param);
operationRecord.setResult(result.toString());
OperationRecordService.addOperationRecord(operationRecord);
return result;
}
}
这样就实现AOP对SpringMVC controller的拦截,当然,我们的项目是统一管理Exception,所有没有对Exception进行拦截,如果有需要也可以配置