How can I do Java annotation like @name(“Luke”) with no attribute inside parenthesis?

前端 未结 3 1167
醉梦人生
醉梦人生 2020-12-29 01:57

How I can do custom Java annotation with no attribute name inside parentheses?

I don\'t want this: @annotation_name(att=valor). I just want like in Serv

3条回答
  •  情深已故
    2020-12-29 02:16

    You specify an attribute named value:

    public @interface MyAnnotation {
    
        String value();
    
    }
    

    This doesn't have to be the only attribute if they have default values:

    public @interface MyAnnotation {
    
        String value();
        int myInteger() default 0;
    
    }
    

    But if you want to explicitly assign a value to the attribute other than value, you then must explicitly assign value. That is to say:

    @MyAnnotation("foo")
    @MyAnnotation(value = "foo", myInteger = 1)
    

    works

    but

    @MyAnnotatino("foo", myInteger = 1)
    

    does not

提交回复
热议问题