需求:在方法上添加注解,通过aop获取这个方法,对方法进行处理
// 获取注解的方法,推荐 spring 自带的工具类。
// 1)能获取 代理bean 上的真实target 注解。
// 2)能递归 获取 注解的父子,比如 直接用 RequestMapping 就可以获取 PostMapping 的信息
@Component
@Aspect
@Slf4j
public class WebLogAspect {
@Pointcut("@within(org.springframework.web.bind.annotation.RestController)||@annotation(com.haier.strategy.resolution.config.log.WebLog)")
public void customAnnoPc() {
// 这就是个切面
}
@Around(value = "customAnnoPc()")
public Object doLog(ProceedingJoinPoint pjp) throws Throwable {
Object o = dealLog(pjp);
if (o != null) {
return o;
}
}
public static String dealLog(ProceedingJoinPoint pjp) {
//类上的注解
RequestMapping classAnno = AnnotationUtils.findAnnotation(pjp.getTarget().getClass(), RequestMapping.class);
//方法上的注解
RequestMapping methodAnno = AnnotatedElementUtils.findMergedAnnotation(getMethod(pjp), RequestMapping.class);
String page = classAnno.name(), service = methodAnno.name();
return String.format("%s-%s", page, service);
}
/**
* 获取方法
*/
private static Method getMethod(ProceedingJoinPoint pjp) {
MethodSignature sign = (MethodSignature) pjp.getSignature();
return sign.getMethod();
}
}