Spring @RestController注解組合實現(xiàn)方法解析
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兩個注解的組合,同時產(chǎn)生兩個注解的作用。
本人一開始以為這是Java的特性,Java能夠通過注解上的注解實現(xiàn)自動組合注解的效果。于是寫了這樣一段代碼
/** * @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 { }
結(jié)果測試發(fā)現(xiàn)翻車
/** * @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); } } }
打印結(jié)果如下:
interface com.example.demo.anno.MyController
false
經(jīng)過本人查閱資料,發(fā)現(xiàn)我想要的那個注解組合注解的功能是Spring自己實現(xiàn)的。。通過Spring中的AnnotationUtils.findAnnotation(類,注解)方法來判斷某個類上是否能找到組合的注解。
比如現(xiàn)在我想知道AnnotatedService這個類上是否存在@MyComponent注解,畢竟這是我一開始的目的(通過組合減少注解),我可以調(diào)用一下代碼
/** * @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)); } }
控制臺打?。?/p>
null
總結(jié):Java本身沒有實現(xiàn) 通過標(biāo)記注解 來組合注解的功能。假如我們自定義注解時需要可以使用Spring的AnnotationUtils.findAnnotation()的方法幫助我們實現(xiàn)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IDEA進(jìn)程已結(jié)束,退出代碼-1073741819 (0xC0000005)的bug
這篇文章主要介紹了IDEA進(jìn)程已結(jié)束,退出代碼-1073741819 (0xC0000005)的bug,本文通過實例代碼圖文的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04詳解SpringCloud Gateway之過濾器GatewayFilter
這篇文章主要介紹了詳解SpringCloud Gateway之過濾器GatewayFilter,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10Java刷題之最小k個數(shù)的思路及具體實現(xiàn)
這篇文章主要介紹了Java刷題之最小k個數(shù)的思路及具體實現(xiàn),最小K個數(shù)是一個經(jīng)典的top-K問題,可以通過整體排序、建立小根堆或大根堆的方式解決,排序方式時間復(fù)雜度較高,適合數(shù)據(jù)量小的場景,小根堆適合k較小的情況,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-10-10Java如何使用itext向PDF插入數(shù)據(jù)和圖片
最近項目中使用到Java實現(xiàn)寫入PDF文件,這篇文章主要給大家介紹了關(guān)于Java如何使用itext向PDF插入數(shù)據(jù)和圖片的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01Spring的循環(huán)依賴、三級緩存解決方案源碼詳細(xì)解析
這篇文章主要介紹了Spring的循環(huán)依賴、三級緩存解決方案源碼詳細(xì)解析,在Spring中,由于IOC的控制反轉(zhuǎn),創(chuàng)建對象不再是簡單的new出來,而是交給Spring去創(chuàng)建,會經(jīng)歷一系列Bean的生命周期才創(chuàng)建出相應(yīng)的對象,需要的朋友可以參考下2024-01-01