@AliasFor注解

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

一、前言

@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 {};
}
  • 7
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怪 咖@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值