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文件里的属性,运行正常。这说明服务器能加载到数据库驱动。

所以问题出在什么地方呢?

原文地址:https://www.cnblogs.com/TTTTT/p/6402145.html