SpringBoot中注解@AliasFor的使用詳解
簡(jiǎn)介
本文用示例介紹@AliasFor(別名)注解的用法。
用法1:注解的屬性互為別名
簡(jiǎn)介
它可以注解到自定義注解的兩個(gè)屬性上,表示這兩個(gè)互為別名,也就是說(shuō)這兩個(gè)屬性其實(shí)同一個(gè)含義。
- 其中一個(gè)屬性名必須是"value"
- 無(wú)論指明設(shè)置哪個(gè)屬性名設(shè)置屬性值,另一個(gè)屬性名也是同樣屬性值,也可以缺省屬性名。
- 若兩個(gè)都指明屬性值,要求值必須相同,否則會(huì)報(bào)錯(cuò)。
- 使用簡(jiǎn)潔。這樣使用之后,@MyAnno(location="shanghai")可以直接寫為:@MyAnno("shanghai");
這個(gè)功能產(chǎn)生的原因:
若自定義注解有一個(gè)屬性,且該屬性命名上為了體現(xiàn)其含義,調(diào)用方必須每次使用自定義注解的時(shí)候,都必須寫明屬性 ,然后設(shè)置,這樣稍微麻煩。
實(shí)例
注解
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")
/*//下邊這兩種寫法結(jié)果與上邊是相同的
@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();
}
}測(cè)試
前端訪問(wèn):http://localhost:8080/hello/test1
前端結(jié)果(value和location都是同一個(gè)值)
value:location;loation:location
用法2.繼承父注解的屬性,不重寫屬性名
簡(jiǎn)介
子注解的屬性值的讀寫,其實(shí)是對(duì)父注解的屬性值的讀寫。(對(duì)繼承的屬性來(lái)說(shuō))
此時(shí),只能讀寫繼承了的屬性值。
代碼
注解
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 "";
// 這個(gè)不能寫,只能有一個(gè)與父屬性名同名的屬性,否則報(bào)錯(cuò)
// @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();
}
}測(cè)試
前端訪問(wèn):http://localhost:8080/hello/test
結(jié)果
loation(sub):location(my)
location:
location:location(my)
用法3:繼承父注解的屬性,并重寫屬性名
簡(jiǎn)介
子注解的屬性值的讀寫,其實(shí)是對(duì)父注解的屬性值的讀寫。(對(duì)重寫的屬性來(lái)說(shuō))
無(wú)論指明設(shè)置哪個(gè)屬性名設(shè)置屬性值,另一個(gè)屬性名也是同樣屬性值,不可以缺省屬性名。
若兩個(gè)都指明屬性值,要求值必須相同,否則會(huì)報(bào)錯(cuò)。
代碼
注解
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屬性寫成下邊這兩種結(jié)果是一樣的
// @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") //這個(gè)與上邊結(jié)果相同
// @SubMyAnnotation("subLocation") //缺省屬性名會(huì)報(bào)錯(cuò)
@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();
}
}測(cè)試
前端訪問(wèn):http://localhost:8080/hello/test
結(jié)果
subValue:subLocation;subLoation:subLocation
value:;location:
value:subLocation;location:subLocation
以上就是SpringBoot中注解@AliasFor的使用詳解的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot注解@AliasFor的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java中從JSON轉(zhuǎn)Java實(shí)體的多種方法詳解
在現(xiàn)在的日常開發(fā)中不管前端還是后端,JSON 格式的數(shù)據(jù)是用得比較多的,甚至可以說(shuō)無(wú)處不在,這篇文章主要給大家介紹了關(guān)于Java中從JSON轉(zhuǎn)Java實(shí)體的多種方法,需要的朋友可以參考下2023-12-12
SpringBoot+MyBatis-Plus實(shí)現(xiàn)分頁(yè)功能
在SpringBoot項(xiàng)目中,結(jié)合MyBatis-Plus(簡(jiǎn)稱MP)可以非常方便地實(shí)現(xiàn)分頁(yè)功能,MP為開發(fā)者提供了分頁(yè)插件PaginationInterceptor,只需簡(jiǎn)單配置即可使用,本文給大家介紹了SpringBoot+MyBatis-Plus實(shí)現(xiàn)分頁(yè)功能,文中通過(guò)代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
將Swagger2文檔導(dǎo)出為HTML或markdown等格式離線閱讀解析
這篇文章主要介紹了將Swagger2文檔導(dǎo)出為HTML或markdown等格式離線閱讀,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
Maven入門之使用Nexus搭建Maven私服及上傳下載jar包
這篇文章主要介紹了Maven入門之使用Nexus搭建Maven私服及上傳下載jar包,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
淺析SpringBoot自動(dòng)化配置原理實(shí)現(xiàn)
這篇文章主要介紹了淺析SpringBoot自動(dòng)化配置原理實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
java導(dǎo)出數(shù)據(jù)庫(kù)的全部表到excel
這篇文章主要為大家詳細(xì)介紹了java導(dǎo)出數(shù)據(jù)庫(kù)的全部表到excel的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03

