@CustomAnnotation(name = "findUser", value = "根据ID查找用户")
@RequestMapping(value = "user/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public User findUser(@PathVariable("id") Integer id) {
return userService.findUserById(id);
}
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class TestLogAspect {
@Pointcut("@annotation(cn.test.CustomAnnotation)")
private void pointcut() {}
@Before("pointcut() && @annotation(annotation)")
public void advice(JoinPoint joinPoint, CustomAnnotation annotation) {
System.out.println(
"类名:["
+ joinPoint.getSignature().getDeclaringType().getSimpleName()
+ "],方法名:[" + joinPoint.getSignature().getName()
+ "]-日志内容-[" + annotation.value() + ", "+annotation.name()+ "]");
}
}