package com.module.aop;
import java.net.InetAddress;
import java.util.Date;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import com.easthope.module.dao.XmsVisitorOperateMapper;
import com.easthope.module.model.XmsVisitorOperate;
import com.easthope.sys.model.TblUser;
import com.easthope.sys.util.ResourceUtil;
import com.easthope.sys.util.UUIDGenerator;
import net.sf.json.JSONArray;
@Aspect
public class VisitorOperateAop {
@Autowired
XmsVisitorOperateMapper visitorOperateMapper;
/**
* insertCell(添加业务逻辑方法切入点 去除日志的所有方法)
* @return void
* @author zhanh247
* @date 2018年9月29日 下午3:53:00
* @exception
*
*/
@Pointcut("execution(* com.easthope.*.service.impl.*.save*(..)) && !execution(* com.easthope.module.service.impl.Visitor*.*(..)) && !execution(* com.easthope.sys.service.impl.SynUserServiceImpl.*(..))")
public void insertCell() {
}
/**
*
* updateCell(修改操作日志(后置通知) )
*
* @return void
* @author zhanh247
* @date 2018年9月29日 下午4:21:06
* @exception
*
*/
@Pointcut("execution(* com.easthope.*.service.impl.*.update*(..)) && !execution(* com.easthope.module.service.impl.Visitor*.*(..)) && !execution(* com.easthope.sys.service.impl.SynUserServiceImpl.*(..))")
public void updateCell() {
}
/**
*
* deleteCell(删除日志)
*
* @return void
* @author zhanh247
* @date 2018年9月29日 下午5:03:46
* @exception
*
*/
@Pointcut("execution(* com.easthope.*.service.impl.*.delete*(..)) && !execution(* com.easthope.module.service.impl.Visitor*.*(..)) && !execution(* com.easthope.sys.service.impl.SynUserServiceImpl.*(..))")
public void deleteCell() {
}
/**
*
* opContent(删除操作)
*
* @param joinPoint
* @return
* @return String
* @author zhanh247
* @date 2018年9月29日 下午3:56:32
* @exception
*
*/
public String opContent(JoinPoint joinPoint) {
// 获取类名
String className = joinPoint.getTarget().getClass().getName();
// 获取方法名
String methodName = joinPoint.getSignature().getName();
// 获取参数
Object[] args = joinPoint.getArgs();
String str = "执行了 " + className + " 类的 " + methodName + " 方法";
return str;
}
@AfterReturning(value = "insertCell()", argNames = "object", returning = "object")
public void insertLog(JoinPoint joinPoint, Object object) throws Throwable {
// 判断参数
if (joinPoint.getArgs() == null) {// 没有参数
return;
}
TblUser user = ResourceUtil.getSessionUserName();
XmsVisitorOperate xvo = new XmsVisitorOperate();
xvo.setId(UUIDGenerator.getId());
xvo.setIpAddr(InetAddress.getLocalHost().toString().substring(InetAddress.getLocalHost().toString().lastIndexOf("/") + 1));
xvo.setOperateUri(opContent(joinPoint));
xvo.setUserCode(user.getUsername());
xvo.setOperateTime(new Date());
xvo.setOperate(TypeEnum.SAVE.name());
xvo.setRequestParam(JSONArray.fromObject(joinPoint.getArgs()).toString());
xvo.setDeleted(0);
xvo.setGmtCreate(new Date());
xvo.setGmtCreateUser(user.getUsername());
xvo.setUserName(user.getPerson().getName());
visitorOperateMapper.insert(xvo);
}
@AfterReturning(value = "updateCell()", argNames = "object", returning = "object")
public void updateLog(JoinPoint joinPoint, Object object) throws Throwable {
// 判断参数
if (joinPoint.getArgs() == null) {// 没有参数
return;
}
// 创建日志对象
TblUser user = ResourceUtil.getSessionUserName();
XmsVisitorOperate xvo = new XmsVisitorOperate();
xvo.setId(UUIDGenerator.getId());
xvo.setIpAddr(InetAddress.getLocalHost().toString().substring(InetAddress.getLocalHost().toString().lastIndexOf("/") + 1));
xvo.setOperateUri(opContent(joinPoint));
xvo.setUserCode(user.getUsername());
xvo.setOperateTime(new Date());
xvo.setOperate(TypeEnum.UPDATE.name());
xvo.setRequestParam(JSONArray.fromObject(joinPoint.getArgs()).toString());
xvo.setDeleted(0);
xvo.setGmtCreate(new Date());
xvo.setGmtCreateUser(user.getUsername());
xvo.setUserName(user.getPerson().getName());
visitorOperateMapper.insert(xvo);
}
@AfterReturning(value = "deleteCell()", argNames = "object", returning = "object")
public void deleteLog(JoinPoint joinPoint, Object object) throws Throwable {
if (joinPoint.getArgs() == null) {// 没有参数
return;
}
// 创建日志对象
TblUser user = ResourceUtil.getSessionUserName();
XmsVisitorOperate xvo = new XmsVisitorOperate();
xvo.setId(UUIDGenerator.getId());
xvo.setIpAddr(InetAddress.getLocalHost().toString().substring(InetAddress.getLocalHost().toString().lastIndexOf("/") + 1));
xvo.setOperateUri(opContent(joinPoint));
xvo.setUserCode(user.getUsername());
xvo.setOperateTime(new Date());
xvo.setOperate(TypeEnum.DELETE.name());
xvo.setRequestParam(JSONArray.fromObject(joinPoint.getArgs()).toString());
xvo.setDeleted(0);
xvo.setGmtCreate(new Date());
xvo.setGmtCreateUser(user.getUsername());
xvo.setUserName(user.getPerson().getName());
visitorOperateMapper.insert(xvo);
}
}
spring-mvc.xml 总配置
<!-- 日志 -->
<aop:aspectj-autoproxy />
<bean id="logBean" class="com.easthope.module.aop.VisitorOperateAop"></bean>
要拦截service中的增删改方法,命名一定要规范例如save、update、delete开头,这样配置表达式时可以用save*、update*、delete*来拦截对应的数据库操作