久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

SpringBoot中注解@AliasFor的使用詳解

這篇文章主要為大家詳細介紹了SpringBoot中注解@AliasFor的用法,文中的示例代碼講解詳細,對我們學習或工作有一定幫助,需要的可以參考一下

簡介

本文用示例介紹@AliasFor(別名)注解的用法。

用法1:注解的屬性互為別名

簡介

它可以注解到自定義注解的兩個屬性上,表示這兩個互為別名,也就是說這兩個屬性其實同一個含義。

  • 其中一個屬性名必須是"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

用法2.繼承父注解的屬性,不重寫屬性名

簡介

子注解的屬性值的讀寫,其實是對父注解的屬性值的讀寫。(對繼承的屬性來說)

此時,只能讀寫繼承了的屬性值。

代碼

注解

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)

用法3:繼承父注解的屬性,并重寫屬性名

簡介

子注解的屬性值的讀寫,其實是對父注解的屬性值的讀寫。(對重寫的屬性來說)

無論指明設置哪個屬性名設置屬性值,另一個屬性名也是同樣屬性值,不可以缺省屬性名。

若兩個都指明屬性值,要求值必須相同,否則會報錯。

代碼

注解

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的使用詳解的詳細內容,更多關于SpringBoot注解@AliasFor的資料請關注html5模板網其它相關文章!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

人工智能時代的到來,相信大家已耳濡目染,虹軟免費,離線開放的人臉識別SDK,正推動著全行業進入刷臉時代,下面這篇文章主要給大家介紹了關于如何基于SpringBoot實現人臉識別功能的相關
這篇文章主要介紹了SpringBoot定時任務功能詳細解析,這次的功能開發過程中也算是對其內涵的進一步了解,以后遇到定時任務的處理也更清晰,更有效率了,對SpringBoot定時任務相關知識
這篇文章主要介紹了Spring?MVC基于注解的使用JSON數據處理,json是一種輕量級的數據交換格式,是一種理想的數據交互語言,它易于閱讀和編寫,同時也易于機器解析和生成,本文通過實例
jasypt是一個通用的加解密庫,我們可以使用它在配置文件中對數據庫密碼進行加密,以確保其安全性,接下來通過本文給大家介紹SpringBoot項目使用jasypt加解密的方法,感興趣的朋友一起
這篇文章主要介紹了Spring?Cloud?Eureka基礎應用,Eureka?Client中內置一個負載均衡器,用來進行基本的負載均衡,下面我們將通過搭建一個簡單的Eureka例子來了解Eureka的運作原理,感興趣的
這篇文章主要介紹了SpringBoot通過AOP與注解實現入參校驗詳情,文章從相關問題展開全文內容詳情,具有一定的參考價值,需要的小伙伴可以參考一下
主站蜘蛛池模板: 亚洲精品中文字幕 | 91国内精品久久 | 在线中文字幕亚洲 | 少妇精品亚洲一区二区成人 | 国产黄a一级| 成人深夜小视频 | 日韩在线国产精品 | www亚洲精品 | 亚洲免费大片 | 一区二区高清 | av乱码| 中文字幕在线观看 | 国产一区免费 | 国产免费看| 成人精品视频在线观看 | 欧美日韩中文在线 | 日韩在线综合 | 亚洲精品18 | 精品二 | 日韩欧美一区二区三区免费观看 | 国产在线精品一区二区三区 | 国产日韩精品一区 | 性做久久久久久免费观看欧美 | 四虎永久影院 | 国产情侣在线看 | 91麻豆产精品久久久久久 | 欧美日韩国产一区二区三区 | 天天在线操 | 国产精品成人久久久久 | 人人做人人澡人人爽欧美 | 91精品国产乱码久久蜜臀 | 亚洲色图婷婷 | 免费成人毛片 | 欧美在线| 国产成人一区 | 国产精品久久久久久久久久久久久久 | 不卡视频一区二区三区 | 久久久精 | 91精品国产91久久综合桃花 | 免费观看日韩av | 在线播放一区二区三区 |