[英]Spring NoClassDefFoundError org.springframework.beans.FatalBeanException when adding <context:component-scan …> in the config
[英]Adding a specific components to the Spring Context when creating a facade component
與其讓我的一些類@Autowire
很多小的@Component
,我想創建一個單獨的@Component
來收集並轉發到較小的@Component
。
編碼本身並不太難,但是當涉及到測試時,它變得很麻煩,因為現在我必須將它添加到@ContextConfiguration(classes)
。 有沒有辦法讓我不必單獨管理它?
使用@ComponentScan
添加我可能不想要的整個包,因為它會觸發更多的@MockBean
被創建。
在我看來,您正在尋找的是@Configuration + @Bean
的組合。 您可以通過@Bean
初始化完整的鏈,而不是使用 @Component 標記所有嵌套的子組件,並使用簡單的 pojo-s 而不是 spring 管理的 bean。 當然,最終的結果將是一個 spring 管理的 bean,但是在初始化過程中,您可以在任何地方使用 spring 或不使用 spring 對其進行初始化。
關於 ComponetScan。 通常,如果我只想加載某個部分,我會使用我想要的 conponenbt 掃描的顯式模式創建注釋,然后我在我想要的域中使用它。
例如,假設我們有兩組 Mapper 類,一組是關於訂單的,另一組是關於庫存的。 我可以創建注釋@EnableOrderMappers
和注釋@EnableInventoryMappers
,並在其上添加適當的組件@EnableInventoryMappers
注釋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.