@annotation:用于匹配当前执行方法持有指定注解的方法;
新建一个注解类:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyLogAnnotation {
String name();
}
新建一个切面类:
@Aspect
@Component
public class CustomAspect {
@Pointcut("@annotation(com.lx.bootaop.MyLogAnnotation)")
public void pointAnnotation() {
}
@Before("pointAnnotation() && @annotation(myLogAnnotation)")
public void annotationBefore(JoinPoint joinPoint, MyLogAnnotation myLogAnnotation) {
System.out.println("AOP拦截==执行before:" + joinPoint.getSignature() + ";name=" + myLogAnnotation.name());
}
}
给需要拦截的方法上增加注解:
@MyLogAnnotation(name = "helloMessAnnotation")
public void helloMessAnnotation() {
System.out.println("helloMessAnnotation");
}
测试验证: