Spring對C3P0資料庫連接池注入時出現的問題
Spring中使用C3P0資料源需要匯入2個——3個jar包

? mchange-commons-java-0.2.19.jar這是c3p0資料庫連接池的輔助包,如果沒有這個包系統啟動時會報classnotfoundexception,這是更新c3p0-0.9.2版本后分離出來的包,0.9.1的時候還是只是一個包,
如果沒有匯入這個輔助包,配置bean時,不報錯,但是獲取容器實體化bean時會報錯;
xml配置c3p0:
<bean id="datasource" >
<property name="driverClass" value="https://www.cnblogs.com/renxin-Li/archive/2021/06/13/com.mysql.jdbc.Driver"/>
<property name="user" value="https://www.cnblogs.com/renxin-Li/archive/2021/06/13/root"/>
<property name="password" value=""/>
<property name="jdbcUrl" value="https://www.cnblogs.com/renxin-Li/archive/2021/06/13/jdbc:mysql://localhost:3306/study"/>
</bean>
測驗代碼:
@Test
public void testDataSource() throws SQLException {
ConfigurableApplicationContext ac = new ClassPathXmlApplicationContext("ioc3.xml");
DataSource dataSource = ac.getBean("datasource",DataSource.class);
System.out.println(dataSource);
}
報錯資訊:
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'datasource' defined in class path resource [ioc3.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mchange.v2.c3p0.ComboPooledDataSource]: No default constructor found; nested exception is java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirector
//翻譯大概意思就是:創建名為'datasource'的bean時出錯,該bean在類路徑資源[ioc3.xml]中定義:實體化bean失敗,為找到默認建構式
//但是ComboPooledDataSource是有無參構造的,所以可能無法找到準確的錯誤原因
匯入輔助包后正常獲取到DataSource:
結果:
資訊: Initializing c3p0-0.9.5.5 [built 11-December-2019 22:07:46 -0800; debug? true; trace: 10]
com.mchange.v2.c3p0.ComboPooledDataSource[ identityToken -> 1b61iobahze2bpj1a5zcgw|7fc229ab, dataSourceName -> 1b61iobahze2bpj1a5zcgw|7fc229ab ]
Process finished with exit code 0
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/287057.html
標籤:其他
上一篇:python檔案操作
