自定义注解:

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface DemoAnno {
    String value()  default "";
}

AOP: 

@Pointcut("@annotation(com.hephae.aop.aop.DemoAnno)")
    public void demoAspect() {
    }

    @Around(value = "demoAspect()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature)signature;
        //method为接口的Method对象,获取不到实现类方法上的注解
        Method method = methodSignature.getMethod();
        //targetMethod为实现类方法对象
        Method targetMethod = joinPoint.getTarget().getClass().getMethod(method.getName(), method.getParameterTypes());
        //获取注解
        DemoAnno demoAnno = targetMethod.getAnnotation(DemoAnno.class);
        if (demoAnno != null) {
            String value = demoAnno.value();
        }
        Object obj = null;
        obj = joinPoint.proceed();
        return obj;
    }    

 

相关文章:

  • 2021-09-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-13
  • 2021-07-24
猜你喜欢
  • 2021-12-25
  • 2022-12-23
  • 2022-12-23
  • 2021-08-30
  • 2021-06-10
  • 2021-10-26
相关资源
相似解决方案