自定义注释中的动态值

时间:2019-11-26 18:02:21

标签: java

是否可以将动态值传递到自定义注释中?

例如如下:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomAnnot {
    String[] requestKeys();
} 

注释时,我想动态传递参数的值。

@CustomAnnot(requestKeys = {"id"}) // or maybe "#id"
public Object get(String id) {}

要注意的是,上面的方法不仅有用。需要某种形式的解析才能起作用。

我该怎么做?尝试查找在线参考,但找不到任何可帮助实现的内容。

Spring中的

@Cacheable能够做到这一点,因此显然这是可能的。试图看看@Cacheable的实现,说实话迷路了。这是巨大的,并且调试困难。我可以就此实现方式获得一些建议,或者是否有人熟悉指向可缓存实现该功能的部分?谢谢。

编辑: 我需要一种从方法中的参数获取值的方法。

示例。

// maybe in this method I only want String b and c
String method1(String a, String b, String c){
    return null;
}

// maybe here I want String a and c
String method2(int d, String a, String b, String c){
    return null;
}

// just 1 argument here so only need String a
String method3(String a){
    return null;
}

我正在寻找一种以一种方式获取我所需要的值的方法。 我只想编写一次此功能并完成操作,而不必再次使用其他方法签名。

因此,我要使用自定义注释,该注释使我可以灵活地决定所需的值,以便可以传递数组。

0 个答案:

没有答案