spring-@AliasFor注解

本文深入探讨了Spring框架中的@AliasFor注解,包括注解内部的显性别名、用于元注解属性的显性别名、注解中的隐性别名以及别名传递的概念和用法。通过实例展示了如何定义和使用别名,强调了别名属性的限制和注意事项,帮助读者理解和应用@AliasFor注解提高代码的灵活性和可读性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1、注解内部的显性别名

2、用于元注解属性的显性别名

3、注解中的隐性别名

4、别名传递


@AliasFor是spring中的一个注解,可用来对注解属性声明别名。若想起到别名效果,需通过spring进行处理后才能有效果。

互为别名的属性只设置其中一个值,互为别名的其他属性同时也会设置相同值

@TestAliasFor(value="value")
class TestDemo{
}

如果同时设置别名属性值,则值必须一致,否则报错

@TestAliasFor(value="value",name="value")
class TestDemo{
}

1、注解内部的显性别名

@Retention(RetentionPolicy.RUNTIME)
public @interface TestAliasFor {
    @AliasFor("name")
    String value() default "";

    @AliasFor("value")
    String name() default "";
}
@Test
    public void test6(){
        TestAliasFor annotation = AnnotationUtils.findAnnotation(TestDemo.class,                 
                                  TestAliasFor.class);
        System.out.println(annotation.value());
        System.out.println(annotation.name());
    }

  注意

1.组成别名对的每个属性都必须用@AliasFor进行注释,并且AliasFor中的值必须指向别名对中的另一个属性
2.别名化的属性必须声明相同的返回类型
3.别名化的属性必须声明默认值
4.别名化的属性默认值必须相同

注解内部显示别名只能一对互相别名,若是有3个内部注解互为显性别名,则报错,如下

2、用于元注解属性的显性别名

如果被@AliasFor注解的属性指向的是它所在注解之外的其他注解,那么这个属性被解释成元注解属性的别名。(称之为显性的元注解属性重写)

@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface TestAliasFor1 {
    String value() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@TestAliasFor1
public @interface TestAliasFor {
    @AliasFor(annotation = TestAliasFor1.class, attribute = "value")
    String value() default "";
}
@Test
    public void test6(){
        TestAliasFor annotation = AnnotationUtils.findAnnotation(TestDemo.class, TestAliasFor.class);
        TestAliasFor1 mergedAnnotation = AnnotatedElementUtils.getMergedAnnotation(TestDemo.class, TestAliasFor1.class);
        System.out.println(annotation.value());
        System.out.println(mergedAnnotation.value() );
    }

注意

1 如果一个属性是一个元注解属性的别名,那么这个属性必须用@AliasFor进行注解并且该属性必须指向元注解属性(若是没指定元注解属性名称,则默认与属性名称一致)
2 别名化的属性必须声明相同的返回结果
3.@AliasFor的annotation属性必须引用元注解
4.被引用的元注解必须放置在声明了@AliasFor的注解类上

3、注解中的隐性别名

如果注解中的一个或多个属性声明为同一个元注解属性的属性重写(直接地或传递地重写)那么这些注解会被当作彼此的隐性别名集来对待,结果是它们的行为类似于注解中的显性别名

@Retention(RetentionPolicy.RUNTIME)
@Inherited
@TestAliasFor1
public @interface TestAliasFor {
    @AliasFor(annotation = TestAliasFor1.class, attribute = "value")
    String value() default "";

    @AliasFor(annotation = TestAliasFor1.class, attribute = "value")
    String name() default "";
}
@Test
    public void test7(){
        TestAliasFor testAliasFor = AnnotatedElementUtils.getMergedAnnotation(TestDemo.class, TestAliasFor.class);
        System.out.println(testAliasFor.name());
        System.out.println(testAliasFor.value());
        
        TestAliasFor1 testAliasFor1 = AnnotatedElementUtils.getMergedAnnotation(TestDemo.class, TestAliasFor1.class);
        System.out.println(testAliasFor1.value());
        
    }

TestAliasFor中的value和name属性都复写了TestAliasFor1的value属性,所以TestAliasFor的value和name是彼此的隐性别名,如下TestAliasFor2的name2和value2也是彼此的隐性别名

@Retention(RetentionPolicy.RUNTIME)
@TestAliasFor
public @interface TestAliasFor2 {
    @AliasFor(annotation = TestAliasFor.class, attribute = "name")
    String name2() default "";
    @AliasFor(annotation = TestAliasFor.class, value = "value")
    String value2() default "";
}

注意

1.属于隐性别名组中的每一个属性必须使用@AliasFor进行注解,并且attribute必须引用相同元注解中的同一个属性(或者相同元注解对应的别名属性)
2.别名化的属性必须声明相同的返回类型
3.别名化的属性必须定义默认值
4.别名化的属性必须声明相同的默认值
5.注解必须引用合适的元注解
6.被引用的元注解必须放置在声明了@AliasFor的注解上

4、别名传递

@AliasFor注解是允许别名之间的传递的,简单理解,如果A是B的别名,并且B是C的别名,那么A是C的别名

参考:

@AliasFor注解_不想做咸鱼的王富贵的博客-优快云博客_@aliasfor

Spring中的@AliasFor标签_wolfcode_cn的博客-优快云博客_@aliasfor

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值