领导要求进行用户操作行为跟踪,我是用aop来做,在使用切点时匹配了相应的方法,可是无法排除指定类的方法,经过个总努力,错误在
@Pointcut("execution(* cn.jesong.webcall.console.service..add*(..)) && !execution(* cn.jesong.webcall.console.service.LogService.*(..))")
之前看是写成了
@Pointcut("execution(* cn.jesong.webcall.console.service..add*(..)) and !execution(* cn.jesong.webcall.console.service.LogService.*(..))")
and和&&的错误,铭记!!!
package cn.jesong.webcall.console.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import cn.jesong.webcall.console.dto.CustomerDto;
import cn.jesong.webcall.console.dto.LogDto;
import cn.jesong.webcall.console.service.LogService;
@Component
@Aspect
public class LogForUserAction {
@Autowired
private LogService logService;
@Pointcut("execution(* cn.jesong.webcall.console.service..add*(..)) && !execution(* cn.jesong.webcall.console.service.LogService.*(..))")
public void pointUserAdd(){}
@Pointcut("execution(* cn.jesong.webcall.console.service..update*(..)) ! execution(* cn.jesong.webcall.console.service.LogService.*(..))")
public void pointUserUpdate(){}
@Pointcut("execution(* cn.jesong.webcall.console.service..delete*(..)) ! execution(* cn.jesong.webcall.console.service.LogService.*(..))")
public void pointUserDelete(){}
@Pointcut("execution(* cn.jesong.webcall.console.service.*.get*(..)) ! execution(* cn.jesong.webcall.console.service.LogService.*(..))")
public void pointUserGet(){}
@AfterReturning("pointUserAdd()")
public void noteUserAdd(JoinPoint joinPoint){
this.addAction(joinPoint,"insert");
}
@AfterReturning("pointUserUpdate()")
public void noteUserUpdate(JoinPoint joinPoint){
this.addAction(joinPoint,"update");
}
@AfterReturning(value="pointUserDelete()")
public void noteUserDelete(JoinPoint joinPoint){
this.addAction(joinPoint,"delete");
}
@AfterReturning(value="pointUserGet()")
public void noteUserGet(JoinPoint joinPoint){
this.addAction(joinPoint,"select");
}
public void addAction(JoinPoint joinPoint,String behavior){
System.out.println("this is "+behavior);
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
CustomerDto customer = (CustomerDto)request.getSession().getAttribute("customer");
System.out.println(customer.getCustomerId()+" aaaa "+customer.getUserName()+" aaaa "+ customer.getPassword());
if(customer==null){
return;
}
if(joinPoint.getArgs() == null){
return ;
}
//获得方法名
String methodName = joinPoint.getSignature().getName();
//获取操作内容
String acContext = getUserContext(joinPoint.getArgs(),methodName);
//创建日志对象
LogDto logDto = new LogDto();
logDto.setUserid(Integer.parseInt(customer.getCustomerId()));
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = format.format(new Date());
try {
logDto.setTime(format.parse(time));
} catch (ParseException e1) {
e1.printStackTrace();
}
logDto.setInfo(acContext);
logDto.setBehavior(behavior);
try {
logService.addLog(logDto);
} catch (Exception e) {
e.printStackTrace();
}
}
public String getUserContext(Object[] args,String methodName){
String context =null;
//参数
StringBuffer sb = new StringBuffer();
for (int i = 0; i < args.length; i++) {
sb.append(args[i]);
}
context = sb+methodName;
return context;
}
}