错误信息不做展示了, 三个小时后定位到错误位置为applicationContext.xml中加载外部jdbc.properties文件出错。
<!-- 加载外部的properties配置文件 -->
<context:property-placeholder location="classpath:config/jdbc.properties"/>
<!-- 配置数据库连接池(c3p0) -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 基本信息 -->
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="user" value="${username}"></property> <!--username慎用-->
<property name="password" value="${password}"></property>
jdbc.properties 文件:
jdbcUrl = jdbc:mysql://localhost:3306/hello
driverClass = com.mysql.jdbc.Driver
username = root
password = root
解决方案一: 直接写死在里面(不推荐):
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hello"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
解决方案二: 修改jdbc.properties文件,避免username这种属性名的使用:
mysql.jdbcUrl = jdbc:mysql://localhost:3306/hello
mysql.driverClass = com.mysql.jdbc.Driver
mysql.username = root
mysql.password = root
既然方案二可以用,猜测是properties中四个属性名有部分有问题,一个小时的测试,锁定属性名username。
解决方案三: 添加属性 system-properties-mode=“FALLBACK”
参考: https://blog.csdn.net/fdk2zhang/article/details/83039416
<context:property-placeholder system-properties-mode=“FALLBACK” location=“db.properties”/>
整理一个大神的回帖,供参考:
(回帖来自该文章回复)https://blog.csdn.net/crosstime230/article/details/52637271
llsses: 少了一条属性system-properties-mode=“FALLBACK”, 系统默认为system-properties-mode=“ENVIRONMENT” 意思就是从系统环境中去读取,把电脑名当做mysql的用户名,修改之后,运行成功。 可能和spring底层org.springframework.beans.factory.config.PropertyPlaceholderConfigurer读取properties实现有关,默认从系统中读取真实用户名(username) 。