最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解@AliasFor注解的使用與注意事項

 更新時間:2023年08月03日 16:55:22   作者:Dream_sky_java  
@AliasFor注解是在spring源碼當中提供的,見名知義,他是為了別名而自定義的注解,下面我們來看看它的使用與注意事項,感興趣的小伙伴可以了解一下

一、@AliasFor注解簡介

@AliasFor注解是在spring源碼當中提供的,見名知義,他是為了別名而自定義的注解!

但這并不是java原生支持的,需要通過Spring中提供的工具類org.springframework.core.annotation.AnnotationUtils或者org.springframework.core.annotation.AnnotatedElementUtils來解析。AnnotatedElementUtils內(nèi)部還是調(diào)用的AnnotationUtils

二、使用場景

  • 在注解中一對屬性上通過聲明@AliasFor,進行屬性互換。
  • 在注解上聲明了另一個注解,對另一個注解的屬性進行別名覆蓋(也可以理解為將一個注解上的屬性值傳遞給另一個注解

三、 注意事項(重點關(guān)注)

指定別名,屬性互換的情況下,通過AnnotationUtils仍然可以獲取到值,而通過java原生的方式則無法獲取。 是因為Spring其實是自己實現(xiàn)了jdk動態(tài)的攔截器來實現(xiàn)別名功能. 但是: 如果同時設(shè)置,并且互為別名的兩個屬性值不一樣就會報錯,拋出如下異常:

Caused by: org.springframework.core.annotation.AnnotationConfigurationException: Different @AliasFor mirror values for annotation [com.sysmenu.annotion.MenuAuthCheck] declared on com.controller.ZnjProjectNoticeController.publishNoticeAnnouncement(com.dto.ZnjProjectNoticeAnnouncementInsertDTO); attribute 'permission' and its alias 'value' are declared with values of [lecture] and [lecture_report].
	at org.springframework.core.annotation.AnnotationTypeMapping$MirrorSets$MirrorSet.resolve(AnnotationTypeMapping.java:711)
	at org.springframework.core.annotation.AnnotationTypeMapping$MirrorSets.resolve(AnnotationTypeMapping.java:666)
	at org.springframework.core.annotation.TypeMappedAnnotation.<init>(TypeMappedAnnotation.java:134)
	at org.springframework.core.annotation.TypeMappedAnnotation.<init>(TypeMappedAnnotation.java:118)
	at org.springframework.core.annotation.TypeMappedAnnotation.of(TypeMappedAnnotation.java:599)
	at org.springframework.core.annotation.MergedAnnotation.of(MergedAnnotation.java:610)
	at org.springframework.core.type.classreading.MergedAnnotationReadingVisitor.visitEnd(MergedAnnotationReadingVisitor.java:96)

所以互為別名,指定一個即可,兩個都會有相同的值

四、使用步驟

1.指定別名,屬性互換

1.1自定義注解

@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface MenuAuthCheck {
    @AliasFor("value")
    String permission() default "";
    @AliasFor("permission")
    String value() default "";
}

1.2使用

@MenuAuthCheck("test_admin")

1.3注意事項

  • 構(gòu)成別名對的每個屬性都必須用@AliasFor注釋,并且屬性或值必須引用別名對中的另一個屬性。
  • 這兩個屬性的必須擁有相同的返回值類型。
  • 別名屬性必須聲明默認值。
  • 這兩個屬性必須擁有相同的默認值。

1.4 Spring中的@RequestMapping注解

我們通常直接使用

@RequestMapping("/web"")

value注解注釋也表明 這是 path的別名

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
    String name() default "";
 		/**
	 * The primary mapping expressed by this annotation.
	 * <p>This is an alias for {@link #path}. For example,
	 * {@code @RequestMapping("/foo")} is equivalent to
	 * {@code @RequestMapping(path="/foo")}.
	 * <p><b>Supported at the type level as well as at the method level!</b>
	 * When used at the type level, all method-level mappings inherit
	 * this primary mapping, narrowing it for a specific handler method.
	 * <p><strong>NOTE</strong>: A handler method that is not mapped to any path
	 * explicitly is effectively mapped to an empty path.
	 */
    @AliasFor("path")
    String[] value() default {};
    @AliasFor("value")
    String[] path() default {};
    RequestMethod[] method() default {};
    String[] params() default {};
    String[] headers() default {};
    String[] consumes() default {};
    String[] produces() default {};
}

2.將一個注解上的屬性值傳遞給另一個注解對另一個注解的屬性進行別名覆蓋

2.1舉例說明

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TestAnnotation {
    @AliasFor("name")
    String value() default "";
    @AliasFor("value")
    String name() default "";
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@MyAnnotation
public @interface TestAliasAnnotation {
    @AliasFor(annotation = TestAnnotation .class, attribute = "value")
    String name() default "";
}

將TestAliasAnnotation 注解中的name屬性傳遞給TestAnnotation 注解的value屬性,進行覆蓋

2.2注意事項

被標記@AliasFor的屬性和atttibute所指向的元注解屬性必須有相同的返回值類型。

3.組合多個注解,通過一個注解達到使用多個注解的效果

3.1 這是3個注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation1 {
    String value();
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation2 {
    String name();
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAnnotation3 {
    String desc();
}

使用@AliasFor注解來定義一個新注解,將這三個注解組合起來如下:

3.2合并注解

/*
* 合并注解
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@TestAnnotation1 
@TestAnnotation2 
@TestAnnotation3 
public @interface TestMergeAnnotation {
    @AliasFor(annotation = TestAnnotation1 .class, attribute = "value")
    String value() default "";
    @AliasFor(annotation = TestAnnotation2 .class, attribute = "name")
    String name() default "";
    @AliasFor(annotation = TestAnnotation3 .class, attribute = "desc")
    String desc() default "";
}

在這個新注解中使用了@AliasFor注解,使一個注解達到同時使用3個注解的效果

3.3使用

@MyCombinedAnnotation(value = "perfect", name = "who", desc = "this is a merge annotation")

等價于三個注解同時使用

@TestAnnotation1 ("hello")
@TestAnnotation2 (name = "world")
@TestAnnotation3 (desc = "this is a merge annotation")
public class TestClass {
}

五、總結(jié)

  • 我們在使用時這個注解時,可以直接填寫內(nèi)容,而不用主動指定"name"和"value"屬性,從而達到了隱示表明屬性別名的效果。
  • 指定別名,屬性互換的情況下,通過AnnotationUtils仍然可以獲取到值,而通過java原生的方式則無法獲取。 是因為Spring其實是自己實現(xiàn)了jdk動態(tài)的攔截器來實現(xiàn)別名功能. 但是: 如果同時設(shè)置,并且互為別名的兩個屬性值不一樣就會報錯 所以互為別名,指定一個即可,兩個都會有相同的值

作用

  • 指定別名,屬性互換
  • 將一個注解上的屬性值傳遞給另一個注解對另一個注解的屬性進行別名覆蓋
  • 組合多個注解,通過一個注解達到使用多個注解的效果

到此這篇關(guān)于詳解@AliasFor注解的使用與注意事項的文章就介紹到這了,更多相關(guān)@AliasFor注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java的集合LinkedHashSet詳解

    Java的集合LinkedHashSet詳解

    這篇文章主要介紹了Java的集合LinkedHashSet詳解,LinkedHashSet介于HashSet和TreeSet之間,它也是一個hash表,但是同時維護了一個雙鏈表來記錄插入的順序,需要的朋友可以參考下
    2023-09-09
  • Java正則表達式API Matcher類方法

    Java正則表達式API Matcher類方法

    這篇文章主要介紹了Java正則表達式API Matcher類方法,對Matcher類的一些有用方法進行功能對它們進行分組展開介紹,需要的朋友可以參考一下
    2022-06-06
  • IDEA中 Getter、Setter 注解不起作用的問題如何解決

    IDEA中 Getter、Setter 注解不起作用的問題如何解決

    這篇文章主要介紹了IDEA中 Getter、Setter 注解不起作用的問題如何解決,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • Java Web學(xué)習(xí)教程之Hibernate And MyBatis的理解

    Java Web學(xué)習(xí)教程之Hibernate And MyBatis的理解

    這篇文章主要給大家介紹了關(guān)于Java Web學(xué)習(xí)教程之Hibernate And MyBatis的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-04-04
  • Spring使用注解方式實現(xiàn)創(chuàng)建對象

    Spring使用注解方式實現(xiàn)創(chuàng)建對象

    這篇文章主要介紹了Spring使用注解方式實現(xiàn)創(chuàng)建對象,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2023-02-02
  • Spring Security基于json登錄實現(xiàn)過程詳解

    Spring Security基于json登錄實現(xiàn)過程詳解

    這篇文章主要介紹了Spring Security基于json登錄實現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-08-08
  • 詳解SpringBoot結(jié)合策略模式實戰(zhàn)套路

    詳解SpringBoot結(jié)合策略模式實戰(zhàn)套路

    這篇文章主要介紹了詳解SpringBoot結(jié)合策略模式實戰(zhàn)套路,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • 使用Springboot自定義轉(zhuǎn)換器實現(xiàn)參數(shù)去空格功能

    使用Springboot自定義轉(zhuǎn)換器實現(xiàn)參數(shù)去空格功能

    這篇文章主要介紹了使用Springboot自定義轉(zhuǎn)換器實現(xiàn)參數(shù)去空格功能,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java?將Excel轉(zhuǎn)為UOS的操作方法

    Java?將Excel轉(zhuǎn)為UOS的操作方法

    以.uos為后綴的文件,表示Uniform?Office?Spreadsheet文件,是一種國產(chǎn)的辦公文件格式,該格式以統(tǒng)一辦公格式(UOF)創(chuàng)建,使用XML和壓縮保存電子表格,這篇文章主要介紹了Java?將Excel轉(zhuǎn)為UOS,需要的朋友可以參考下
    2022-09-09
  • mybatis?報錯顯示sql中有兩個limit的解決

    mybatis?報錯顯示sql中有兩個limit的解決

    這篇文章主要介紹了mybatis?報錯顯示sql中有兩個limit的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10

最新評論

石景山区| 略阳县| 琼结县| 桃园县| 青冈县| 淮南市| 九江县| 青冈县| 那坡县| 盘锦市| 敦化市| 博罗县| 栾川县| 宜兰市| 威宁| 德惠市| 遵化市| 平邑县| 鄂托克前旗| 家居| 茂名市| 前郭尔| 武宣县| 崇仁县| 华阴市| 裕民县| 客服| 长海县| 上栗县| 河北区| 大渡口区| 屏南县| 盱眙县| 朝阳县| 麟游县| 卓尼县| 达孜县| 三亚市| 景洪市| 壶关县| 宁武县|