有没有办法实现注释以便自己更改参数值?
例如:
我想创建自定义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();
}
为了将Request Mapping值减少到:
@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";
}
答案 0 :(得分:2)
看起来你正试图制作一个注释的子类型并修改其中一个属性的默认值
无法对注释进行子类型化,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.
答案 1 :(得分:0)
复制问题的一个解决方案可能是提取常量。
@Annotation(MyClass.FOO+"localValue")
public class MyClass
{
public static final String FOO = "foo";
...
}