在一个Spring项目中,新建了一个外部属性文件db.properties,在xml文件中利用${}来引用db.properties文件里面的属性。

beans-properties.xml:

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 导入属性文件 -->
<context:property-placeholder location="classpath:db.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 使用外部化属性文件的属性 -->
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driverclass}"></property>
<property name="jdbcUrl" value="${jdbcurl}"></property>
</bean> </beans>

db.properties:

 user=root;
password=;
driverclass=com.mysql.jdbc.Driver;
jdbcurl=jdbc:mysql:///tt

Main.java:

 package com.tt.spring.beans.properties;

 import java.sql.SQLException;

 import javax.sql.DataSource;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) throws SQLException{ ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-properties.xml"); DataSource dataSource = (DataSource) ctx.getBean("dataSource"); System.out.println(dataSource.getConnection()); }
}

lib目录如下:

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

原因分析:

如果直接在beans-properties.xml文件里对user,password,driverClass,jdbcUrl进行赋值,而不引用db.properties文件里的属性,运行正常。这说明服务器能加载到数据库驱动。

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

Spring警告: Could not load driverClass com.mysql.jdbc.Driver(待解决)的更多相关文章

  1. Could not load driverClass com.mysql.jdbc.Driver错误

    在整合spring和mybatis的时候,在spring配置文件中已经加载了db.properties并配置了c3p0数据源 但在写了一个测试类测试是否取到了数据库的连接时,报错:Could not ...

  2. 搭建jfinal框架时报 Could not load driverClass com.mysql.jdbc.Driver

    搭建jfinal框架时报 Could not load driverClass com.mysql.jdbc.Driver 没有加载MySQL的驱动,你有没有把mysql的驱动包放到你项目的WEB-I ...

  3. ssm框架找不到mysql驱动类WARN DriverManagerDataSource:107 - Could not load driverClass com.mysql.jdbc.Driver

    找了很久错误,检查了配置文件,和spring配置数据源,都没有发现问题,最后上网查询了下,发现是由于配置文件后面有空格. 去除掉配置文件后面的空格就可以正常运行了.

  4. powerdesigner连接MySQL数据库时出现Non SQL Error : Could not load class com.mysql.jdbc.Driver

    Non SQL Error : Could not load class com.mysql.jdbc.Driver 这是因为powerdesigner 无法找到驱动所产生的 解决办法是:配置系统的c ...

  5. PowerDesigner 逆向工程Non SQL Error : Could not load class com.mysql.jdbc.Driver

    建立与数据库的连接. 在菜单条上,有一个Database的选择项: 选择connect…后弹出设置对话框: 在Data source里选择第三个单选按钮,即Connection profile:后,点 ...

  6. cloudera-scm-server启动出现Error creating bean with name 'entityManagerFactoryBean'与HHH010003: JDBC Driver class not found: com.mysql.jdbc.Driver错误解决办法(图文详解)

    不多说,直接上干货! 问题详情 -- ::, INFO main:com.cloudera.server.cmf.Main: Starting SCM Server. JVM Args: [-Dlog ...

  7. 错误:“Cannot load JDBC driver class 'com.mysql.jdbc.Driver”的解决方法

    “Cannot load JDBC driver class 'com.mysql.jdbc.Driver ” 表示没有JDBC连接MySql的驱动包,因此需要手动添加驱动包到WEB-INF目录下的l ...

  8. IntelliJ IDEA web项目进行数据库连接时出现java.lang.ClassNotFoundException: com.mysql.jdbc.Driver错误解决办法

    首先看报错信息: 意思是找不到类:  com.mysql.jdbc.Driver.也就是说tomcat找不到MySQL数据库连接要用的jar包! 出现这种错误的原因是: 项目中没有导入这个jar包, ...

  9. Exception 02 : java.lang.ClassNotFoundException: Could not load requested class : com.mysql.jdbc.Driver

    异常名称 java.lang.ClassNotFoundException: Could not load requested class : com.mysql.jdbc.Driver 异常详细信息 ...

随机推荐

  1. Learning Phrase Representations using RNN Encoder–Decoder for Statistical Machine Translation

    1.主要完成的任务是能够将英文转译为法文,使用了一个encoder-decoder模型,在encoder的RNN模型中是将序列转化为一个向量.在decoder中是将向量转化为输出序列,使用encode ...

  2. 20145314郑凯杰 《Java程序设计》第5周学习总结

    20145314郑凯杰 <Java程序设计>第5周学习总结 教材学习内容总结 托管的代码: 电脑上的代码: try与catch 简单来说,try与catch是两个块,java的程序会把正常 ...

  3. 20145327 《Java程序设计》第十周学习总结

    20145327 <Java程序设计>第十周学习总结 教材学习内容总结 网络编程就是运行在不同计算机中两个程序之间的数据交换. 网络中的每个设备都会有一个唯一的数字标识,这个就是IP地址. ...

  4. Jquery15 插件

    学习要点: 1.插件概述 2.验证插件 3.自动完成插件 4.自定义插件 插件(Plugin)也成为 jQuery 扩展(Extension),是一种遵循一定规范的应用程序接口编写出来的程序.目前 j ...

  5. 解读:计数器Counter

    Counters: 44 File System Counters FILE: Number of bytes read=655771325 FILE: Number of bytes written ...

  6. JSON 中JsonConfig的使用(转)

    我们通常对一个Json串和Java对象进行互转时,经常会有选择性的过滤掉一些属性值,而json-lib包中的JsonConfig为我们提供了这种 功能,具体实现方法有以下几种.(1)建立JsonCon ...

  7. wechat4j获取用户昵称乱码修复

    项目对接微信公众号平台时,微信的官方给出的建议是使用wechat4j.官方建议的,自然心里踏实,但实际用起来时发现wechat4j埋有很多雷,最让人心烦意乱的就是中文乱码问题. 之前写过一篇为JAXB ...

  8. web platform installer

    下载链接 https://www.microsoft.com/web/downloads/platform.aspx 默认的安装路径 C:\Program Files\Microsoft\Web Pl ...

  9. UVA 1213 Sum of Different Primes(经典dp)

    题意:选择k(k<15)个唯一质数,求出和为n(n<1121)的可能数 题解:预处理dp,dp[k][n]表示使用k个素数拼成n的总方案数 就是三重枚举,枚举k,枚举n,枚举小于n的素数 ...

  10. SpringBoot项目结构介绍

    一项目结构介绍 springboot框架本身对项目结构并没有特别的要求,但是按照最佳的项目结构可以帮助我们减少可能遇到的错误问题.结构如下: (1)应用主类SpringbootApplication应 ...