java - 修改自定义注解的参数值

标签 java spring annotations refactoring

有没有办法实现注解,以便自己改变他的参数值?

例如:

我想创建自定义 RequestMapping 注释以消除一些代码重复。

当前代码:

@RequestMapping("/this/is/duplicate/few/times/some")
public class SomeController {
}

我想创建这样的东西

@Retention(RetentionPolicy.RUNTIME)
@RequestMapping()
public @interface CustomRequestMapping {

    String value() default "";

    @AliasFor(annotation = RequestMapping.class, attribute = "value")
    String "/this/is/duplicate/few/times"+value();

}

为了将请求映射值减少到此:

@CustomRequestMapping("/some")
public class SomeController {
}

不幸的是,我找不到使其可编译的方法。 或者也许有一种方法可以使用 AliasFor 注释将参数传递到目标数组。像这样的事情:

@Retention(RetentionPolicy.RUNTIME)
    @RequestMapping()
    public @interface CustomRequestMapping {

    @AliasFor(annotation = RequestMapping.class, attribute = "value{1}")
    String value() default "";

    @AliasFor(annotation = RequestMapping.class, attribute = "value{0}")
    String prefixPath() default "/this/is/duplicate/few/times";

}

最佳答案

您似乎正在尝试创建注释的子类型并修改其属性默认值之一

注释的子类型是不可能的,here JSR 是否说明了原因。

It complicates the annotation type system,and makes it much more difficult
to write “Specific Tools”.

“Specific Tools” — Programs that query known annotation types of arbitrary
 external programs. Stub generators, for example, fall into this category.
 These programs will read annotated classes without loading them into the 
 virtual machine, but will load annotation interfaces.

关于java - 修改自定义注解的参数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41375412/

相关文章:

java - NPE 在尝试在 java 中创建多个记录器时使用静态 ThreadLocal 记录器

java - Android Javascript 接口(interface)

java - Spring ToStringCreator 的使用

iPhone map 套件集群精确定位

LinkedList输出null的Java实现

java - 在 recyclerView android 上只能点击一个项目

java - spring security框架中不使用ROLE_前缀的影响

java - Spring REST 将字段添加到 404 响应代码

Java:在编译时强制注释靠近另一个注释

php - 将缓存添加到 Zend\Form\Annotation\AnnotationBuilder