zoukankan      html  css  js  c++  java
  • Spring:如何实现注解的组合

    Spring中存在很多注解组合的情况,例如@RestController

    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Controller
    @ResponseBody
    public @interface RestController {
    
    	/**
    	 * The value may indicate a suggestion for a logical component name,
    	 * to be turned into a Spring bean in case of an autodetected component.
    	 * @return the suggested component name, if any (or empty String otherwise)
    	 * @since 4.0.1
    	 */
    	@AliasFor(annotation = Controller.class)
    	String value() default "";
    
    }
    

    @RestController就是@Controller、@ResponseBody两个注解的组合,同时产生两个注解的作用。
    本人一开始以为这是Java的特性,Java能够通过注解上的注解实现自动组合注解的效果。于是写了这样一段代码

    /**
     * @author Fcb
     * @date 2020/6/23
     * @description
     */
    @Documented
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyComponent {
    }
    
    /**
     * @author Fcb
     * @date 2020/6/23
     * @description
     */
    @Documented
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @MyComponent
    public @interface MyController {
    }
    
    @MyController
    public class AnnotatedService {
    }
    

    结果测试发现翻车

    /**
     * @author Fcb
     * @date 2020/6/23
     * @description
     */
    public class Test {
    
        public static void main(String[] args) {
            Annotation[] annotations = AnnotatedService.class.getAnnotations();
            for (Annotation anno : annotations) {
                System.out.println(anno.annotationType());
                System.out.println(anno.annotationType() == MyComponent.class);
            }
        }
    }
    

    打印结果如下:

    interface com.example.demo.anno.MyController
    false
    

    经过本人查阅资料,发现我想要的那个注解组合注解的功能是Spring自己实现的。。通过Spring中的AnnotationUtils.findAnnotation(类,注解)方法来判断某个类上是否能找到组合的注解。
    比如现在我想知道AnnotatedService这个类上是否存在@MyComponent注解,毕竟这是我一开始的目的(通过组合减少注解),我可以调用一下代码

    /**
     * @author Fcb
     * @date 2020/6/23
     * @description
     */
    public class Test {
    
        public static void main(String[] args) {
            Annotation[] annotations = AnnotatedService.class.getAnnotations();
            System.out.println(AnnotationUtils.findAnnotation(AnnotatedService.class, MyComponent.class));
        }
    }
    

    打印如下:

    @com.example.demo.anno.MyComponent()
    

    假如传入的注解是一个不存在的值,则会返回null,示例如下:

    /**
     * @author Fcb
     * @date 2020/6/23
     * @description
     */
    public class Test {
    
        public static void main(String[] args) {
            Annotation[] annotations = AnnotatedService.class.getAnnotations();
            System.out.println(AnnotationUtils.findAnnotation(AnnotatedService.class, OtherAnno.class));
        }
    }
    

    控制台打印:

    null
    

    总结:Java本身没有实现 通过标记注解 来组合注解的功能。假如我们自定义注解时需要可以使用Spring的AnnotationUtils.findAnnotation()的方法帮助我们实现。

    希望这篇文章对你有帮助~~~有空的话记得顺手点赞呀~

  • 相关阅读:
    redis liunx安装
    db2实现每条数据累加
    js实现目录链接,内容跟着目录滚动显示
    Anaconda3安装过程中遇到“Anaconda3-5.1.0-Linux-x86_64.sh:行350: bunzip2: 未找到命令 tar: 它似乎不像是一个 tar 归档文件 tar: 由于前次错误,将以上次的错误状态退出”
    java axis2生成wsdl
    java axis2解析xml(wsdl返回List数据Map<String,Object>
    java axis2解析xml(wsdl返回List数据Map<String,String>
    java axis2解析xml(wsdl返回List数据)
    jquery 合并单元格,rowspan
    poi导出excel
  • 原文地址:https://www.cnblogs.com/fcb-it/p/13185450.html
Copyright © 2011-2022 走看看