Access denied for user 'root'@'localhost'

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

相关文章
相关标签/搜索