AOP日志记录
package com.longshare.custom.widget.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
@Getter
@AllArgsConstructor
public enum LogType {
BOARD_ADD,
BOARD_DELETE,
BOARD_UPDATE,
COLUMN_ADD,
COLUMN_DELETE,
COLUMN_UPDATE,
GROUP_ADD,
GROUP_DELETE,
GROUP_UPDATE,
DEFAULT,
}
@Documented
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface BoardChangeLog {
@AliasFor("type")
LogType value() default LogType.DEFAULT;
@AliasFor("value")
LogType type() default LogType.DEFAULT;
}
@Aspect
@Component
@Order(1)
@Slf4j
public class BoardDefChangeAspect {
@Pointcut("@annotation(com.longshare.custom.widget.annotation.BoardChangeLog)")
public void pointBoardChangeCut() {
}
@Around(value = "pointBoardChangeCut()")
public Object aroundMethod(ProceedingJoinPoint joinPoint) {
try {
Object arg = getBoardId(joinPoint, "boardId");
String boardId = arg == null ? null : (String) arg;
BoardDefinitionEntity oldBoardEntity = null;
if (!StringUtils.isEmpty(boardId)) {
oldBoardEntity = boardDefinitionMapper.selectByPrimaryKey(boardId);
}
Object result = joinPoint.proceed();
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method method = methodSignature.getMethod();
if (method != null) {
ColumnChangeLog apiLog = AnnotatedElementUtils.findMergedAnnotation(method, ColumnChangeLog.class);
switch (apiLog.value()) {
case COLUMN_DELETE: {
.....
break;
}
case COLUMN_UPDATE: {
......
break;
}
case COLUMN_ADD: {
Optional<BoardDefinition> optional = (Optional<BoardDefinition>) result;
BoardDefinition newBoard = optional.orElse(null);
if (newBoard != null) {
......
}
break;
}
}
}
return result;
} catch (Throwable throwable) {
log.info("执行异常");
}
return null;
}
private Object getBoardId(ProceedingJoinPoint joinPoint, String arg) {
Object[] args = joinPoint.getArgs();
Signature signature = joinPoint.getSignature();
MethodSignature methodSignature = (MethodSignature) signature;
String[] parameterNames = methodSignature.getParameterNames();
int index = ArrayUtils.indexOf(parameterNames, arg);
return args[index];
}
}