注解Spring @AliasFor使用笔记

  1. 简单注解类

    java 复制代码
    @Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    public @interface ErasePasswordAnno {
    @AliasFor(attribute = "expression")
    String value() default "" ;
    
        String expression() default "" ;
    }
  2. 嵌套注解类

    java 复制代码
    @Target({ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @ErasePasswordAnno
    public @interface MapErasePasswordAnno {
        @AliasFor(annotation = ErasePasswordAnno.class, attribute = "value")
        String value() default "['recordList']" ;
    
        @AliasFor(annotation = ErasePasswordAnno.class, attribute = "expression")
        String expression() default "['recordList']" ;
    }
  3. 编写测试代码

    java 复制代码
    @Slf4j
    public class AliasForAnnoTest {
    
        @Test
        void simple() throws NoSuchMethodException {
            Method method = MapErasePasswordAnnoTest.class.getMethod("hello");
            ErasePasswordAnno annotation = AnnotationUtils.getAnnotation(method, ErasePasswordAnno.class);
            Assertions.assertNotNull(annotation);
            log.info("annotation value : {}", annotation.value());
            log.info("annotation expression : {}", annotation.value());
        }
    
        @Test
        void complex() throws NoSuchMethodException {
            Method method = MapErasePasswordAnnoTest.class.getMethod("hello2");
            ErasePasswordAnno annotation = AnnotatedElementUtils.findMergedAnnotation(method, ErasePasswordAnno.class);
            Assertions.assertNotNull(annotation);
            log.info("annotation value : {}", annotation.value());
            log.info("annotation expression : {}", annotation.value());
        }
        
        @ErasePasswordAnno("username")
        public void hello(){
    
        }
    
        @MapErasePasswordAnno
        public void hello2(){
    
        }
    }
相关推荐
文丑颜不良啊1 小时前
LeetCode2981~3000题目解析
java·leetcode
nice_lcj5203 小时前
【从零理解 Spring AI · 13】RAG 要解决什么:把“闭卷考试“变成“开卷“
java·spring·检索增强·rag·spring ai·ai应用开发
学长毕设3 小时前
基于SpringBoot军事纪念馆管理系统设计与实现-附源码
java·spring boot·后端
这个昵称不容易4 小时前
SpringBoot 医疗病历交互系统的设计与实现:192 个 Java 文件、256 个接口、22 张表,附真实运行截图
java·springboot·毕设
驭渊的小故事5 小时前
订单服务如何调用商品服务?一次 RestTemplate 跨服务调用入门
java·开发语言·php
文丑颜不良啊7 小时前
LeetCode3001~3020题目解析
java·leetcode
山楂树の8 小时前
Java 字符串高效处理:StringBuilder、StringBuffer、StringJoiner
java·开发语言
.痞老板.10 小时前
java ee--文件操作和IO
java·java-ee
简 洁 冬冬10 小时前
分页「向上取整」公式 `(total + size - 1) / size` 的完整推导
java·分页
装不满的克莱因瓶12 小时前
【从零搭建】AI大模型之ReactAgent智能体对接MCP实战
java·人工智能·ai·springai·mcp·springaialibaba