1、pom.xml引入aop
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
2、自定义注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Log {
String value() default "";
}
3、定义aspectj
@Aspect
@Component
public class LogAspectj {
private static final Logger LOG = LoggerFactory.getLogger(LogAspectj.class);
@Before(value = "@annotation(com.ss.quickStart.annotation.Log)")
public void logAfterReturning(JoinPoint joinPoint){
String className = joinPoint.getTarget().getClass().getSimpleName();
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
LOG.info("方法:{}.{},参数:{}",className,methodName,JSON.toJSONString(args));
}
}
使用@Log注解
@RequestMapping("user/batchAdd.do")
@Log
public Boolean batchAdd(UserListDTO userListDTO){
return userService.batchAdd(userListDTO.getUserList());
}
控制台输出:
[ INFO ] [ com.ss.quickStart.aop.LogAspectj : 33 ] - 方法:UserController.batchAdd,参数:[{"userList":[{"name":"A","sex":1},{"name":"B","sex":0}]}]