SpringBoot中注解@AliasFor的使用详解

 更新时间:2022年05月17日 14:13:02   作者:IT利刃出鞘  
这篇文章主要为大家详细介绍了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的资料请关注脚本之家其它相关文章!

相关文章

  • Spring Security角色继承分析

    Spring Security角色继承分析

    这篇文章主要介绍了Spring Security角色继承分析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • Java——对象初始化顺序使用详解

    Java——对象初始化顺序使用详解

    本篇文章介绍了,Java对象初始化顺序的使用。需要的朋友参考下
    2017-04-04
  • Java中从JSON转Java实体的多种方法详解

    Java中从JSON转Java实体的多种方法详解

    在现在的日常开发中不管前端还是后端,JSON 格式的数据是用得比较多的,甚至可以说无处不在,这篇文章主要给大家介绍了关于Java中从JSON转Java实体的多种方法,需要的朋友可以参考下
    2023-12-12
  • SpringBoot+MyBatis-Plus实现分页功能

    SpringBoot+MyBatis-Plus实现分页功能

    在SpringBoot项目中,结合MyBatis-Plus(简称MP)可以非常方便地实现分页功能,MP为开发者提供了分页插件PaginationInterceptor,只需简单配置即可使用,本文给大家介绍了SpringBoot+MyBatis-Plus实现分页功能,文中通过代码示例给大家介绍的非常详细,需要的朋友可以参考下
    2024-01-01
  • 将Swagger2文档导出为HTML或markdown等格式离线阅读解析

    将Swagger2文档导出为HTML或markdown等格式离线阅读解析

    这篇文章主要介绍了将Swagger2文档导出为HTML或markdown等格式离线阅读,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-11-11
  • Maven入门之使用Nexus搭建Maven私服及上传下载jar包

    Maven入门之使用Nexus搭建Maven私服及上传下载jar包

    这篇文章主要介绍了Maven入门之使用Nexus搭建Maven私服及上传下载jar包,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • slf4j与log4j全面了解

    slf4j与log4j全面了解

    下面小编就为大家带来一篇slf4j与log4j全面了解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-07-07
  • 详解java基于MyBatis使用示例

    详解java基于MyBatis使用示例

    这篇文章主要介绍了详解java基于MyBatis使用示例,对学习MyBatis有一定的帮助,有需要的可以了解一下。
    2016-11-11
  • 浅析SpringBoot自动化配置原理实现

    浅析SpringBoot自动化配置原理实现

    这篇文章主要介绍了浅析SpringBoot自动化配置原理实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • java导出数据库的全部表到excel

    java导出数据库的全部表到excel

    这篇文章主要为大家详细介绍了java导出数据库的全部表到excel的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-03-03

最新评论