SpringBoot中注解@AliasFor的使用详解

目录
  • 简介
  • 用法1:注解的属性互为别名
    • 简介
    • 实例
  • 用法2.继承父注解的属性,不重写属性名
    • 简介
    • 代码
  • 用法3:继承父注解的属性,并重写属性名
    • 简介
    • 代码

简介

本文用示例介绍@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的资料请关注我们其它相关文章!

(0)

相关推荐

  • Springboot @RequestBody注解踩坑记录

    目录 @RequestBody注解踩坑 @RequestBodyjson注入Bean属性为空 @RequestBody注解的一些注意事项 @RequestBody注解踩坑 @RequestBody json注入Bean属性为空 1.接收JSON数据注入到Bean中,必须要加RuquestBody注解 2.判断属性名称字段是否相同 3.spring 默认不为首字母大写的JavaBean进行赋值操作,若首字母大写进行赋值操作需加入@JsonProperty(value=“AlarmTime”)注解

  • SpringBoot @Retryable注解方式

    背景 在调用第三方接口或者使用MQ时,会出现网络抖动,连接超时等网络异常,所以需要重试.为了使处理更加健壮并且不太容易出现故障,后续的尝试操作,有时候会帮助失败的操作最后执行成功.一般情况下,需要我们自行实现重试机制,一般是在业务代码中加入一层循环,如果失败后,再尝试重试,但是这样实现并不优雅.在SpringBoot中,已经实现了相关的能力,通过@Retryable注解可以实现我们想要的结果. @Retryable 首先来看一下Spring官方文档的解释: @Retryable注解可以注解于方法

  • SpringBoot @Autowired注解注入规则介绍

    目录 @Autowired注解注入规则 验证 小结一下 @Autowired注解无法自动注入的错误 @Autowired注解注入规则 @Autowired - 注入默认根据类型,匹配不到则根据bean名字 Spring中注解方式的默认beanName生成规则: 在Spring中,当我们配置一个bean的时候,可以不指定name,这样的话,Spring会生成一个默认的beanName 1. 驼峰形式类名首字母小写:UserService--userService 2. 特殊情况--当类名的首字母和

  • springboot中@component注解的使用实例

    目录 @component注解的使用 方式一 方式二 @component注解有什么作用 用一句话概括 @component注解的使用 配置响应头的内容. 方式一 直接在拦截器里配置响应头内容. /**  * 拦截器--用户身份确认.  */ public class RestInterceptor implements HandlerInterceptor {         private static final Logger log = LoggerFactory.getLogger(R

  • SpringBoot配置@Configuration注解和@bean注解

    目录 1.@Configuration注解 2.@bean注解 3.单实例 4.配置类也是容器的组件 5.直接调用配置类里面的person1()方法 6.proxyBeanMethods——代理bean的方法 1.@Configuration注解 用法:作用在类上面 作用:告诉SpringBoot这是一个配置类,相当于Spring中的xml配置文件. @Configuration //告诉SpringBoot这是一个配置类 == 配置文件 public class Config { } 2.@b

  • SpringBoot中的@Value注解用法

    目录 一.前言 二.数组怎么样 三.替代方法 3.1 解析 List 3.2 解析 Set 3.3 解析 Map 四.后续 一.前言 在日常开发中,经常会遇到需要在配置文件中,存储 List 或是 Map 这种类型的数据. Spring 原生是支持这种数据类型的,以配置 List 类型为例,对于 .yml 文件配置如下: test: list: - aaa - bbb - ccc 对于 .properties 文件配置如下所示: test.list[0]=aaa test.list[1]=bbb

  • SpringBoot中注解@AliasFor的使用详解

    目录 简介 用法1:注解的属性互为别名 简介 实例 用法2.继承父注解的属性,不重写属性名 简介 代码 用法3:继承父注解的属性,并重写属性名 简介 代码 简介 本文用示例介绍@AliasFor(别名)注解的用法. 用法1:注解的属性互为别名 简介 它可以注解到自定义注解的两个属性上,表示这两个互为别名,也就是说这两个属性其实同一个含义. 其中一个属性名必须是"value" 无论指明设置哪个属性名设置属性值,另一个属性名也是同样属性值,也可以缺省属性名. 若两个都指明属性值,要求值必须

  • Mybatis中注解@MapKey的使用详解

    mybatis的原身是ibatis,现在已经脱离了apache基金会,新官网是http://www.mybatis.org/. 在研究Mybatis源码之前并不知道这个注解的妙用的,但是当我看到参数解析的时候 有这个一个注解,所以我了解了一下,当我们返回像Map<String, Map<String, Object>>这种类型的时候,我们往往很难做到,因为这里面可能是多个表的数据,所以我们不可能再建一个模型. 这时候我们就可以使用这个注解了 @Retention(Retention

  • SpringBoot中使用 RabbitMQ的教程详解

    本章主要建立在已经安装好Erlang以及RabbitMQ的基础上,接下来,简单介绍一下使用 一.Direct直接模式 通过routingKey和exchange决定的那个唯一的queue可以接收消息 1.首先到RabbitMQ的管理界面新建一个队列(Direct模式) 2.测试项目的基础结构如下: 这里为了方便测试,直接在父项目中建立两个子模块(生产者和消费者) 3.pom.xml文件的依赖如下: 父项目: <?xml version="1.0" encoding="U

  • SpringBoot中获取profile的方法详解

    目录 spring boot与profile 静态获取方式 autowire ProfileConfig spring boot与profile spring boot 的项目中不再使用xml的方式进行配置,并且,它还遵循着约定大于配置. 静态获取方式 静态工具类获取当前项目的profile环境. import org.springframework.beans.BeansException; import org.springframework.context.ApplicationConte

  • springboot中redis正确的使用详解

    redis实现了对数据的缓存,在项目里一些字典数据,会话数据,临时性数据都会向redis来存储,而在springboot里对redis也有支持,一般来说多个线程共同使用一个redis实现是有线程安全的风险的,而每个实现一个线程又太浪费资源,无法控制线程数量是非常危险的,所以就出现了一些redis线程池组件,下面说一下两个主要的组件. jedis 线程池主要是每个实例有自己的线程,线程可以从它建立的池子里获取lettuce lettuce是 apache推出的线程池工具,它的redis实例是可以被

  • Java中注解与原理分析详解

    目录 一.注解基础 二.注解原理 三.常用注解 1.JDK注解 2.Lombok注解 四.自定义注解 1.同步控制 2.类型引擎 一.注解基础 注解即标注与解析,在Java的代码工程中,注解的使用几乎是无处不在,甚至多到被忽视: 无论是在JDK源码或者框架组件,都在使用注解能力完成各种识别和解析动作:在对系统功能封装时,也会依赖注解能力简化各种逻辑的重复实现: 基础接口 在Annotation的源码注释中有说明:所有的注解类型都需要继承该公共接口,本质上看注解是接口,但是代码并没有显式声明继承关

  • SpringBoot日志注解与缓存优化详解

    目录 日志注解: 缓存的优化: 总结 日志注解: 关于SpringBoot中的日志处理,在之前的文章中页写过: 点击进入 这次通过注解+Aop的方式来实现日志的输出: 首先需要定义一个注解类:  @Target(ElementType.METHOD)  @Retention(RetentionPolicy.RUNTIME)  @Documented  public @interface LogAnnotation {      String module() default "";  

  • SpringBoot中使用监听器的方法详解

    目录 1.监听器 2.SpringBoot中监听器的使用 2.1监听Servlet上下文对象 2.2监听HTTP会话Session对象 总结 1.监听器 web监听器是一张Servlet中特殊的类,它们能帮助开发者监听web中特定的事件,比如ServletContext,HttpSession,ServletRequest的创建和销毁:变量的创建.销毁.和修改等.可以在某些动作前后增加处理,实现监控 2.SpringBoot中监听器的使用 web监听器的使用场景很多,比如监听servlet上下文

  • SpringBoot Application注解原理及代码详解

    1.SpringBoot 启动main() @SpringBootApplication public class TomcatdebugApplication { public static void main(String[] args) { SpringApplication.run(TomcatdebugApplication.class, args); } } 1.1 @SpringBootApplication 注解,其实主要是@ComponentScan,@EnableAutoCo

  • SpringBoot中自定义参数绑定步骤详解

    正常情况下,前端传递来的参数都能直接被SpringMVC接收,但是也会遇到一些特殊情况,比如Date对象,当我的前端传来的一个日期时,就需要服务端自定义参数绑定,将前端的日期进行转换.自定义参数绑定也很简单,分两个步骤: 1.自定义参数转换器 自定义参数转换器实现Converter接口,如下: public class DateConverter implements Converter<String,Date> { private SimpleDateFormat simpleDateFor

随机推荐