如何从我在Spring AOP中的建议方法中的Custom Annotation中获取值?

vtwuwzda  于 2022-10-23  发布在  Spring
关注(0)|答案(2)|浏览(141)

我的自定义注解如下所示:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface CutomAnnotation{

    String value() default "";
}

我的方面类如下所示:

@Aspect
@Component
public class MyCustomAspect{

@Around("@annotation(com.forceframework.web.handlers.monitoring.MeterRegTimer)")
public Object aroundJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable{

    System.out.println("Timer started: "+joinPoint.getSignature());
    Object objToReturn=joinPoint.proceed();
    System.out.println("Timer ended: "+joinPoint.getSignature());

    return objToReturn;
    }
}

我在控制器类中使用注解的位置:

.
.
.
@CustomAnnotation(value="timer")
@GetMapping(value="/test")
public ResponseEntity test(){
}
.
.
.

我想知道我是否可以访问从我的“CustomAnnotation”传递的“Value”,该值位于“About”建议方法中--“MyCustomAspect”类中的“aroundJoinPoint”。
区块报价

9udxz4iz

9udxz4iz1#

您的建议应如下所示声明:

@Around("@annotation(customAnnotationArgumentName)")
public Object aroundJoinPoint(ProceedingJoinPoint joinPoint, CustomAnnotation customAnnotationArgumentName) throws Throwable {
// ...
}

有关详细信息,请参阅文档。

fcwjkofz

fcwjkofz2#

MethodSignature signature = (MethodSignature) joinPoint.getSignature();
    Method method = signature.getMethod();
    CutomAnnotation methodAnnotation = method.getDeclaredAnnotation(CutomAnnotation.class);

相关问题