[英]creating custom annotation and using it in java?
I have written below the Custom Annotation. 我已经在“自定义注释”下面写了。
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value();
}
and am using the annotation as below. 并使用以下注释。
@MyAnnotation("someValue")
public void someMethod(){
}
above code is working fine without any issues. 上面的代码工作正常,没有任何问题。 But in the annotation class, value() method name i have to reanme.
但是在注释类中,我必须重命名value()方法名称。 Can i do as below?
我可以做以下事情吗?
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String name();
}
I tried doing but eclipse is giving the compilation error. 我试着做,但日食给编译错误。
- The attribute value is undefined for the annotation type
MyAnnotation
- The annotation @MyAnnotation must define the attribute
name
Any reason? 任何原因?
Use it like this : 像这样使用它:
@MyAnnotation(name="someValue")
public void someMethod(){
}
because by default annotation has value method so if you specify like this 因为默认情况下注解具有值方法,所以如果您这样指定
@MyAnnotation("someValue")
public void someMethod(){
}
it will by default take it as value="someValue"
默认情况下,它将
value="someValue"
为value="someValue"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.