码迷,mamicode.com
首页 > 数据库 > 详细

Access denied for user ‘root‘@‘localhost‘

时间:2015-11-16 17:49:58      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:

spring4和hibernate4整合通过占位符读取properties文件时报java.sql.SQLException: Access denied for user ‘root‘@‘localhost‘ (using password: YES)

jdbc.properties文件内容如下:

jdbc.driverClassName=com.mysql.jdbc.Driver 
jdbc.url=jdbc:mysql://localhost:3306/spring 
jdbc.username=root
jdbc.password=****

beans.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"
    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>

不使用占位符时一点问题没有。问题出在哪呢?网上查了好多,都是让改密码,后来无意看到这一问题,链接如下:

http://stackoverflow.com/questions/9319387/mysql-access-denied-for-user-using-password-yes

在该链接中的答案里这样提到: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).

我重新检查了下jdbc.properties文件,发现username的value即root后有一个空格,可能是不小心打上的,而spring读取时未进行trim,故出现这一异常。

解决方案:检查properties里的所有value,看后面是否有空格。有则去掉

Access denied for user ‘root‘@‘localhost‘

标签:

原文地址:http://my.oschina.net/u/2430057/blog/530936

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!