Spring AOP开启操作日志记录功能

定义注解属性:
import java.lang.annotation.ElementType;  
import java.lang.annotation.Retention;  
import java.lang.annotation.RetentionPolicy;  
import java.lang.annotation.Target;  
  
@Retention(RetentionPolicy.RUNTIME)  
@Target({ElementType.METHOD})  
public @interface BussAnnotation {  
    
    String dataTyp() default "";  //数据类型
    
    String changeRmk() default ""; //修改变动描述 
    

配置切入

@Aspect
@Component
public class LogInterceptor {


@Autowired
LogOperService logOpeService;


@Pointcut("@annotation(com.yunda.base.common.annotation.BussAnnotation)")
public void logPointCut() {
}


@Around("logPointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
long beginTime = System.currentTimeMillis();
// 执行方法
Object result = point.proceed();
// 执行时长(毫秒)
long time = System.currentTimeMillis() - beginTime;
// 异步保存日志
saveLog(point, time);
return result;
}


@Async
void saveLog(ProceedingJoinPoint joinPoint, long time) throws InterruptedException {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
BussAnnotation syslog = method.getAnnotation(BussAnnotation.class);
// 请求的方法名
String className = joinPoint.getTarget().getClass().getName();
String methodName = signature.getName();
// 获取request
HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
// 获取当前登录人信息
UserDO currUser = ShiroUtils.getUser();


// 请求的参数
Object[] args = joinPoint.getArgs();
String params = JSONUtils.beanToJson(args[0]);


LogOperDO logoper = new LogOperDO();
logoper.setSeqId(DateUtils.formatTodaty("yyyyMMddHHmmss") + (int) ((Math.random() * 9 + 1) * 10000));
Map sys = JSONUtils.jsonToMap(params);
logoper.setOperSeqId(sys.get("seqId") + "");
logoper.setChangeRmk("操作类型:" + syslog.changeRmk() + "----请求的方法名:" + className + "." + methodName + "()"
+ "------传入参数:" + params + "-----ip地址:" + IPUtils.getIpAddr(request));
// 保存系统日志
logoper.setOperUsrCd(currUser.getUserId() + "");
logoper.setOperUsrNm(currUser.getUsername());
logoper.setDataTyp(syslog.dataTyp());
logOpeService.save(logoper);


}


}


使用注解


猜你喜欢

转载自blog.csdn.net/kwmnitw/article/details/80741317
今日推荐