av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

Spring @RestController注解組合實現方法解析

瀏覽:41日期:2023-08-31 09:16:07

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

@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Controller@ResponseBodypublic @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)@MyComponentpublic @interface MyController {}

@MyControllerpublic 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.MyControllerfalse

經過本人查閱資料,發現我想要的那個注解組合注解的功能是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
相關文章:
主站蜘蛛池模板: 中文字幕在线观看一区二区三区 | 无遮挡在线观看 | 高清免费av | 国产精品欧美精品 | 国产精品99久久久久久www | 欧美精品999 | 伊人超碰在线 | 精品国产区一区二 | 欧美在线亚洲 | 国产麻豆一区二区三区 | 97精品在线视频 | 美女毛片视频 | 免费三片在线播放 | 欧美黄色三级视频 | 亚洲一区在线看 | 国产精品亚洲一区 | 日韩欧美精品在线 | 免费三级网站 | 国产专区在线播放 | 四虎欧美 | 国产激情视频在线 | 毛片网站视频 | 日本www视频| 国产在线不卡视频 | 色天使在线视频 | 我要看一级黄色片 | 97精品国产| 国产欧美在线 | 国产一区在线播放 | 国产免费一级 | 成人欧美一区二区三区黑人免费 | 中文在线观看免费视频 | 色av吧| 色av综合 | 亚洲天天操| 国产一级一片免费播放放a 国产黄色大片 | 精品国产毛片 | 在线不欧美 | av一区在线 | 日韩高清一区二区 | 日韩一级片视频 |