一、pom.xml引入aopjava
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
二、自定義註解spring
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Log { String value() default ""; }
三、定義aspectjapp
@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註解spring-boot
@RequestMapping("user/batchAdd.do") @Log public Boolean batchAdd(UserListDTO userListDTO){ return userService.batchAdd(userListDTO.getUserList()); }
控制檯輸出:ui
[ INFO ] [ com.ss.quickStart.aop.LogAspectj : 33 ] - 方法:UserController.batchAdd,參數:[{"userList":[{"name":"A","sex":1},{"name":"B","sex":0}]}]code