在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ spring掃描組件時會掃描該組件的父類及接口么?有沒有明確的文檔說明?

spring掃描組件時會掃描該組件的父類及接口么?有沒有明確的文檔說明?

如題。
如題。
如題。

回答
編輯回答
葬愛

這里有非常好的例子展示spring掃描的范圍設定
符合以下條件的會被掃描并創(chuàng)建bean

  • 類有@Component注解
  • 且包名包含在@ComponentScan注解范圍內(nèi)
  • 或包含在@ComponentScan.Filter范圍內(nèi)

如果子類符合條件,但父類沒有包含在掃描范圍內(nèi), 子類會創(chuàng)建,但父類不會創(chuàng)建, 因為不符合instanceof條件,即不能說父類子類
如果父類被創(chuàng)建, 子類有@Component注解,但不在指定Filter范圍內(nèi),也會創(chuàng)建,因為符合instanceof條件,因為子類一定是父類

@Component注解沒有繼承關系(@Inherited), 所以想被創(chuàng)建必須首先要有這個注解才行.
或創(chuàng)建你自己的可繼承的注解過的接口.

如:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Component 
@Inherited
public @interface BusinessService {
}
2018年4月3日 14:15
編輯回答
吃藕丑

另外 如果是spring boot的其實可以自己測試 看下結果的 http://ip:port/beans

2017年7月22日 16:34
編輯回答
淺淺

自問自答,提出問題第二天就已通過跟蹤源碼找到答案,說說結果給后人以經(jīng)驗.
為什么會有這樣的問題,來自于springcloud feign 遠程調(diào)用場景,controller繼承了api service,在service的方法上定義RequestMapping,controller中不定義,spring同樣能掃描到,完成請求地址與方法的映射,這個過程蹊蹺的地方在于,接口中定義的注解并不會被類繼承,其唯一可以識別到的方式就是 掃描父類,但是一般似乎從來沒這么用過,在關于spring源碼解析的文章當中也很少有提及掃描組件會去查找父類中的注解,帶著問題去跟蹤源碼,發(fā)現(xiàn),在掃描組件過程中反復調(diào)用了 這么一個類AnnotatedElementUtils,其中的方法searchWithGetSemantics,語義化搜索,什么叫語義化搜索,該類的注釋已明確給出了答案.
` * <p>{@code AnnotatedElementUtils} defines the public API for Spring's

  • meta-annotation programming model with support for annotation attribute
  • overrides. If you do not need support for annotation attribute
  • overrides, consider using {@link AnnotationUtils} instead. * <p>Find semantics are much more exhaustive, providing
  • get semantics plus support for the following:

*

  • <ul>
  • <li>Searching on interfaces, if the annotated element is a class
  • <li>Searching on superclasses, if the annotated element is a class
  • <li>Resolving bridged methods, if the annotated element is a method
  • <li>Searching on methods in interfaces, if the annotated element is a method
  • <li>Searching on methods in superclasses, if the annotated element is a method
  • </ul>`
2018年6月7日 18:23