You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring 5.2.1.RELEASE中getProperty获取username返回User问题排查

Spring中environment.getProperty("username")返回"User"而非配置值的问题排查

问题现象

  • 调用dataSource.setUsername(environment.getProperty("username"));时,返回值为"User",而非配置文件中设置的username=postgres
  • 触发HTTP 500数据库连接错误,具体异常信息:
    org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is org.postgresql.util.PSQLException: FATAL: password authentication failed for user "User"
    
  • 经IntelliJ IDEA调试确认,environment.getProperty("username")确实返回"User"
  • 将属性名修改为db-username并同步调整getProperty参数后,数据库连接恢复正常,但希望继续使用"username"作为属性名

环境说明

  • 处于Spring框架学习阶段,当前项目与课程示例项目的唯一差异为使用最新版本PostgreSQL依赖,尝试修改依赖版本未解决问题

疑问

  • 该问题的根本原因是什么?
  • 如何排查PropertyResolver接口getProperty()方法的属性来源冲突问题?

内容的提问来源于stack exchange,提问作者it4545454545

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 01:12:09