@AliasFor 原理
用法:
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 原理的更多相关文章
- SpringBoot系列二:SpringBoot自动配置原理
主程序类的注解 @SpringBootApplication 注解,它其实是个组合注解,源码如下: @Target({ElementType.TYPE}) @Retention(RetentionPo ...
- SpringBoot入门(0) HelloWorld的实现与原理分析
SpringBoot(0) HelloWorld的实现与原理分析 一.环境准备 1.1 环境约束 –jdk1.8:Spring Boot 推荐jdk1.7及以上:java version “1.8.0 ...
- Spring @AliasFor
原文地址:https://blog.csdn.net/wolfcode_cn/article/details/80654730 在Spring的众多注解中,经常会发现很多注解的不同属性起着相同的作用, ...
- spring boot actuator工作原理之http服务暴露源码分析
spring boot actuator的官方文档地址:https://docs.spring.io/spring-boot/docs/current/reference/html/productio ...
- Spring MVC源码(四) ----- 统一异常处理原理解析
SpringMVC除了对请求URL的路由处理特别方便外,还支持对异常的统一处理机制,可以对业务操作时抛出的异常,unchecked异常以及状态码的异常进行统一处理.SpringMVC既提供简单的配置类 ...
- 从原理层面掌握@SessionAttribute的使用【一起学Spring MVC】
每篇一句 不是你当上了火影大家就认可你,而是大家都认可你才能当上火影 前言 该注解顾名思义,作用是将Model中的属性同步到session会话当中,方便在下一次请求中使用(比如重定向场景~). 虽然说 ...
- 从原理层面掌握@RequestAttribute、@SessionAttribute的使用【一起学Spring MVC】
每篇一句 改我们就改得:取其精华,去其糟粕.否则木有意义 前言 如果说知道@SessionAttributes这个注解的人已经很少了,那么不需要统计我就可以确定的说:知道@RequestAttribu ...
- 从原理层面掌握@ModelAttribute的使用(核心原理篇)【一起学Spring MVC】
每篇一句 我们应该做一个:胸中有蓝图,脚底有计划的人 前言 Spring MVC提供的基于注释的编程模型,极大的简化了web应用的开发,我们都是受益者.比如我们在@RestController标注的C ...
- spring5 源码深度解析----- 被面试官给虐懵了,竟然是因为我不懂@Configuration配置类及@Bean的原理
@Configuration注解提供了全新的bean创建方式.最初spring通过xml配置文件初始化bean并完成依赖注入工作.从spring3.0开始,在spring framework模块中提供 ...
随机推荐
- pod has unbound immediate PersistentVolumeClaims : statefulset挂载不上pv的另一种情况
大家都直到当volumeClaimTemplates匹配不上pv时,会出现statefulset挂载不到pv的问题.错误提示如下: error while running "VolumeBi ...
- 5G三大应用场景
5G三大应用场景:eMBB(增强移动宽带).eMTC(海量物联).uRLLC(高可靠低时延连接) ------20191215闪
- 【巨杉数据库SequoiaDB】巨杉Tech | 巨杉数据库数据高性能数据导入迁移实践
SequoiaDB 一款自研金融级分布式数据库产品,支持标准SQL和分布式事务功能.支持复杂索引查询,兼容 MySQL.PGSQL.SparkSQL等SQL访问方式.SequoiaDB 在分布式存储功 ...
- Help Jimmy POJ - 1661 dp
#include<iostream> #include<stdio.h> #include<algorithm> #include<cstring> u ...
- Common Subsequence POJ - 1458 最长公共子序列 线性DP
#include <iostream> #include <algorithm> #include <string> #include <cstring> ...
- Failed to restart docker.service: Unit not found 镜像加速
解决方案 以前的安装残留 重新安装 find / -name "docker*" centos8 添加软件源信息 yum-config-manager --add-repo htt ...
- PHP对一维数组去重
一维数组去重 $arr =[1,2,2,3,6]; $arr1 =array_flip($arr); $arr =array_flip($arr1); return $arr; array_flip( ...
- linux - mysql:安装mysql
安装环境 系统是 centos6.5 1.下载 下载地址:http://dev.mysql.com/downloads/mysql/5.6.html#downloads 下载版本:我这里选择的5.6. ...
- TAB_JG_COMMAND_SERVERINFO
简介 存储设备基本信息 表结构
- 动态生成sku组合输入列表
<!DOCTYPE html> <html> <head> <title></title> <meta http-equiv=&quo ...