1、pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
2、自定义注解类
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface BusiLog {
String busCode();
String desc() default "";
}
3、定义aop类
@Aspect
@Component
public class BusiLogAspect {
private static final Logger logger = LoggerFactory.getLogger(BusiLogAspect.class);
@Pointcut("@annotation(cn.annotion.BusiLog)")
public void auditAspect(){
}
// 方法执行前,打印入参日志
@Before("auditAspect()")
public void doBefore(JoinPoint pjp) {
logger.info("Time:{}, ARGS:{}", System.currentTimeMillis(), Arrays.toString(joinPoint.getArgs()));
}
}
4、使用
@RestController
public class TestController {
@BusiLog
@GetMapping("/hello")
public String hello(@RequestParam String param){
return "hello " + param;
}
}