Spring注入bean報錯 Error creating bean with name的網上找不到的解決方案
今天在研究BaseService,在進行單元測試時,發現tomcat一直報錯,錯誤程式碼如下:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘這是我的service測試類’: Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [這是我的service類] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
從資訊中,可以看到是找不到需要注入的bean!我的程式是通過註解@Resource注入的。
嘗試了網上的N種方法,無非以下幾種:
- 配置檔案沒有開啟註解掃描bean
- spring配置檔案裡beans標籤沒配置default-autowire=”byname”
- 各種配置問題…
- 呼叫死迴圈
但是問題依然存在,我就開始去檢查需要注入的類有沒有問題,結果發現了。
@Transactional@Service("roomTypeService")public abstract class RoomTypeServiceImpl extends BaseServiceImpl<RoomType, Integer> implements RoomTypeService { // other fileds and methods......}
- 1
- 2
- 3
- 4
- 5
不知道小夥伴們看出問題沒有,這裡的Service註解和name都沒有問題,問題出在類寫成了抽象類!!!抽象類啊!!!我把它去掉了就能正常跑起來了!!
雖然我不知道為什麼當時寫成了抽象類,但還是想分享出來讓跟我犯錯誤的同學儘快脫離debug的痛苦。
說回問題的本質,為什麼寫成了抽象類就無法正常注入呢?這也正是java基礎知識:注入相當於例項化一個物件,而在java的語法裡面,抽象類(和介面)是無法例項化(new)的,所以就注入失敗了!
好久沒寫過原創的文章了,這樣不行,說好的堅持呢= =
再分享一下我老師大神的人工智慧教程吧。零基礎!通俗易懂!風趣幽默!希望你也加入到我們人工智慧的隊伍中來!http://www.captainbed.net
相關文章
- Error creating bean with name 解決ErrorBean
- Error creating bean with name 'tomcatEmbeddedServletContainerFactory'ErrorBeanTomcatServletAI
- Error creating bean with name ‘dataSource‘ErrorBean
- 異常解決——Spring Cloud FeignClient: BeanCreationException: Error creating bean with name XXXServiceSpringCloudclientBeanExceptionError
- 關於報錯Error creating bean with name 'xxxxx': Invocation of init method....ErrorBean
- Error creating bean with name ‘dataSource‘: Unsatisfied dependency expressed through fieldErrorBeanExpress
- 解決程式中Error creating bean with name 'XXXXX‘ defined in class path resource [application的異常ErrorBeanAPP
- 關於Spring的bean注入SpringBean
- spring配置檔案中配置了,但找不到Bean , 啟動不報錯,執行報錯No bean namedSpringBean
- Spring注入Bean的幾種方式SpringBean
- spring註解開發(一)Bean注入SpringBean
- spring注入bean的幾種策略模式SpringBean模式
- Spring中bean的四種注入方式SpringBean
- spring中bean.xml的http://www.springframework.org/schema/bean報錯SpringBeanXMLHTTPFramework
- Configuration problem: Bean name 'xxxxx' is already used in this element spring例項重複注入BeanSpring
- Spring IoC bean 的建立(上)SpringBean
- No bean named 'xxx' is defined錯誤,原因及解決方案Bean
- Spring裝配Bean(七) Spring的執行時注入SpringBean
- Spring裝配Bean(五)profile註解和解決自動注入的歧義性SpringBean
- 省掉bean自定義spring mvc註解注入json值BeanSpringMVCJSON
- Spring Bean 詳解SpringBean
- 扒一扒Bean注入到Spring的那些姿勢BeanSpring
- Spring裝配Bean(六)Bean的作用域SpringBean
- idea maven搭建spring報錯:No bean named 'xxx' availableIdeaMavenSpringBeanAI
- spring中如何向一個單例bean中注入非單例beanSpring單例Bean
- SpringBoot整合Junit單元測試找不到bean報錯:expected at least 1 bean which qualifies as autowire candidateSpring BootBeanAST
- Java報錯:Missing ServletWebServerFactory bean,如何解決JavaServletWebServerBean
- 對於@Bean注入的新理解Bean
- Spring bean到底是如何建立的?(上)SpringBean
- [Spring]BeanSpringBean
- Spring:錯誤No bean named 'xxxxxxx' availableSpringBeanAI
- 《Spring》(六)---- Bean的scopeSpringBean
- spring注入在有常量的情況下使用@AllArgsConstructor報錯Consider defining a bean of type ‘java.lang.String‘ in your cSpringStructIDEBeanJava
- Spring中給靜態欄位(field)注入beanSpringBean
- 24--Spring解決bean之間的迴圈依賴SpringBean
- Spring的Bean生命週期中@PostConstruct註解SpringBeanStruct
- Spring 原始碼分析之 bean 依賴注入原理(注入屬性)Spring原始碼Bean依賴注入
- 【Spring】BeanFactory 解析 bean 詳解SpringBean