Spring @AliasFor用法

同注解内属性互为alias

  • @AliasFor的两端必须对称使用,如下name和value互为alias
  • 且两端的类型、默认值必须相同,
  • 在实际使用时仅赋值一个属性即可,若同时设置多个属性则要求值必须都相同
  • 通过Spring AnnotationUtils工具解析注解支持@AliasFor,直接使用Java Reflect机制不支持@AliasFor
java 复制代码
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnno {
	//注:@AliasFor的两端必须对称使用,如下name和value互为alias
	//且两端的类型、默认值必须相同,
	//在实际使用时仅赋值一个属性即可,若同时设置多个属性则要求值必须都相同
    @AliasFor("value")
    String name() default "biz";

    @AliasFor("name")
    String value() default "biz";
}

如下为AOP 切面内解析注解的示例,原生Java反射不支持解析@AliasFor,需要通过Spring工具类AnnotationUtils进行支持:

java 复制代码
@Aspect
@Component
public class MyAnnoAspect {

    @Pointcut("@annotation(com.luo.MyAnno)")
    public void myAnnoCut() {
    }

 
    @Before("myAnnoCut()")
    public Object before(ProceedingJoinPoint joinPoint) throws Throwable {
       MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
       //不支持:直接使用Java Reflect机制不支持@AliasFor
       MyAnno myAnno1 = methodSignature.getMethod().getAnnotation(MyAnno.class)
       //支持:通过Spring AnnotationUtils工具解析注解支持@AliasFor
       //即name和value值相同
       MyAnno myAnno2 = AnnotationUtils.getAnnotation(methodSignature.getMethod(), MyAnno.class);
    }
   //...
}

覆盖元注解属性

  • 当前注解需在定义处引用元注解
  • 当前注解属性作为元注解的属性的别名,标注了当前注解即相当于同时亦标注了元注解
  • 当前注解属性的默认值可以和元注解属性的默认值不同
  • AnnotationUtils.getAnnotation支持解析元注解,但是不支持@AliasFor
  • AnnotatedElementUtils.getMergedAnnotation支持解析元注解,并且支持@AliasFor
java 复制代码
//当前注解
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@MyMetaAnno //引用元注解
public @interface MyAnno {
    @AliasFor("value")
    String name() default "biz";

    @AliasFor("name")
    String value() default "biz";
	
	//当前注解内属性作为元注解属性的别名,即当前属性值会自动赋值到元注解相应属性中
	//注:当前注解属性的默认值可以和元注解属性的默认值不同
    @AliasFor(attribute = "keyAttr", annotation = MyMetaAnno.class)
    String keyAttr() default "myBizid";
}


//此为元注解(可作用于其他注解定义上)
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyMetaAnno {  
    String keyAttr() default "id";
}

如下为AOP 切面内解析注解的示例,AnnotationUtils.getAnnotation支持解析元注解,但是不支持@AliasFor,需通过AnnotatedElementUtils.getMergedAnnotation支持解析元注解且支持@AliasFor:

java 复制代码
@Aspect
@Component
public class MyAnnoAspect {

    @Pointcut("@annotation(com.luo.MyAnno)")
    public void myAnnoCut() {
    }

 
    @Before("myAnnoCut()")
    public Object before(ProceedingJoinPoint joinPoint) throws Throwable {
       MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();

       //通过Spring AnnotationUtils工具解析注解支持@AliasFor,
       //支持同注解内属性互为alias,即name和value值相同
       MyAnno myAnno = AnnotationUtils.getAnnotation(methodSignature.getMethod(), MyAnno.class);
		
	   //通过Spring AnnotatedElementUtils工具解析元注解,
	   //支持元注解@AliasFor属性,即MyAnno.keyAttr会自动赋值给MyMetaAnno.keyAttr,
	   //仅在切点方法上标注了MyAnno注解,但可以通过AnnotatedElementUtils解析MyAnno的关联的元注解MyMetaAnno,并支持属性的覆盖
       MyMetaAnno myMetaAnno = AnnotatedElementUtils.getMergedAnnotation(methodSignature.getMethod(), MyMetaAnno.class);
    }

   //...
}

参考:
https://www.cnblogs.com/ken-jl/p/9705502.html
https://www.baeldung.com/spring-aliasfor-annotation

相关推荐
先睡6 小时前
Redis的缓存击穿和缓存雪崩
redis·spring·缓存
Bug退退退12311 小时前
RabbitMQ 高级特性之死信队列
java·分布式·spring·rabbitmq
booooooty16 小时前
基于Spring AI Alibaba的多智能体RAG应用
java·人工智能·spring·多智能体·rag·spring ai·ai alibaba
极光雨雨16 小时前
Spring Bean 控制销毁顺序的方法总结
java·spring
Spirit_NKlaus17 小时前
解决HttpServletRequest无法获取@RequestBody修饰的参数
java·spring boot·spring
lwb_011818 小时前
SpringCloud——Gateway新一代网关
spring·spring cloud·gateway
lxsy19 小时前
spring-ai-alibaba 1.0.0.2 学习(七)——集成阿里云百炼平台知识库
学习·spring·阿里云·spring-ai·ai-alibaba
程序猿小D19 小时前
[附源码+数据库+毕业论文]基于Spring+MyBatis+MySQL+Maven+jsp实现的电影小说网站管理系统,推荐!
java·数据库·mysql·spring·毕业设计·ssm框架·电影小说网站
CodeWithMe20 小时前
【Note】《深入理解Linux内核》 Chapter 15 :深入理解 Linux 页缓存
linux·spring·缓存
llwszx21 小时前
Spring中DelayQueue深度解析:从原理到实战(附结构图解析)
java·后端·spring·delayqueue·延迟任务