今天遇到一个奇怪的问题,以前SSM框架迁移过来的日记记录类中出现一个错误
Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource
完整源码如下:
packagecn.fy3d.boot.common.aspect;
importcn.fy3d.boot.system.entity.SysLog;
importcn.fy3d.boot.system.service.ISysLogService;
importorg.apache.commons.lang.StringUtils;
importorg.apache.log4j.Logger;
importorg.apache.shiro.SecurityUtils;
importorg.apache.shiro.subject.PrincipalCollection;
importorg.apache.shiro.subject.Subject;
importorg.aspectj.lang.ProceedingJoinPoint;
importorg.aspectj.lang.annotation.Around;
importorg.aspectj.lang.annotation.Aspect;
importorg.aspectj.lang.annotation.Before;
importorg.aspectj.lang.annotation.Pointcut;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Component;
importorg.springframework.web.context.request.RequestContextHolder;
importorg.springframework.web.context.request.ServletRequestAttributes;
importjavax.servlet.http.HttpServletRequest;
importjava.util.Date;
importjava.util.Enumeration;
/**
*日志插入
* Created by houpe on 2017/4/12.
*/
@Aspect
@Component
public class LogAdvice{
public static finalLoggerlogger=Logger.getLogger(LogAdvice.class);
@Autowired
ISysLogServicesysLogService;
/**
*切面配置
*/
// @Pointcut(value = "execution(* cn.fy3d.boot.*.*.*Controller.*(..))")
@Pointcut(value="execution(* cn.fy3d.boot.*.*..*(..))")
public voidlog() {
}
@Before("log()")
public voiddoBefore() {
}
@Around("log()")
publicObjectrecordSysLog(ProceedingJoinPointpoint)throwsThrowable{
//方法名
String strMethodName= point.getSignature().getName();
//类名
String strClassName= point.getTarget().getClass().getName();
//参数
Object[]params= point.getArgs();
StringBuffer bfParams=newStringBuffer();
EnumerationparaNames=null;
HttpServletRequest request=null;
//url
String url=null;
if(params!=null&¶ms.length>0) {
request= ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
paraNames=request.getParameterNames();
url=request.getRequestURI();
String key;
String value;
while(paraNames.hasMoreElements()) {
key=paraNames.nextElement();
value=request.getParameter(key);
bfParams.append(key).append("=").append(value).append("&");
}
if(StringUtils.isBlank(String.valueOf(bfParams))) {
bfParams.append(request.getQueryString());
}
}
String strMessage=String.format("[类名]:%s,[方法]:%s,[参数]:%s,[URL]:%s",strClassName,strMethodName,bfParams.toString(),url);
logger.info(strMessage);
// if (isWriteLog(strMethodName)) {
if(true) {
try{
Subject currentUser=SecurityUtils.getSubject();
SecurityUtils.getSubject().getPrincipal();
PrincipalCollection collection=currentUser.getPrincipals();
if(null!=collection) {
String loginName=collection.getPrimaryPrincipal().toString();
SysLog sysLog=newSysLog();
sysLog.setTitle(strMethodName);
sysLog.setUserName(loginName);
sysLog.setCreateTime(newDate());
sysLog.setDelFlag("0");
sysLog.setParams(bfParams.toString());
if(request!=null) {
sysLog.setUrl(request.getRequestURI());
sysLog.setIp(request.getRemoteAddr());
}
logger.info(sysLog.toString());
sysLogService.insert(sysLog);
}
}catch(Exceptione) {
logger.error(e.getMessage(),e);
}
}
returnpoint.proceed();
}
private booleanisWriteLog(Stringmethod) {
String[]pattern= {"index","manager","dataGrid","add","addpage","edit","editpage","delete","grant"};
for(String s:pattern) {
if(method.indexOf(s) > -1) {
return true;
}
}
return false;
}
}
报错说:RequestContextHolder为空
原因出现在这里,
@Pointcut(value="execution(* cn.fy3d.boot.*.*..*(..))")
本类LogAdvice是通过@Aspect实现的方法拦截器,上面的切面规则,会拦截所有方法,而RequestContextHolder是springmvc中的上下文的Request容器,只会在controller中存在,其他方法中肯定是null、
解决办法:修改切面规则,定位到controller
@Pointcut(value = "execution(* cn.fy3d.boot.*.*.*Controller.*(..))")
网友评论