在Spring容器中经过配置 <context:property-placeholder location="classpath:/jdbc.properties"/> 调用properties数据源配置文件时出现 Access denied for user 'Administrator'@'localhost' (using password: YES) 错误!!!!mysql
Properties配置(C3p0数据源):sql
dirver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/mydb1
username=root
password=admin
Spring基本配置(完成注入):数据库
<context:property-placeholder location="classpath:/jdbc.properties"/> <bean id="datasouce" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${dirver}"/> <property name="jdbcUrl" value="${jdbcUrl}"/> <property name="user" value="${username}"/> <property name="password" value="${password}"/> </bean>
---- 在获取数据源中的链接时出现上述的错误。安全
错误缘由:spa
在系统中也有个username属性,这时系统变量覆盖了Properties中的值,这时取得username的值为系统的用户名Administrator,密码为properties中的password去查询数据库,此时用户名名和密码并不匹配就会报错。在Spring完成注入时是用 "${..}" 方式获取值完成注入的。而经过这种表达式也能直接获取到JVM系统属性..........code
解决方案:blog
方案一:将properties文件中的username换成user或其余就字符串就能够成功获取链接访问数据库。建议:username时敏感词汇,为了安全起见仍是尽可能不要使用username。字符串
方案二:在Spring配置文件中修改为:<context:property-placeholder location="classpath:/jdbc.properties" system-properties-mode="FALLBACK / NEVER"/> io
添加一个system-properties-mode属性class
该属性有三个值:FALLBACK --- 默认值,不存在时覆盖
NEVER --- 不覆盖
OVERRIDE --- 覆盖