解決程式中Error creating bean with name 'XXXXX‘ defined in class path resource [application的異常
最近在專案中犯了一個低階的錯誤,很low的錯誤,首先貼出我自己程式的報錯資訊,資訊如下:
Error creating bean with name 'sqlSessionFactory' defined in class path resource [applicationContext.xml]:
Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException:
Failed to convert property value of type 'java.lang.String' to required type 'javax.sql.DataSource' for property 'dataSource';
nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'javax.sql.DataSource'
for property 'dataSource': no matching editors or conversion strategy found
從上述的錯誤資訊中我們可以知道,報錯的原因就是因為建立名稱為'sqlSessionFactory'的bean時出錯,即建立mybatis的sqlSessionFactory失敗,失敗的原因是無法將你的datasource裡配置的字串轉換成javax.sql.DataSource物件,導致SessionFactory無法完成。
到這裡我們就可以明確的知道datasource配置肯定有誤,因此我們要去檢查我們在applicationContext.xml檔案中配置的資料來源,
開啟applicationContext.xml檔案,我發現我自己寫的配置資料來源的程式碼是這樣的:
<property name="dataSource" value="dataSource">
看,在這裡我居然寫的是value!我居然寫的是value!!我居然寫的是value!!!心態有點崩,
於是就把上面一句中的value改成了ref,如下:
<property name="dataSource" ref="dataSource"/>
這裡解釋一下,ref和value是有區別的,value對應的是給屬性賦值是基本資料型別和string型別的。ref是對應的當前xml檔案中配置過的bean型別。這個概念在我的https://blog.csdn.net/qq_38701478/article/details/82778431這篇部落格中有介紹過,有不明白的可以去看看。
在這裡如果我們用了value進行注入的話,Spring會以字串方式注入,因此會報型別不匹配的異常
好了,希望大家在寫程式的時候細心點,不要犯一些低階的錯誤。
相關文章
- The bean ‘xxx‘, defined in class path resource , ......Bean
- Error creating bean with name 解決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 'tomcatEmbeddedServletContainerFactory'ErrorBeanTomcatServletAI
- Error creating bean with name ‘dataSource‘ErrorBean
- Spring注入bean報錯 Error creating bean with name的網上找不到的解決方案SpringBeanError
- Error creating bean with name ‘dataSource‘: Unsatisfied dependency expressed through fieldErrorBeanExpress
- Caused by: Error: ' ' is not a valid resource name characterError
- No bean named 'cacheManager' availablej 異常解決BeanAI
- 異常 This application has no explicit mapping for /error so yAPPError
- Python 解決 :NameError: name 'reload' is not defined 問題PythonError
- FeignClientSpecification‘ could not be registered. A bean with that name has already been definedclientBean
- Configuration problem: Bean name 'xxxxx' is already used in this element spring例項重複注入BeanSpring
- No bean named 'xxx' is defined錯誤,原因及解決方案Bean
- java.lang.IllegalArgumentException: Could not find class異常解決方式JavaException
- 異常解決——Failed to introspect Class [com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration]AIROSGithub
- mac xcrun: error: active developer path 解決方法MacErrorDeveloper
- HTML中的tag、element、class、name、idHTML
- SAP Cloud Application Programming 裡的@(path) 註解CloudAPP
- Android Studio中的package name和application IDAndroidPackageAPP
- Python(Path().name)Python
- 【JavaDebug(七)】之SpringBoot的Web專案異常:This application has no explicit mapping for /error, so you are seJavaSpring BootWebAPPError
- 執行 locust 的異常 pywintypes.error 如何解決?Error
- jedis異常:Could not get a resource from the pool
- 前端開發中的Error以及異常捕獲前端Error
- Django web框架-----url path name詳解DjangoWeb框架
- Tomcat常見異常及解決方案程式碼例項Tomcat
- Ionic異常及解決
- spring getBean(String) No bean named '' is definedSpringBean
- element-ui中row-class-name的用法UI
- git丟棄本地改動檔案時提示“error: path “file_path“ is unmerged”的解決方案GitError
- WebApplicationContext 中特殊的 bean 型別(一)--- 請求/異常處理WebAPPContextBean型別
- WebApplicationContext 中特殊的 bean 型別(一)— 請求/異常處理WebAPPContextBean型別
- 關於SpringMVC的HttpMediaTypeNotSupportedException異常解決SpringMVCHTTPException
- No Bean named 'Xxx' available 異常報錯BeanAI
- SpringCloud BeanCurrentlyInCreationException 異常解決方案SpringGCCloudBeanException
- [BUG反饋]defined('ADDON_PATH') or define('ADDON_PATH', APP_PATH.'Addon');APP