1.annotation 注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodLog {
String value() default “”;
}
2.手写Aop实现
@Aspect
@Component
public class MethodLogAspect {
@Pointcut("@annotation(cc.mrbird.common.annotation.MethodLog)")
public void pointcut() {
// do nothing
}
@Around("pointcut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
Object result;
long beginTime = System.currentTimeMillis();
System.out.println("=============开始时间==========="+beginTime);
result = point.proceed();
long endTime = System.currentTimeMillis();
//获取方法名
MethodSignature signature = (MethodSignature) point.getSignature();
System.out.println("=============方法名==========="+signature.getMethod());
System.out.println("=============方法结束==========="+endTime);
return result;
}
}
- Aop 代码中的使用
@MethodLog(“获取客户信息”)
@Log(“获取用户信息”)
@RequestMapping(“user/list”)
@PreAuthorize(“hasAuthority(‘user:list’)”)
@ResponseBody
public Map<String, Object> userList(QueryRequest request, MyUser user) {
return super.selectByPageNumSize(request, () -> this.userService.findUserWithDept(user));
}