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()的方法帮助我们实现。

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

Spring:如何实现注解的组合的更多相关文章

  1. Spring Boot @Condition 注解,组合条件你知道吗

    上一篇文章 你应该知道的 @ConfigurationProperties 注解的使用姿势,这一篇就够了 介绍了如何通过 @ConfigurationProperties 注解灵活读取配置属性,这篇文 ...

  2. 【转】Spring Boot @Condition 注解,组合条件你知道吗

    原文:https://www.cnblogs.com/FraserYu/p/11280420.html 上一篇文章 你应该知道的 @ConfigurationProperties 注解的使用姿势,这一 ...

  3. JavaEE开发之Spring中的条件注解、组合注解与元注解

    上篇博客我们详细的聊了<JavaEE开发之Spring中的多线程编程以及任务定时器详解>,本篇博客我们就来聊聊条件注解@Conditional以及组合条件.条件注解说简单点就是根据特定的条 ...

  4. Spring 的@Scheduled注解实现定时任务运行和调度

    Spring 的@Scheduled注解实现定时任务运行和调度 首先要配置我们的spring.xml   ---  即spring的主配置文件(有的项目中叫做applicationContext.xm ...

  5. Spring基于纯注解方式的使用

    经过上篇xml与注解混合方式,对注解有了简单额了解,上篇的配置方式极大地简化了xml中配置,但仍有部分配置在xml中进行,接下来我们就通过注解的方式将xml中的配置用注解的方式实现,并最终去掉xml配 ...

  6. Spring之缓存注解@Cacheable

    https://www.cnblogs.com/fashflying/p/6908028.html https://blog.csdn.net/syani/article/details/522399 ...

  7. Spring的Cache注解

    Spring的Cache注解如下所示: @CacheConfig:主要用于配置该类中会用到的一些共用的缓存配置.在这里@CacheConfig(cacheNames = "users&quo ...

  8. Spring 3.0 注解

    原文 :http://www.blogjava.net/ashutc/archive/2011/04/14/348270.html 另两 参考博客 : http://kingtai168.iteye. ...

  9. Spring的相关注解

    说明写在最前面:摘录于   博客园--受伤滴小萝卜   文章 文章链接受伤滴小萝卜文章--Spring注解 本文章只用作学习和帮助其他人学习记录使用 Spring 注解学习笔记 声明Bean的注解: ...

随机推荐

  1. SpringBoot--SpringMVC自动配置

    SpringMVC自动配置 1.SpringBoot官方文档对SpringMVC的默认配置: Inclusion of ContentNegotiatingViewResolver and BeanN ...

  2. burpsuite 2.0beta体验

    这里有破解版:http://ximcx.cn/post-110.html 一直再用1.7x版本,2.0的还没怎么用过 移除了 Scanner 和spider 选项卡,全部整理到Dashboard里 代 ...

  3. Rocket - devices - PLIC

    https://mp.weixin.qq.com/s/FR3yeLLBqy0n-fflw-ATgg 简单介绍TLPLIC的实现. 1. GatewayPLICIO PLIC是Platform-Leve ...

  4. Chisel3 - model - connect

    https://mp.weixin.qq.com/s/w8NqM3GVlF0NydpsB65KPg   介绍创建模块顺序逻辑的connect命令.     0. 这里先简单对 "=" ...

  5. QTI EAS学习之find_energy_efficient_cpu

    Energy Awareness Scheduler是由ARM和Linaro开发的新的linux kernel调度器. 原先CFS调度器是基于policy进行调度,并有不同的吞吐量.例如,有一个新的t ...

  6. Java实现 蓝桥杯 算法提高 欧拉函数(数学)

    试题 算法提高 欧拉函数 问题描述 老师出了一道难题,小酱不会做,请你编个程序帮帮他,奖金一瓶酱油: 从1-n中有多少个数与n互质? |||||╭══╮ ┌═════┐ ╭╯让路║═║酱油专用车║ ╰ ...

  7. Java实现 LeetCode 200 岛屿数量

    200. 岛屿数量 给定一个由 '1'(陆地)和 '0'(水)组成的的二维网格,计算岛屿的数量.一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的.你可以假设网格的四个边均被水包围. ...

  8. Java实现字符串转换成整数

    1 问题描述 输入一个由数字组成的字符串,请把它转换成整数并输出.例如,输入字符串"123",输出整数123. 请写出一个函数实现该功能,不能使用库函数. 2 解决方案 解答本问题 ...

  9. 【asp.net core 系列】4. 更高更强的路由

    0. 前言 在之前我们介绍了请求通过路由寻找到控制器,以及控制器与视图的数据流转.那么,我们回过头来,再看看路由的一些其他用法. 1. 路由属性(Route Attribute) 按照英文的直接翻译, ...

  10. 用vue实现一个简单的时间屏幕

    前言 去年用了一个小的 app,叫做 一个木函,本来想着用来做动画优化就删掉了的,不过看到他有个时间屏幕的小工具,就点进去看了下,觉得挺好玩的,就想着能不能自己实现一下. ps: 闲话不多说,先上例子 ...