spring-@AliasFor注解
目录
1、注解内部的显性别名
2、用于元注解属性的显性别名
3、注解中的隐性别名
4、别名传递
@AliasFor是spring中的一个注解,可用来对注解属性声明别名。若想起到别名效果,需通过spring进行处理后才能有效果。
互为别名的属性只设置其中一个值,互为别名的其他属性同时也会设置相同值
@TestAliasFor(value="value")
class TestDemo{
}
如果同时设置别名属性值,则值必须一致,否则报错
@TestAliasFor(value="value",name="value")
class TestDemo{
}
1、注解内部的显性别名
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAliasFor {@AliasFor("name")String value() default "";@AliasFor("value")String name() default "";
}
@Testpublic void test6(){TestAliasFor annotation = AnnotationUtils.findAnnotation(TestDemo.class, TestAliasFor.class);System.out.println(annotation.value());System.out.println(annotation.name());}
注意:
1.组成别名对的每个属性都必须用@AliasFor进行注释,并且AliasFor中的值必须指向别名对中的另一个属性
2.别名化的属性必须声明相同的返回类型
3.别名化的属性必须声明默认值
4.别名化的属性默认值必须相同
注解内部显示别名只能一对互相别名,若是有3个内部注解互为显性别名,则报错,如下
2、用于元注解属性的显性别名
如果被@AliasFor注解的属性指向的是它所在注解之外的其他注解,那么这个属性被解释成元注解属性的别名。(称之为显性的元注解属性重写)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface TestAliasFor1 {String value() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@TestAliasFor1
public @interface TestAliasFor {@AliasFor(annotation = TestAliasFor1.class, attribute = "value")String value() default "";
}
@Testpublic void test6(){TestAliasFor annotation = AnnotationUtils.findAnnotation(TestDemo.class, TestAliasFor.class);TestAliasFor1 mergedAnnotation = AnnotatedElementUtils.getMergedAnnotation(TestDemo.class, TestAliasFor1.class);System.out.println(annotation.value());System.out.println(mergedAnnotation.value() );}
注意:
1 如果一个属性是一个元注解属性的别名,那么这个属性必须用@AliasFor进行注解并且该属性必须指向元注解属性(若是没指定元注解属性名称,则默认与属性名称一致)
2 别名化的属性必须声明相同的返回结果
3.@AliasFor的annotation属性必须引用元注解
4.被引用的元注解必须放置在声明了@AliasFor的注解类上
3、注解中的隐性别名
如果注解中的一个或多个属性声明为同一个元注解属性的属性重写(直接地或传递地重写)那么这些注解会被当作彼此的隐性别名集来对待,结果是它们的行为类似于注解中的显性别名
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@TestAliasFor1
public @interface TestAliasFor {@AliasFor(annotation = TestAliasFor1.class, attribute = "value")String value() default "";@AliasFor(annotation = TestAliasFor1.class, attribute = "value")String name() default "";
}
@Testpublic void test7(){TestAliasFor testAliasFor = AnnotatedElementUtils.getMergedAnnotation(TestDemo.class, TestAliasFor.class);System.out.println(testAliasFor.name());System.out.println(testAliasFor.value());TestAliasFor1 testAliasFor1 = AnnotatedElementUtils.getMergedAnnotation(TestDemo.class, TestAliasFor1.class);System.out.println(testAliasFor1.value());}
TestAliasFor中的value和name属性都复写了TestAliasFor1的value属性,所以TestAliasFor的value和name是彼此的隐性别名,如下TestAliasFor2的name2和value2也是彼此的隐性别名
@Retention(RetentionPolicy.RUNTIME)
@TestAliasFor
public @interface TestAliasFor2 {@AliasFor(annotation = TestAliasFor.class, attribute = "name")String name2() default "";@AliasFor(annotation = TestAliasFor.class, value = "value")String value2() default "";
}
注意:
1.属于隐性别名组中的每一个属性必须使用@AliasFor进行注解,并且attribute必须引用相同元注解中的同一个属性(或者相同元注解对应的别名属性)
2.别名化的属性必须声明相同的返回类型
3.别名化的属性必须定义默认值
4.别名化的属性必须声明相同的默认值
5.注解必须引用合适的元注解
6.被引用的元注解必须放置在声明了@AliasFor的注解上
4、别名传递
@AliasFor注解是允许别名之间的传递的,简单理解,如果A是B的别名,并且B是C的别名,那么A是C的别名
参考:
@AliasFor注解_不想做咸鱼的王富贵的博客-CSDN博客_@aliasfor
Spring中的@AliasFor标签_wolfcode_cn的博客-CSDN博客_@aliasfor
spring-@AliasFor注解相关推荐
- Spring中@AliasFor注解的作用及原理
本文基于Springboot 2.1.6.RELEASE 版本分析. 关于@AliasFor注解,曾提过的一个issue Explicit attribute overrides configured ...
- 【Spring】Spring 深入理解注解及spring对注解的增强
1.概述 深入理解注解及spring对注解的增强 2. 什么是注解? 代码中注释大家都熟悉吧,注释是给开发者看的,可以提升代码的可读性和可维护性,但是对于java编译器和虚拟机来说是没有意义的,编译之 ...
- 从源码深入理解Spring IoC 注解
全注解下的 Spring IoC 本文基于 Spring Boot,所以并不使用 XML 配置,使用注解描述生成对象 版权声明:本文为博主原创文章,未经博主允许不得转载. Ioc 容器简介 Sprin ...
- @enableautoconfiguration注解作用_如何让代码变“高级”-Spring组合注解提升代码维度(这么有趣)...
[如何让代码变"高级"]-Spring组合注解提升代码维度 "致"高级"工程师(BUG工程师) 一颗折腾的心:heartpulse: 原创不易,点个赞 ...
- Spring Boot 注解原理
Spring Boot 注解原理 首先,先看SpringBoot的主配置类: @SpringBootApplication public class StartEurekaApplication {p ...
- java中@AliasFor注解
Java中的@AliasFor注解,是Spring框架的一个元注解,用于指定注解的别名关系.它可以用于定义同一个注解的多个属性之间的别名关系,让它们在使用时可以互相替代. 使用@AliasFor注解的 ...
- @AliasFor注解
@AliasFor注解 @AliasFor是一个注解,用于为注解属性声明别名. 代码如下:它有两个属性value和attribute @AliasFor注解注释了 自身,并且value和attribu ...
- java aliasfor_@AliasFor注解
@AliasFor注解 @AliasFor是一个注解,用于为注解属性声明别名. 代码如下:它有两个属性value和attribute @AliasFor注解注释了 自身,并且value和attribu ...
- Spring缓存注解【@Cacheable、@CachePut、@CacheEvict、@Caching、@CacheConfig】使用及注意事项
文章目录 一.概述 二.缓存注解种类 三.优劣势说明 四.如何使用? 五.详细介绍介绍 1)@Cacheable(常用) 1.value/cacheNames 属性 2.key属性 3.keyGene ...
- Spring——事务注解@Transactional【建议收藏】
在某些业务场景下,如果一个请求中,需要同时写入多张表的数据或者执行多条sql,为了保证操作的原子性(要么同时成功,要么同时失败),避免数据不一致的情况,我们一般都会用到事务:Spring框架下,我们经 ...
最新文章
- 类加载子系统的详解——未完待续
- 关于单点登录中同一浏览器session信息共享的又一解决办法
- asterisk 常用命令
- NeHe OpenGL教程 第四十五课:顶点缓存
- 《快速开发》通过Maven创建WebService项目Hello World!
- 微生物组数据系统发育分析的方法
- 台式计算机有hdmi口,我的台式电脑没有hdmi接口怎么解决
- 使用kindeditor直接粘贴本地图片或者是qq截图
- Android 热点开启流程
- vue拦截器刷新登陆页面_Vue + Spring Boot 项目实战(六):前端路由与登录拦截器-Go语言中文社区...
- (JAVA编程练习):输入某年某月某日,判断这一天是这一年的第几天?
- php实现团购功能,PHP 对接美团大众点评团购券(门票)的开发步骤
- matlab粒子群算法求解无约束最小值,pso matlab粒子群算法和遗传 是解决约束优化问题,无 和多目标 的优 259万源代码下载- www.pudn.com...
- buuctf_crypto
- 改变程序黑窗口的背景和字体颜色
- jQuery对于FireBox中的keypress与event
- xcode解决xxx has conflicting provisioning settings.
- JVM系列:jvm基本结构
- vue3+ts百度编辑器
- java 重复对象_Java根据条件查找重复项并覆盖/更新重复对象