@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模块中提供 ...
随机推荐
- java - jdk线程池详解
线程池参数详解 public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUni ...
- Leetcode Week2 Two Sum
Question Given an array of integers, return indices of the two numbers such that they add up to a sp ...
- 申请一个美国paypal账户
近期为了做PayPal支付,进口demo已经写好,就差一个美国PayPal账户进行支付测试,几经折腾,终于申请下来,附上参考链接: paypal官网: https://www.paypal.com ...
- 创建登录WEB UI页面的Business role.
1: Define business role 2: business role 中可以指定 config key, 该config key可以用于UI configurationo determ ...
- Dataset: online data
From Kaggle: Appliances Energy Prediction Energy consumption of the Netherlands International Energy ...
- python3练习100题——028
原题链接:http://www.runoob.com/python/python-exercise-example28.html 题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁.问第4 ...
- ECMAScript基本语法——①与HTML的结合方式
内部JS: HTML页面内定义一个script标签,标签内就是JS代码一般情况会写到特定的位置,写到head标签内 外部JS: HTML页面内定义一个script标签,使用src加载外部的JS文件 注 ...
- 动图演示23个鲜为人知的VSCode快捷键
动图演示23个鲜为人知的VSCode快捷键 原文地址:dev.to/devmount/23… 代码同步浏览器 安装vccode 安装live server插件 尽管我在VS Code中经常使用许多快捷 ...
- opencv-python常用接口
最直接的是参考官网:https://docs.opencv.org/4.2.0/d6/d00/tutorial_py_root.html
- maven scope 的作用
一: 1.Maven中的依赖作用范围概述 Maven中使用 scope 来指定当前包的依赖范围和依赖的传递性.常见的可选值有:compile, provided, runtime, test, sys ...