Spring警告: Could not load driverClass com.mysql.jdbc.Driver(待解決)


在一個Spring項目中,新建了一個外部屬性文件db.properties,在xml文件中利用${}來引用db.properties文件里面的屬性。

beans-properties.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 6                         http://www.springframework.org/schema/beans/spring-beans.xsd
 7                         http://www.springframework.org/schema/context 
 8                         http://www.springframework.org/schema/context/spring-context-4.0.xsd">
 9     
10     <!-- 導入屬性文件 -->
11     <context:property-placeholder location="classpath:db.properties"/>
12    
13     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
14           <!-- 使用外部化屬性文件的屬性 -->
15           <property name="user" value="${user}"></property>        
16           <property name="password" value="${password}"></property>
17           <property name="driverClass" value="${driverclass}"></property>
18           <property name="jdbcUrl" value="${jdbcurl}"></property>
19     </bean>
20 
21 </beans>

db.properties:

1 user=root;
2 password=1230;
3 driverclass=com.mysql.jdbc.Driver;
4 jdbcurl=jdbc:mysql:///tt

Main.java:

 1 package com.tt.spring.beans.properties;
 2 
 3 import java.sql.SQLException;
 4 
 5 import javax.sql.DataSource;
 6 
 7 import org.springframework.context.ApplicationContext;
 8 import org.springframework.context.support.ClassPathXmlApplicationContext;
 9 
10 
11 public class Main {
12 
13     public static void main(String[] args) throws SQLException{ 
14         
15         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-properties.xml");
16         
17         DataSource dataSource = (DataSource) ctx.getBean("dataSource");
18         
19         System.out.println(dataSource.getConnection());
20     
21     }
22 }

lib目錄如下:

 

運行Main.java代碼,控制台打印信息如下:

原因分析:

如果直接在beans-properties.xml文件里對user,password,driverClass,jdbcUrl進行賦值,而不引用db.properties文件里的屬性,運行正常。這說明服務器能加載到數據庫驅動。

 

所以問題出在什么地方呢?

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM