Spring @RestController注解組合實(shí)現(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兩個(gè)注解的組合,同時(shí)產(chǎn)生兩個(gè)注解的作用。
本人一開(kāi)始以為這是Java的特性,Java能夠通過(guò)注解上的注解實(shí)現(xiàn)自動(dòng)組合注解的效果。于是寫(xiě)了這樣一段代碼
/** * @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é)果測(cè)試發(fā)現(xiàn)翻車(chē)
/** * @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)過(guò)本人查閱資料,發(fā)現(xiàn)我想要的那個(gè)注解組合注解的功能是Spring自己實(shí)現(xiàn)的。。通過(guò)Spring中的AnnotationUtils.findAnnotation(類(lèi),注解)方法來(lái)判斷某個(gè)類(lèi)上是否能找到組合的注解。
比如現(xiàn)在我想知道AnnotatedService這個(gè)類(lèi)上是否存在@MyComponent注解,畢竟這是我一開(kāi)始的目的(通過(guò)組合減少注解),我可以調(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()
假如傳入的注解是一個(gè)不存在的值,則會(huì)返回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)); } }
控制臺(tái)打印:
null
總結(jié):Java本身沒(méi)有實(shí)現(xiàn) 通過(guò)標(biāo)記注解 來(lái)組合注解的功能。假如我們自定義注解時(shí)需要可以使用Spring的AnnotationUtils.findAnnotation()的方法幫助我們實(shí)現(xiàn)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring注解@RestControllerAdvice原理解析
- springboot @Controller和@RestController的區(qū)別及應(yīng)用詳解
- SpringBoot http請(qǐng)求注解@RestController原理解析
- SpringBoot的@RestControllerAdvice作用詳解
- SpringBoot常用注解@RestControllerAdvice詳解
- Spring中@RestControllerAdvice注解的使用詳解
- Spring中的@RestController注解詳細(xì)解析
- springboot中@RestController注解實(shí)現(xiàn)
- Spring中@RestController注解的使用實(shí)現(xiàn)
相關(guān)文章
idea使用mybatis插件mapper中的方法爆紅的解決方案
這篇文章主要介紹了idea使用mybatis插件mapper中的方法爆紅的解決方案,文中給出了詳細(xì)的原因分析和解決方案,對(duì)大家解決問(wèn)題有一定的幫助,需要的朋友可以參考下2024-07-07使用@Transactional 設(shè)置嵌套事務(wù)不回滾
這篇文章主要介紹了使用@Transactional 設(shè)置嵌套事務(wù)不回滾問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07總結(jié)Junit4,Junit5,Jupiter之間的聯(lián)系
Jupiter和Junit5之間有什么聯(lián)系?Jupiter提供了哪些新的測(cè)試方法?如何用IDEA和Jupiter生成可讀性更好的測(cè)試報(bào)告?文中有非常詳細(xì)的說(shuō)明,需要的朋友可以參考下2021-06-06Map如何根據(jù)key指定條件進(jìn)行過(guò)濾篩選
這篇文章主要介紹了Map如何根據(jù)key指定條件進(jìn)行過(guò)濾篩選問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09java程序代碼與文本對(duì)比實(shí)用工具簡(jiǎn)介
可以對(duì)兩段文本進(jìn)行對(duì)比,檢測(cè)/比較兩個(gè)文本有什么不同的差異,以便修改,常用于程序代碼,就是不需要人工查看,尤其是大文件,有幾百上千行的代碼,這時(shí)候就建議使用比較工具了,不用浪費(fèi)過(guò)多時(shí)間去尋找2021-09-09JDK1.7中HashMap的死循環(huán)問(wèn)題及解決方案
這篇文章主要為大家介紹了JDK1.7中HashMap的死循環(huán)問(wèn)題及解決方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10K8S(Docker)如何優(yōu)雅的關(guān)閉SpringBoot微服務(wù)
這篇文章主要介紹了K8S(Docker)如何優(yōu)雅的關(guān)閉SpringBoot微服務(wù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-01-01基于Mybatis plus 自動(dòng)代碼生成器的實(shí)現(xiàn)代碼
本文通過(guò)實(shí)例代碼給大家介紹了基于Mybatis-plus 自動(dòng)代碼生成器的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-05-05