它可以註解到自訂註解的兩個屬性上,表示這兩個互為別名,也就是說這兩個屬性其實同一個意義。
其中一個屬性名稱必須是"value"
#無論指明設定哪個屬性名稱設定屬性值,另一個屬性名稱也是同樣屬性值,也可以缺省屬性名。
若兩個都指明屬性值,要求值必須相同,否則會報錯。
使用簡潔。這樣使用之後,@MyAnno(location="shanghai")可以直接寫成:@MyAnno("shanghai");
這個功能產生的原因:
#若自訂註解有一個屬性,且該屬性命名上為了體現其意義,呼叫方必須每次使用自訂註解的時候,都必須寫明屬性,然後設定,這樣稍微麻煩。
註解
package com.example.annotation; import org.springframework.core.annotation.AliasFor; import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.TYPE}) @Documented @Inherited public @interface MyAnnotation { @AliasFor(attribute = "location") String value() default ""; @AliasFor(attribute = "value") String location() default ""; }
#控制器
package com.example.controller; import com.example.annotation.MyAnnotation; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/hello") public class HelloController { @MyAnnotation(value = "location") /*//下边这两种写法结果与上边是相同的 @MyAnnotation("location") @MyAnnotation(location = "location")*/ @RequestMapping("/test1") public String test1() { MyAnnotation myAnnotation = null; try { myAnnotation = AnnotationUtils.getAnnotation(this.getClass().getMethod("test1"), MyAnnotation.class); } catch (NoSuchMethodException e) { e.printStackTrace(); } return "value:" + myAnnotation.value() + ";loation:" + myAnnotation.location(); } }
#測試
前端存取:http://localhost:8080/hello/test1
前端結果(value和location都是同一個值)
value:location; loation:location
子註解的屬性值的讀寫,其實是對父註解的屬性值的讀寫。 (對繼承的屬性來說)
此時,只能讀寫繼承了的屬性值。
註解
package com.example.annotation; import org.springframework.core.annotation.AliasFor; import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.TYPE}) @Documented @Inherited public @interface MyAnnotation { @AliasFor(attribute = "location") String value() default ""; @AliasFor(attribute = "value") String location() default ""; }
package com.example.annotation; import org.springframework.core.annotation.AliasFor; import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.TYPE}) @Documented @Inherited @MyAnnotation public @interface SubMyAnnotation { @AliasFor(annotation = MyAnnotation.class) String location() default ""; // 这个不能写,只能有一个与父属性名同名的属性,否则报错 // @AliasFor(annotation = MyAnnotation.class) // String value() default ""; }
控制器
package com.example.controller; import com.example.annotation.MyAnnotation; import com.example.annotation.SubMyAnnotation; import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/hello") public class HelloController { @SubMyAnnotation(location = "location(my)") @RequestMapping("/test") public String test() { SubMyAnnotation subMyAnnotation = null; MyAnnotation myAnnotation = null; MyAnnotation myAnnotation1 = null; try { subMyAnnotation = AnnotationUtils.getAnnotation(this.getClass().getMethod("test"), SubMyAnnotation.class); myAnnotation = AnnotationUtils.getAnnotation(this.getClass().getMethod("test"), MyAnnotation.class); myAnnotation1 = AnnotatedElementUtils.getMergedAnnotation(this.getClass().getMethod("test"), MyAnnotation.class); } catch (NoSuchMethodException e) { e.printStackTrace(); } return "loation(sub):" + subMyAnnotation.location() + "\n" + "location:" + myAnnotation.location() + "\n" + "location:" + myAnnotation1.location(); } }
測試
前端存取:http://localhost:8080/hello/test
#結果
loation(sub):location(my)
location:
location:location(my)
子註解的屬性值的讀寫,其實是對父註解的屬性值的讀寫。 (對重寫的屬性來說)
無論指明設定哪個屬性名設定屬性值,另一個屬性名也是同樣屬性值,不可以缺省屬性名。
若兩個都指明屬性值,要求值必須相同,否則會報錯。
註解
package com.example.annotation; import org.springframework.core.annotation.AliasFor; import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.TYPE}) @Documented @Inherited public @interface MyAnnotation { @AliasFor(attribute = "location") String value() default ""; @AliasFor(attribute = "value") String location() default ""; }
package com.example.annotation; import org.springframework.core.annotation.AliasFor; import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.TYPE}) @Documented @Inherited @MyAnnotation public @interface SubMyAnnotation { @AliasFor(attribute = "value", annotation = MyAnnotation.class) String subValue() default ""; @AliasFor(attribute = "location", annotation = MyAnnotation.class) String subLocation() default ""; // subLocation属性写成下边这两种结果是一样的 // @AliasFor(attribute = "value", annotation = MyAnnotation.class) // String subLocation() default ""; // @AliasFor(value = "location", annotation = MyAnnotation.class) // String subLocation() default ""; // }
控制器
package com.example.controller; import com.example.annotation.MyAnnotation; import com.example.annotation.SubMyAnnotation; import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/hello") public class HelloController { @SubMyAnnotation(subValue = "subLocation") // @SubMyAnnotation(subLocation = "subLocation") //这个与上边结果相同 // @SubMyAnnotation("subLocation") //缺省属性名会报错 @RequestMapping("/test") public String test() { SubMyAnnotation subMyAnnotation = null; MyAnnotation myAnnotation = null; MyAnnotation myAnnotation1 = null; try { subMyAnnotation = AnnotationUtils.getAnnotation(this.getClass().getMethod("test"), SubMyAnnotation.class); myAnnotation = AnnotationUtils.getAnnotation(this.getClass().getMethod("test"), MyAnnotation.class); myAnnotation1 = AnnotatedElementUtils.getMergedAnnotation(this.getClass().getMethod("test"), MyAnnotation.class); } catch (NoSuchMethodException e) { e.printStackTrace(); } return "subValue:" + subMyAnnotation.subValue() + ";subLoation:" + subMyAnnotation.subLocation() + "\n" + "value:" + myAnnotation.value() + ";location:" + myAnnotation.location() + "\n" + "value:" + myAnnotation1.value() + ";location:" + myAnnotation1.location(); } }
測試
前端存取:http://localhost:8080/hello/test
結果
#subValue:subLocation;subLoation:subLocation
value:;location:
value:subLocation;location:subLocation
以上是SpringBoot中的@AliasFor註解怎麼使用的詳細內容。更多資訊請關注PHP中文網其他相關文章!