Springmvc断面ログ
a
a
フェースクラスの必要性とactionはxmlファイルに配置されています:spring-mvc.xml
package com.sniper.common.advice;
public interface ILogger {
}
a
package com.sniper.common.advice;
/**
*
*/
@Aspect
public class Logger implements ILogger {
@Resource(name="logService")
private LogService logService ;
//="(execution(* *..*Service*.*(..))) !(execution(* *..sequence.*SequenceService*.*(..))) !(execution(* *..forminfo.*SequenceService*.*(..))) !(execution(* *..sjcx.*LogService*.*(..))) !bean(logService)
@Pointcut("execution (* *..*.service.sjcx.*PopulationService*.*(..))")
//@Pointcut("(execution(* *..*Service*.*(..))) and !bean(logService)")
//@Pointcut("(execution (* *..*.service.sjcx.*Service*.*(..))) and !(execution (* *..*.service.sjcx.*LogService*.*(..))) and !bean(logService)")
public void servicePointCut() {
}
@Pointcut("execution(* *..*Action*.*(..))")
public void actionPointCut() {
}
/**
*
*/
@Around("actionPointCut()")
public Object record(ProceedingJoinPoint pjp){
LogVo logVo = new LogVo();
try {
HttpServletRequest req = RequestUtil.getRequest();
Object obj = RequestUtil.getSessionAttribute(IndexAction.LOGIN_USER_SESSION_KEY);
if(obj != null) {
User user = (User)obj;
logVo.setOperator(user.getAccount());
logVo.setOperatorName(user.getUserName());
} else {
logVo.setOperator("");
logVo.setOperatorName("");
}
String ctx = req.getContextPath();
String uri = req.getRequestURI();
if(!StringUtil.isBlankOrNull(ctx) && !StringUtil.isBlankOrNull(uri)) {
if(uri.contains(ctx)) {
uri = uri.split(ctx)[1];
}
}
logVo.setUri(uri);
logVo.setFunctionName(FunctionUtil.getFunctionName(uri));
// ,
logVo.setYear(DateUtil.getYear());
//operName,
//String methodName = pjp.getSignature().getName();
//log.setOperName(methodName);
//operParams,
Object[] args = pjp.getArgs();
String parameter = "";
for(Object o : args) {
if(o != null) {
if(o instanceof ModelMap) {
continue;
}
if(o instanceof HttpServletRequest) {
continue;
}
if(o instanceof Map) {
Collection coll = ((Map) o).values();
if(coll != null && !coll.isEmpty()) {
for(Object oo : coll) {
if(oo != null && !StringUtil.isBlankOrNull(oo.toString())) {
parameter = parameter + oo + ",";
}
}
}
} else {
if(!StringUtil.isBlankOrNull(o.toString())) {
parameter = parameter + o + ",";
}
}
}
}
logVo.setParameter(StringUtil.deal(parameter));
//ip
String ip = "";
if (req.getHeader("x-forwarded-for") == null) {
ip = req.getRemoteAddr();
} else {
ip = req.getHeader("x-forwarded-for");
}
ip = "0:0:0:0:0:0:0:1".equals(ip)?"127.0.0.1":ip;
logVo.setIp(ip);
//
Object ret = pjp.proceed();
return ret ;
} catch (Throwable e) {
e.printStackTrace();
} finally {
logService.addLog(logVo);
}
return null ;
}
}
フェースクラスの必要性とactionはxmlファイルに配置されています:spring-mvc.xml
<aop:aspectj-autoproxy/>
<bean class="com.sniper.common.advice.Logger" />
<import resource="classpath*:META-INF/web/*.xml" />
<context:component-scan base-package="com.sniper.common.web.action" />