用法:

import org.springframework.core.annotation.AliasFor;

import java.lang.annotation.*;

@Target(ElementType.TYPE)//目标是方法
@Retention(RetentionPolicy.RUNTIME) //注解会在class中存在,运行时可通过反射获取
public @interface Request { @AliasFor("service")
String value() default ""; @AliasFor("value")
String service() default ""; String lang() default "zh-CN"; }
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@RequestMapping(method = RequestMethod.GET)
public @interface GetMapping { /**
* Alias for {@link RequestMapping#name}.
*/
@AliasFor(annotation = RequestMapping.class)
String name() default "";

AliasFor的作用

  1.互为指定属性,比如 我们定义的service,但如果我们想 @Request("OrderService") 这样指定 service,就必须使用@AliasFor
  2.继承注解类中的互为别名关系 如GetMapping
@Request(value = "test1")
@Slf4j
public class Test { @org.junit.Test
@GetMapping("test4")
public void test4() throws NoSuchMethodException {
Request ann = AnnotationUtils.findAnnotation(getClass(),Request.class);
System.out.println(ann.value());
System.out.println(ann.service()); GetMapping test4 = AnnotationUtils.findAnnotation(getClass().getMethod("test4"), GetMapping.class);
System.out.println(Lists.newArrayList(test4.value()));
System.out.println(Lists.newArrayList(test4.path())); RequestMapping rq = AnnotationUtils.findAnnotation(getClass().getMethod("test4"), RequestMapping.class);
System.out.println(rq.method());
}

原理:

//AnnotationUtils static <A extends Annotation> A synthesizeAnnotation(A annotation, @Nullable Object annotatedElement) {
//判断当前的注解是否是合成的注解:方法上带有别名的注解。
if (!isSynthesizable(annotationType)) {
return annotation;
}
     //如果是合成的注解:构造动态代理,获取互为别名的注解属性。
DefaultAnnotationAttributeExtractor attributeExtractor =
new DefaultAnnotationAttributeExtractor(annotation, annotatedElement);
InvocationHandler handler = new SynthesizedAnnotationInvocationHandler(attributeExtractor); // Can always expose Spring's SynthesizedAnnotation marker since we explicitly check for a
// synthesizable annotation before (which needs to declare @AliasFor from the same package)
Class<?>[] exposedInterfaces = new Class<?>[] {annotationType, SynthesizedAnnotation.class};
return (A) Proxy.newProxyInstance(annotation.getClass().getClassLoader(), exposedInterfaces, handler);
												

@AliasFor 原理的更多相关文章

  1. SpringBoot系列二:SpringBoot自动配置原理

    主程序类的注解 @SpringBootApplication 注解,它其实是个组合注解,源码如下: @Target({ElementType.TYPE}) @Retention(RetentionPo ...

  2. SpringBoot入门(0) HelloWorld的实现与原理分析

    SpringBoot(0) HelloWorld的实现与原理分析 一.环境准备 1.1 环境约束 –jdk1.8:Spring Boot 推荐jdk1.7及以上:java version “1.8.0 ...

  3. Spring @AliasFor

    原文地址:https://blog.csdn.net/wolfcode_cn/article/details/80654730 在Spring的众多注解中,经常会发现很多注解的不同属性起着相同的作用, ...

  4. spring boot actuator工作原理之http服务暴露源码分析

    spring boot actuator的官方文档地址:https://docs.spring.io/spring-boot/docs/current/reference/html/productio ...

  5. Spring MVC源码(四) ----- 统一异常处理原理解析

    SpringMVC除了对请求URL的路由处理特别方便外,还支持对异常的统一处理机制,可以对业务操作时抛出的异常,unchecked异常以及状态码的异常进行统一处理.SpringMVC既提供简单的配置类 ...

  6. 从原理层面掌握@SessionAttribute的使用【一起学Spring MVC】

    每篇一句 不是你当上了火影大家就认可你,而是大家都认可你才能当上火影 前言 该注解顾名思义,作用是将Model中的属性同步到session会话当中,方便在下一次请求中使用(比如重定向场景~). 虽然说 ...

  7. 从原理层面掌握@RequestAttribute、@SessionAttribute的使用【一起学Spring MVC】

    每篇一句 改我们就改得:取其精华,去其糟粕.否则木有意义 前言 如果说知道@SessionAttributes这个注解的人已经很少了,那么不需要统计我就可以确定的说:知道@RequestAttribu ...

  8. 从原理层面掌握@ModelAttribute的使用(核心原理篇)【一起学Spring MVC】

    每篇一句 我们应该做一个:胸中有蓝图,脚底有计划的人 前言 Spring MVC提供的基于注释的编程模型,极大的简化了web应用的开发,我们都是受益者.比如我们在@RestController标注的C ...

  9. spring5 源码深度解析----- 被面试官给虐懵了,竟然是因为我不懂@Configuration配置类及@Bean的原理

    @Configuration注解提供了全新的bean创建方式.最初spring通过xml配置文件初始化bean并完成依赖注入工作.从spring3.0开始,在spring framework模块中提供 ...

随机推荐

  1. 你不知道的JavaScript下卷

    阿喀琉斯之踵 学习编程最好的办法就是编写代码 比较 隐式类型转换 数字和字符串 比较 字符串会隐式转换为数字,再进行比较 == 类型转换 ecma

  2. 532-数组中的K-diff数对

    532-数组中的K-diff数对 给定一个整数数组和一个整数 k, 你需要在数组里找到不同的 k-diff 数对.这里将 k-diff 数对定义为一个整数对 (i, j), 其中 i 和 j 都是数组 ...

  3. Pi和e的积分

    Evaluate integral $$\int_{0}^{1}{x^{-x}(1-x)^{x-1}\sin{\pi x}dx}$$ Well,I think we have $$\int_{0}^{ ...

  4. zabbix的web界面出现乱码解决方案

    1.问题描述:当我们搭建好zabbix服务器后,查看监控信息时,发现数据显示的下端文字显示为乱码. 2.解决方式: (1)拷贝windows系统字体: 可根据各自的喜好进行选择,我这边就选择楷体 常规 ...

  5. Vue中v-show和v-if的使用以及区别

    个人博客 地址:http://www.wenhaofan.com/article/20190321143330 v-if 1.v-if 根据条件渲染,它会确保在切换过程中条件块内的组件销毁和重建    ...

  6. EF模型+MySql问题

    1.添加ADO.NET模型不出现MYSQL连接时: a.确认mysql-for-visualstudio-1.2.7.msi是否安装 b.确认.net版本是否为4.6 2.已经选定了连接,但是在下一步 ...

  7. SpringBoot--SSM框架整合

    方式一 1 建立一个Spring Starter 2.在available中找到要用的包,配置mybatis 3.建立file,application.yml 文件 spring: datasourc ...

  8. Python入门9 —— 循环

    一:问号三连 1.什么是循环? 循环就是重复做一件事 2.为何要用循环? 为了让计算机能够像人一样去重复做事情 3.如何用循环 while循环,又称之为条件循环 for循环 二:循环 1.while循 ...

  9. LaTeX 文字带边框

    1.使用framed宏包 \usepackage{framed} 可以使用verb|...|和verbatim环境而不使用cprotect宏包的cprotect命令 \begin{framed} \v ...

  10. 生成器和迭代器_python

    一.生成器简介(generator) 在进行较大数据的存储,如果直接存储在列表之中,则会可能造成内存的不够与速度的减慢,因为列表创建完是立即创建并存在的,而在python中生成器(generator) ...