详解@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内部还是调用的AnnotationUtils

二、使用场景

  • 在注解中一对属性上通过声明@AliasFor,进行属性互换。
  • 在注解上声明了另一个注解,对另一个注解的属性进行别名覆盖(也可以理解为将一个注解上的属性值传递给另一个注解

三、 注意事项(重点关注)

指定别名,属性互换的情况下,通过AnnotationUtils仍然可以获取到值,而通过java原生的方式则无法获取。 是因为Spring其实是自己实现了jdk动态的拦截器来实现别名功能. 但是: 如果同时设置,并且互为别名的两个属性值不一样就会报错,抛出如下异常:

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注意事项

  • 构成别名对的每个属性都必须用@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 {
}

五、总结

  • 我们在使用时这个注解时,可以直接填写内容,而不用主动指定"name"和"value"属性,从而达到了隐示表明属性别名的效果。
  • 指定别名,属性互换的情况下,通过AnnotationUtils仍然可以获取到值,而通过java原生的方式则无法获取。 是因为Spring其实是自己实现了jdk动态的拦截器来实现别名功能. 但是: 如果同时设置,并且互为别名的两个属性值不一样就会报错 所以互为别名,指定一个即可,两个都会有相同的值

作用

  • 指定别名,属性互换
  • 将一个注解上的属性值传递给另一个注解对另一个注解的属性进行别名覆盖
  • 组合多个注解,通过一个注解达到使用多个注解的效果

到此这篇关于详解@AliasFor注解的使用与注意事项的文章就介绍到这了,更多相关@AliasFor注解内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 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 注解不起作用的问题如何解决,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • Java Web学习教程之Hibernate And MyBatis的理解

    Java Web学习教程之Hibernate And MyBatis的理解

    这篇文章主要给大家介绍了关于Java Web学习教程之Hibernate And MyBatis的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们来一起学习学习吧。
    2018-04-04
  • Spring使用注解方式实现创建对象

    Spring使用注解方式实现创建对象

    这篇文章主要介绍了Spring使用注解方式实现创建对象,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2023-02-02
  • Spring Security基于json登录实现过程详解

    Spring Security基于json登录实现过程详解

    这篇文章主要介绍了Spring Security基于json登录实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-08-08
  • 详解SpringBoot结合策略模式实战套路

    详解SpringBoot结合策略模式实战套路

    这篇文章主要介绍了详解SpringBoot结合策略模式实战套路,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-10-10
  • 使用Springboot自定义转换器实现参数去空格功能

    使用Springboot自定义转换器实现参数去空格功能

    这篇文章主要介绍了使用Springboot自定义转换器实现参数去空格功能,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • Java 将Excel转为UOS的操作方法

    Java 将Excel转为UOS的操作方法

    以.uos为后缀的文件,表示Uniform Office Spreadsheet文件,是一种国产的办公文件格式,该格式以统一办公格式(UOF)创建,使用XML和压缩保存电子表格,这篇文章主要介绍了Java 将Excel转为UOS,需要的朋友可以参考下
    2022-09-09
  • mybatis 报错显示sql中有两个limit的解决

    mybatis 报错显示sql中有两个limit的解决

    这篇文章主要介绍了mybatis 报错显示sql中有两个limit的解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-10-10

最新评论