spring4和hibernate4整合經過佔位符讀取properties文件時報java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)java
jdbc.properties文件內容以下:mysql
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/spring jdbc.username=root jdbc.password=****
beans.xml文件內容以下:spring
<?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" xmlns:aop="http://www.springframework.org/schema/aop" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:property-placeholder location="classpath:jdbc.properties" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <bean id="userDao" class="dao.impl.UserDaoImpl"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="userInterceptor" class="aop.UserInterceptor"></bean> <bean id="userService" class="service.UserService"> <property name="userDao" ref="userDao"></property> </bean> </beans>
不使用佔位符時一點問題沒有。問題出在哪呢?網上查了好多,都是讓改密碼,後來無心看到這一問題,連接以下:sql
http://stackoverflow.com/questions/9319387/mysql-access-denied-for-user-using-password-yes app
在該連接中的答案裏這樣提到:Someone else checked out the file, and when committing it, some trailing whitespace characters got appended to the password. It seems that these aren't trimmed when spring reads the property file (reasonable, I guess).url
我從新檢查了下jdbc.properties文件,發現username的value即root後有一個空格,多是不當心打上的,而spring讀取時未進行trim,故出現這一異常。spa
解決方案:檢查properties裏的全部value,看後面是否有空格。有則去掉hibernate