AOP实现日志记录

mac2024-05-26  36

@Aspect @Component @Slf4j public class AopLog { @Autowired private EmployeeActionLogService employeeActionLogService; @Autowired EmployeeActionLogDAO employeeActionLogDAO; private List<SensitivityOperation> sensitivityOperations = null; @Pointcut("execution(public * com.i2shopping.i2work.operation.action.*.*Action.*(..))") public void log() { } /** * 前置操作 * @param point 切入点 */ @Before("log()") public void beforeLog(JoinPoint point) throws Throwable { //point.getSignature().getDeclaringTypeName() 获取包名和类名 //point.getSignature().getName() 获取方法名 String name = point.getSignature().getDeclaringTypeName() + '.' + point.getSignature().getName(); if (sensitivityOperations == null) { sensitivityOperations = employeeActionLogService.getIntercepts(); } //返回符合表达式的集合的第一个对象,如果没有,创建一个新对象 SensitivityOperation operation = sensitivityOperations.stream().filter(sensitivityOperation -> StringUtils.equalsIgnoreCase(sensitivityOperation.getOperation(), name)).findFirst().orElse(new SensitivityOperation()); Boolean intercept = operation.getIntercept(); if (null == intercept || !intercept) { return; } EmployeeActionLog employeeActionLog = new EmployeeActionLog(); //不在controller层获取request ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = Objects.requireNonNull(attributes).getRequest(); Date date = new Date(); String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date); Timestamp start = Timestamp.valueOf(nowTime); employeeActionLog.setOperationUrl(request.getRequestURI()); employeeActionLog.setTypeName(name); employeeActionLog.setEmployeeId(onlineObject.getEmployeeId()); employeeActionLog.setEmployeeName(onlineObject.getUserName()); employeeActionLog.setOperationDate(start); employeeActionLog.setOperation(operation.getValue()); employeeActionLogService.addNewAction(employeeActionLog); } }
最新回复(0)