用法:

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. 学习Python常用的工具

    Python编程语言 Python是一门高级计算机程序设计语言! Python是一种解释型(脚本)语言,因为其代码简明,书写效率高,功能强大.易扩展.有丰富的专业库而受大众欢迎! 最常用的专业库有: ...

  2. CSS的display显示

    CSS的display显示 1. 行内元素和块级元素关系 块级元素:1.标题标签:h1~h6:2.段落标签:p1~p6:3.div:4.列表:等 行内元素:1.span:2.a:3.img:4.str ...

  3. CSS的列表样式和网页背景

    CSS的列表样式 1. 设置title和列表 HTML: <!DOCTYPE html><html lang="en"><head>    &l ...

  4. vjudge I - Vladik and fractions 一道小学生的提。

    原题链接:https://vjudge.net/contest/331993#problem/I Vladik and Chloe decided to determine who of them i ...

  5. BZOJ3531 SDOI2014 旅行 - 树链剖分,主席树

    题意:给定一棵树,树上每个点有权值和类型.支持:修改某个点的类型:修改某个点的权值:询问某条链上某个类型的点的和/最大值.点数/类型数/询问数<=100000. 分析: 树链剖分,对每个类型的点 ...

  6. CSS伪类before,after制作左右横线中间文字效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. mysql的优化总结

    1,允许数据库适当冗余提高查询效率 2,对于索引的使用要适当

  8. python接口自动化测试之http协议(一)

    1.http(超文本传输)协议:是一个基于请求与响应模式的.无状态的(不会记住每个请求的状态).应用层协议 2.url详解 https://www.baidu.com/s?ie=utf-8&f ...

  9. C语言注释符号

    同学们认为注释很简单,那我来看看下面的代码是否正确? 1.似是而非的问题 int main() { int/*...*/i; char* s = "abcdefgh //hijklmn&qu ...

  10. docker使用nginx实现ssl(https)反向代理其他容器应用

    安装nginx容器 搜索nginx镜像 docker search nginx 拉取最新版nginx docker pull nginx:latest 运行容器 docker run --name=n ...