1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
@Aspect @Component @Profile({"self", "dev", "prod"}) public class OperationLogAspect {
@Autowired private ConfigurableApplicationContext publisher;
@Pointcut("execution(* com.epet.microservices.user.service..impl..*(..))") public void operationLogPointcut() { }
@Around("operationLogPointcut()") public Object around(ProceedingJoinPoint joinPoint) { try { Signature signature = joinPoint.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; Method targetMethod = methodSignature.getMethod(); Operation operation = targetMethod.getAnnotation(Operation.class); if (operation != null) { byte operationType; switch (operation.operation()) { case DELETE: operationType = (byte) 3; break; case UPDATE: operationType = (byte) 2; break; default: operationType = (byte) 1; } Object[] args = joinPoint.getArgs(); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); StringBuffer sb = new StringBuffer(); for (Object object : args) { sb.append(objectMapper.writeValueAsString(object)); } Long acId = ThreadHolderUtil.getUser() == null || ThreadHolderUtil.getUser().getAcId() == null ? 0L : ThreadHolderUtil.getUser().getAcId(); OperationLog operationLog = new OperationLog().setOperationColumn(sb.toString()).setOperationTable(operation.table()).setAcId(acId).setOperationType(operationType); publisher.publishEvent(new OperationEvent(operationLog)); } return joinPoint.proceed(); } catch (Throwable throwable) { throwable.printStackTrace(); } return null; }
}
|
近期评论