解決程式中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會以字串方式注入,因此會報型別不匹配的異常
好了,希望大家在寫程式的時候細心點,不要犯一些低階的錯誤。
相關文章
- Error creating bean with name 解決ErrorBean
- Error creating bean with name ‘dataSource‘ErrorBean
- 異常解決 java.io.FileNotFoundException: class path resource [spring/springmvc.xml]JavaExceptionSpringMVCXML
- 異常解決——Spring Cloud FeignClient: BeanCreationException: Error creating bean with name XXXServiceSpringCloudclientBeanExceptionError
- Spring注入bean報錯 Error creating bean with name的網上找不到的解決方案SpringBeanError
- 關於報錯Error creating bean with name 'xxxxx': Invocation of init method....ErrorBean
- Error creating bean with name 'tomcatEmbeddedServletContainerFactory'ErrorBeanTomcatServletAI
- Error creating bean with name 'memcachedClient'...java.lang.OutOfMemoryErrorErrorBeanclientJava
- Error creating bean with name ‘dataSource‘: Unsatisfied dependency expressed through fieldErrorBeanExpress
- error: '[class name]' does not name a typeError
- 異常解決:non-compatible bean definition of same name and class【com.xxx.xxx.XXX】Bean
- No bean named 'cacheManager' availablej 異常解決BeanAI
- Caused by: Error: ' ' is not a valid resource name characterError
- _resource_manager_always_on=FALSE引起排程異常的解決案例False
- Python 解決 :NameError: name 'reload' is not defined 問題PythonError
- FeignClientSpecification‘ could not be registered. A bean with that name has already been definedclientBean
- 解決Eclipse/STS 中出現Resource is out of sync with the file system 的異常Eclipse
- 異常 This application has no explicit mapping for /error so yAPPError
- java.lang.IllegalArgumentException: Could not find class異常解決方式JavaException
- View.setTag(key,object)異常:The key must be an application-specific resource id.ViewObjectAPP
- 報錯No bean named ' is defined的原因和解決方案Bean
- No bean named 'xxx' is defined錯誤,原因及解決方案Bean
- Eclipse/STS 異常解決:A cycle was detected in the build path of project XXXEclipseUIProject
- mac xcrun: error: active developer path 解決方法MacErrorDeveloper
- Server Application Error報錯資訊的解決方案ServerAPPError
- javaWeb常見異常的解決方法JavaWeb
- 前端開發中的Error以及異常捕獲前端Error
- jedis異常:Could not get a resource from the pool
- native程式異常crash定位解決方案
- 執行 locust 的異常 pywintypes.error 如何解決?Error
- Ionic異常及解決
- Django web框架-----url path name詳解DjangoWeb框架
- 解決WebLogic中Too many open files異常Web
- Python(Path().name)Python
- element-ui中row-class-name的用法UI
- Tomcat常見異常及解決方案程式碼例項Tomcat
- 解決Mac java.net Local host name unknown error的方法MacJavaError
- flume file channel 異常解決