午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Spring @RestController注解組合實(shí)現(xiàn)方法解析

瀏覽:87日期: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兩個(gè)注解的組合,同時(shí)產(chǎn)生兩個(gè)注解的作用。

本人一開始以為這是Java的特性,Java能夠通過(guò)注解上的注解實(shí)現(xiàn)自動(dòng)組合注解的效果。于是寫了這樣一段代碼

/** * @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 {}

結(jié)果測(cè)試發(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.MyControllerfalse

經(jīng)過(guò)本人查閱資料,發(fā)現(xiàn)我想要的那個(gè)注解組合注解的功能是Spring自己實(shí)現(xiàn)的。。通過(guò)Spring中的AnnotationUtils.findAnnotation(類,注解)方法來(lái)判斷某個(gè)類上是否能找到組合的注解。

比如現(xiàn)在我想知道AnnotatedService這個(gè)類上是否存在@MyComponent注解,畢竟這是我一開始的目的(通過(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本身沒有實(shí)現(xiàn) 通過(guò)標(biāo)記注解 來(lái)組合注解的功能。假如我們自定義注解時(shí)需要可以使用Spring的AnnotationUtils.findAnnotation()的方法幫助我們實(shí)現(xiàn)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 久久久久久成人 | 亚洲一区二区三区免费在线观看 | 国产免费一区二区三区最新6 | 日本中文字幕在线免费观看 | 四虎精品影院 | 午夜xx| 99国产精品一区二区 | 久久久www成人免费毛片 | 一二三区中文字幕 | 日韩成人午夜 | 成年人在线观看视频网站 | 精品国产99久久久久久 | 这里只有精品国产 | 操久久久| 国产区av | 69精品久久久久久 | 成人免费av在线 | 99热国内精品| 欧美精品久久久久久久久46p | 成人av影视 | 尹人在线观看 | 天堂中文资源在线 | 日韩三级av | 中文字幕第一页在线 | 98久久| 欧美日韩精| www.一级片 | 国产精品乱 | 国产一区在线免费观看 | 欧美xxxxxx片免费播放软件 | 91在线视频免费 | 欧美精品成人在线 | 黑人巨大国产9丨视频 | v天堂中文在线 | 国产妞干网 | 久久久999 | 久久精品香蕉 | 毛片网站在线播放 | 在线观看日韩欧美 | 久久精品99国产国产精 | 国产又大又粗又硬 |