[英]Testing MongoDB repository: Error creating bean with name: No qualifying bean of type
[英]Spring Framework Error creating bean with name and No qualifying bean of type
我開始使用 Spring Framework 並且遇到了一些問題:在我更改了@Service
、 @Controller
、 @Entity
和@Repository
類的包結構后,我的項目停止了工作。 在更改之前我的項目運行正常,現在我不知道為什么它不會啟動。
包結構類似於:
com.sandbox.config
com.sandbox.service
com.sandbox.service.impl
com.sandbox.controllers
com.sandbox.dao
com.sandbox.dao.impl
com.sandbox.entities
我改為:
com.sandbox.config
com.sandbox.business.service
com.sandbox.business.service.impl
com.sandbox.frontend.controllers
com.sandbox.persistency.dao
com.sandbox.persistency.dao.impl
com.sandbox.persistency.entities
我的配置類是:
SpringJdbcConfig:
package com.sandbox.config;
@Configuration
@ComponentScan(basePackages = { "com.sandbox.*" })
public class SpringJdbcConfig {...}
網絡配置:
package com.sandbox.config;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.sandbox.*" })
public class WebConfig implements WebMvcConfigurer {...}
有誰知道我做錯了什么? 我在 eclipse 輸出控制台上收到這些錯誤:
Error creating bean with name 'staffServiceImpl': Unsatisfied dependency expressed through field 'staffDao'.
No qualifying bean of type 'com.sandbox.persistency.dao.StaffDao' available: expected at least 1 bean which qualifies as autowire candidate
。對於我的項目的每個類,我都會收到相同的錯誤。
如下更改ComponentScan
並重試:
@ComponentScan({ "com.sandbox.business.service","com.sandbox.persistency.dao" })
經過數小時的搜索和測試,我找到了解決問題的方法。 如果我更改包結構添加一個新的包級別,如果@ComponentScan
被多次使用, Spring 會復制 Bean 類。 如果您將所有包保持在同一級別,則不會發生這種情況。 就我而言,我在SpringJdbcConfig
和WebConfig
@ComponentScan
中使用@ComponentScan
。
我是這樣設置的:
SpringJdbcConfig:
@ComponentScan(basePackages = { "com.sandbox.*" })
public class SpringJdbcConfig {...}
網絡配置:
@ComponentScan(basePackages = { "com.sandbox.*" })
public class WebConfig implements WebMvcConfigurer {...}
我把它改成:
SpringJdbcConfig:
@ComponentScan({ "com.sandbox.config", "com.sandbox.persistency.*" })
public class SpringJdbcConfig {...}
網絡配置:
@ComponentScan({ "com.sandbox.config", "com.sandbox.business.*", "com.sandbox.persistency.*", "com.sandbox.frontend.*" })
public class WebConfig implements WebMvcConfigurer {...}
我希望它有幫助。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.