当前位置: 首页 > news >正文

@AliasFor注解

读本篇文章最低要求掌握Java注解,如果对注解不是特别了解的,建议您先读完这篇文章:https://blog.csdn.net/weixin_43888891/article/details/126963074

目录

    • 一、前言
    • 二、AnnotationUtils 和 AnnotatedElementUtils
    • 三、同一个注解类的属性设置互为别名
    • 四、将一个注解上的属性值传递给另一个注解
    • 五、@AliasFor注解应用场景
      • 5.1. @SpringBootApplication源码
      • 5.2. @RequestMapping源码

一、前言

@AliasFor注解基本上都是在spring源码当中出现的,AliasFor是Spring提供的注解,Alias是别名的意思,For是为了,首先我们通过命名可以得出一个结论,他是为了别名而自定义的注解!

Spring中@AliasFor注解的作用有两点:

  1. 将同一个注解类的属性设置互为别名
  2. 将一个注解上的属性值传递给另一个注解

但这并不是java原生支持的,需要通过Spring中提供的工具类:org.springframework.core.annotation.AnnotationUtils或者org.springframework.core.annotation.AnnotatedElementUtils来解析。AnnotatedElementUtils内部还是调用的AnnotationUtils。

源码如下:它有三个属性value和attribute以及annotation,@AliasFor注解注释了自身,并且value和attribute互为别名,通过源码很容易知道,当我们使用这个注解,@AliasFor(value=“xxx”)和@AliasFor(attribute=“xxx”)是等价的

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public @interface AliasFor {
    @AliasFor("attribute")
    String value() default "";

    @AliasFor("value")
    String attribute() default "";

    Class<? extends Annotation> annotation() default Annotation.class;
}

二、AnnotationUtils 和 AnnotatedElementUtils

Java 运行时读取Annotation 需要通过反射,Spring 提供AnnotationUtils , AnnotationElementUtils 用于简化操作,其他特点如下:

  1. 查询Meta Annotation(注解的注解)
  2. 对@AliasFor 解析,生成指定注解的代理,并赋值。(注:定义其他Annotation 的别名)

Utils 调用涉及的元素:

  1. Annotation: Annotation 元数据
  2. AnnotatedElement: 被Annotation注解的对象,包括annotation , class , method等
  3. Method: 类和接口中的方法信息

AnnotationUtils常用方法:

  1. getAnnotation: 从某个类获取某个annotation
  2. findAnnotation: 从类或方法中查找某个annotation。
  3. isAnnotationDeclaredLocally: 验证annotation是否直接注释在类上而不是集成来的。
  4. isAnnotationInherited: 验证annotation是否继承于另一个class。
  5. getAnnotationAttributes: 获取annotation的所有属性。
  6. getValue: 获取指定annotation的值.
  7. getDefaultValue: 获取指定annotation或annotation 属性的默认值

AnnotatedElementUtils常用方法:

  1. findMergedAnnotation:这个方法会合并@AliasFor传递的值

三、同一个注解类的属性设置互为别名

1.自定义一个EnableCVS 注解,一共有两个属性

import org.springframework.core.annotation.AliasFor;

import java.lang.annotation.*;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EnableCVS {
	
	// 这里就算是改成@AliasFor(attribute = "address")测试结果也是一样的
    @AliasFor(value = "address")
    String value() default "";

    @AliasFor(value = "value")
    String address() default "";
}

2.创建一个类,然后使用自定义的注解@EnableCVS修饰

@Configuration
@EnableCVS(address = "hhh")
public class AppConfig {

}

3.测试:通过两种方式获取自定义注解当中的属性值

import org.springframework.core.annotation.AnnotationUtils;

public class Test {
    public static void main(String[] args) {
    	// spring提供的
        EnableCVS annotation = AnnotationUtils.findAnnotation(AppConfig.class, EnableCVS.class);
        System.out.println("AnnotationUtils:address:" + annotation.address());
        System.out.println("AnnotationUtils:value:" + annotation.value());
		
		// jdk原生
        EnableCVS annotation1 = AppConfig.class.getAnnotation(EnableCVS.class);
        System.out.println("AppConfig:address:" + annotation1.address());
        System.out.println("AppConfig:value:" + annotation1.value());
    }
}

4.输出结果如下:首先我们设置的注解是@EnableCVS(address = "hhh") ,只设置了address属性,并没有设置value属性,会发现jdk原生方式获取value的时候是拿不到值的,而spring提供的AnnotationUtils却可以获取到,而且获取到的就是address的值!

在这里插入图片描述

其实就可以理解为,一旦value值设置了如下注解@AliasFor(value = "address"),也就意味着通过AnnotationUtils来获取value属性值的时候,当value值没有设置的时候,实际上会去获取address属性的值!

@AliasFor(value = "address")
String value() default "";

注意:如果@AliasFor注解当中两个属性互相设置了@AliasFor别名,并且使用自定义注解的时候,同时设置address和value的值,这时候通过AnnotationUtils#findAnnotation(Class<?>, annotationType)获取属性值,则会抛出异常!示例如下:

@Configuration
@EnableCVS(value = "hhh",address = "222")
public class AppConfig {

}

四、将一个注解上的属性值传递给另一个注解

1.自定义注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Scope("singleton")
@Component
@Inherited
public @interface SingletonComponent {

    @AliasFor(annotation = Component.class, attribute = "value")
    String value() default "";
}

2.声明一个类,使用@SingletonComponent修饰

@SingletonComponent("simpleService")
public class SimpleSingletonService {

}

3.通过AnnotationUtils和AnnotatedElementUtils获取注解

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Scope;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

// 这个注解一定要加,不然getAllAnnocations方法获取不到值
@ComponentScan
public class AnnotationUtilsDemo {
    private static void annotationUtilsDemo() {

        // 获取类注解
        SingletonComponent singletonComponentAnnocation = AnnotationUtils.
                findAnnotation(SimpleSingletonService.class, SingletonComponent.class);

        System.out.println("@SingletonComponent : " + singletonComponentAnnocation);
        System.out.println("@SingletonComponent value: " + AnnotationUtils.getValue(singletonComponentAnnocation, "value"));


        System.out.println("----------------------------------------------");

        Scope scopeAnnocation = AnnotationUtils.findAnnotation(SimpleSingletonService.class, Scope.class);
        System.out.println("@Scope : " + scopeAnnocation);
        System.out.println("@Scope value: " + AnnotationUtils.getValue(scopeAnnocation, "scopeName"));

        System.out.println("----------------------------------------------");

        // 获取@AliasFor Marge 后的注解,直接调用 AnnotationUtils的方法不会组合@AliasFor的值,需要调用AnnotatedElementUtils
        Component componentAnnocation = AnnotatedElementUtils.findMergedAnnotation(SimpleSingletonService.class, Component.class);
        System.out.println("@Component : " + componentAnnocation);
        System.out.println("@Component value: " + AnnotationUtils.getValue(componentAnnocation, "value"));
    }

    private static void getAllAnnocations() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AnnotationUtilsDemo.class);
        // 获取SingletonComponent注解修饰的类
        Map<String, Object> beans = context.getBeansWithAnnotation(SingletonComponent.class);
        for (Object bean : beans.values()) {
            System.out.println("bean : " + bean);
            // @SingletonComponent 继承了 @Component 所以存在实例,@Component的value值就是通过@AliasFor注解传递过去的
            Component componentAnnocation = AnnotatedElementUtils.findMergedAnnotation(bean.getClass(), Component.class);
            System.out.println(componentAnnocation);
        }
    }

    public static void main(String[] args) {
        AnnotationUtilsDemo.annotationUtilsDemo();

        System.out.println("----------------------------------------------");

        AnnotationUtilsDemo.getAllAnnocations();
    }
}

4.输出结果

Connected to the target VM, address: '127.0.0.1:49763', transport: 'socket'
@SingletonComponent : @com.gzl.cn.springbootnacos.aa.SingletonComponent(value="simpleService")
@SingletonComponent value: simpleService
----------------------------------------------
@Scope : @org.springframework.context.annotation.Scope(proxyMode=DEFAULT, scopeName="singleton", value="singleton")
@Scope value: singleton
----------------------------------------------
@Component : @org.springframework.stereotype.Component(value="simpleService")
@Component value: simpleService
----------------------------------------------
bean : com.gzl.cn.springbootnacos.aa.SimpleSingletonService@1b759d6
@org.springframework.stereotype.Component(value="simpleService")

五、@AliasFor注解应用场景

5.1. @SpringBootApplication源码

如下所示@SpringBootApplication并没有定义新的属性,而是复用其他注解已有的注解属性,并对其进行组合形成新的注解从而到达到便捷的目的。这样的注解我们可以称之为复合注解。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
    @AliasFor(annotation = EnableAutoConfiguration.class)
    Class<?>[] exclude() default {};

    @AliasFor(annotation = EnableAutoConfiguration.class)
    String[] excludeName() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackages"
    )
    String[] scanBasePackages() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackageClasses"
    )
    Class<?>[] scanBasePackageClasses() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "nameGenerator"
    )
    Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;

    @AliasFor(annotation = Configuration.class)
    boolean proxyBeanMethods() default true;
}

所以在使用SpringBoot 时我们只需要@SpringBootApplication一个注解就能开启自动配置,自动扫描的功能。而不再需要使下面三个注解来达到同样的目的。

@Configuration
@ComponentSan
@EnnableAutoConfiguration

5.2. @RequestMapping源码

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
    String name() default "";

    @AliasFor("path")
    String[] value() default {};

    @AliasFor("value")
    String[] path() default {};

    RequestMethod[] method() default {};

    String[] params() default {};

    String[] headers() default {};

    String[] consumes() default {};

    String[] produces() default {};
}

相关文章:

  • 微信公众号扫码登录(一)—— 获取微信公众号二维码
  • JUC-多线程锁Synchronized锁的八种情况1
  • qt udp tcp代替RPC(一)
  • 刷题记录:牛客NC50965Largest Rectangle in a Histogram
  • 在线副业教程之 02 你学的越多,你赚的越多+你必须开始学习的5个最好的在线副业
  • VUE | key的内部原理、Vue监测数据的原理、Vue.set()和vm.$set()的使用
  • Centos/Docker 环境中文乱码如何解决
  • VS2019 Qt源码编译
  • Linux8-fork父子进程逻辑地址相同、进程的逻辑地址与物理地址、fork相关例题、僵死进程
  • java毕业设计普通中学体育卫生信息管理系统源码+lw文档+mybatis+系统+mysql数据库+调试
  • 基于C语言的查找算法汇编
  • 多网段多通道IP地址和通讯端口转换
  • 【PyQt】PyQt入门安装和Hello World
  • 怎样创建一个VUE项目(超简单)
  • C++【STL】【queue的使用和模拟实现】【priority_queue的使用和模拟实现】
  • 【EOS】Cleos基础
  • Javascript 原型链
  • Laravel深入学习6 - 应用体系结构:解耦事件处理器
  • SpringCloud(第 039 篇)链接Mysql数据库,通过JpaRepository编写数据库访问
  • 技术攻略】php设计模式(一):简介及创建型模式
  • 聊聊spring cloud的LoadBalancerAutoConfiguration
  • 前端每日实战:61# 视频演示如何用纯 CSS 创作一只咖啡壶
  • 日剧·日综资源集合(建议收藏)
  • 限制Java线程池运行线程以及等待线程数量的策略
  • 一个项目push到多个远程Git仓库
  • 【运维趟坑回忆录 开篇】初入初创, 一脸懵
  • 2017年360最后一道编程题
  • 国内开源镜像站点
  • ​软考-高级-信息系统项目管理师教程 第四版【第14章-项目沟通管理-思维导图】​
  • #14vue3生成表单并跳转到外部地址的方式
  • $$$$GB2312-80区位编码表$$$$
  • (第8天)保姆级 PL/SQL Developer 安装与配置
  • (附程序)AD采集中的10种经典软件滤波程序优缺点分析
  • (附源码)spring boot球鞋文化交流论坛 毕业设计 141436
  • (附源码)ssm高校志愿者服务系统 毕业设计 011648
  • (译) 理解 Elixir 中的宏 Macro, 第四部分:深入化
  • (转)c++ std::pair 与 std::make
  • (转)linux下的时间函数使用
  • (转)nsfocus-绿盟科技笔试题目
  • ***linux下安装xampp,XAMPP目录结构(阿里云安装xampp)
  • .dat文件写入byte类型数组_用Python从Abaqus导出txt、dat数据
  • .NET Windows:删除文件夹后立即判断,有可能依然存在
  • .NET 设计模式初探
  • .net 提取注释生成API文档 帮助文档
  • .Net+SQL Server企业应用性能优化笔记4——精确查找瓶颈
  • .net反混淆脱壳工具de4dot的使用
  • .NET微信公众号开发-2.0创建自定义菜单
  • .sh 的运行
  • @angular/cli项目构建--Dynamic.Form
  • @CacheInvalidate(name = “xxx“, key = “#results.![a+b]“,multi = true)是什么意思
  • [ MSF使用实例 ] 利用永恒之蓝(MS17-010)漏洞导致windows靶机蓝屏并获取靶机权限
  • [ 环境搭建篇 ] 安装 java 环境并配置环境变量(附 JDK1.8 安装包)
  • [145] 二叉树的后序遍历 js
  • [ActionScript][AS3]小小笔记
  • [cocos2d-x]关于CC_CALLBACK