Spring容器使用中出现 Access denied for user ‘Administrator‘@‘localhost‘ (using password: YES)

问题描述

在Spring容器中通过配置 <context:property-placeholder location=“classpath:/jdbc.properties”/> 调用properties数据源配置文件时出现 Access denied for user ‘Administrator’@‘localhost’ (using password: YES)
错误!!!!

报错

运行环境

Properties配置:
properties配置文件

Spring基本配置(完成注入):
xml配置文件]

原因分析

在系统中也有个username属性,这时系统变量覆盖了Properties中的值,这时取得username的值为系统的用户名Administrator,密码为properties中的password去查询数据库,此时用户名名和密码并不匹配就会报错。在Spring完成注入时是用"${…}" 方式获取值完成注入的。而通过这种表达式也能直接获取到JVM系统属性…

解决方案

方案一:将properties文件中的username换成user或其他就字符串就可以成功获取连接访问数据库。建议:username时敏感词汇,为了安全起见还是尽量不要使用username。

在这里插入图片描述
在这里插入图片描述

方案二:在Spring配置文件中修改成:<context:property-placeholder
location=“classpath:/jdbc.properties” system-properties-mode=“FALLBACK
/ NEVER”/>
      添加一个system-properties-mode属性
      该属性有三个值:FALLBACK  — 默认值,不存在时覆盖
              NEVER    — 不覆盖
              OVERRIDE  — 覆盖
在这里插入图片描述

效果

通过测试,可以获取到数据库连接

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值