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

相关推荐
Minyy112 小时前
SpringBoot程序的创建以及特点,配置文件,LogBack记录日志,配置过滤器、拦截器、全局异常
xml·java·spring boot·后端·spring·mybatis·logback
ai大佬9 小时前
Java 开发玩转 MCP:从 Claude 自动化到 Spring AI Alibaba 生态整合
java·spring·自动化·api中转·apikey
来自星星的猫教授11 小时前
spring,spring boot, spring cloud三者区别
spring boot·spring·spring cloud
Bling_12 小时前
请求参数、路径参数、查询参数、Spring MVC/FeignClient请求相关注解梳理
java·spring·spring cloud·mvc
-曾牛13 小时前
企业级AI开发利器:Spring AI框架深度解析与实战
java·人工智能·python·spring·ai·rag·大模型应用
二进制独立开发13 小时前
[Trae 04.22+]适用于JAVA Spring开发的智能体提示词
spring·trae
大家都说我身材好14 小时前
Spring缓存注解深度实战:3大核心注解解锁高并发系统性能优化‌
spring·缓存·性能优化
都叫我大帅哥15 小时前
Spring AI中的ChatClient:从入门到精通,一篇搞定!
java·spring·ai编程
都叫我大帅哥15 小时前
《@SpringBootApplication:Spring Boot的"一键启动"按钮,还是程序员的"免死金牌"?》
java·后端·spring
-曾牛15 小时前
Spring AI 快速入门:从环境搭建到核心组件集成
java·人工智能·spring·ai·大模型·spring ai·开发环境搭建