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

相关推荐
曲莫终30 分钟前
SpEl表达式之强大的集合选择(Collection Selection)和集合投影(Collection Projection)
java·spring boot·spring
kaikaile19951 小时前
解密Spring Boot:深入理解条件装配与条件注解
java·spring boot·spring
广州山泉婚姻1 小时前
解锁高效开发:Spring Boot 3和MyBatis-Flex在智慧零工平台后端的应用实战
人工智能·spring boot·spring
三目君2 小时前
SpringMVC异步处理Servlet
java·spring·servlet·tomcat·mvc
fie88892 小时前
Spring MVC扩展与SSM框架整合
java·spring·mvc
用户590336360593 小时前
spring Context中代理对象和原始对象存储关系
spring
YuTaoShao7 小时前
Java八股文——Spring「SpringMVC 篇」
java·开发语言·spring
33255_40857_280597 小时前
深入解析Spring启动流程:从源码角度一步步揭秘Spring的魔法
java·spring
天天摸鱼的java工程师7 小时前
八年开发视角:Spring 框架在企业级应用中的实战场景解析
java·后端·spring
chanalbert8 小时前
Spring AOP 模块设计文档
后端·spring