记录:Could not resolve placeholder ‘user.userName‘ in value “${user.userName}“...【亲测有效】
👨🎓作者:bug菌
💌公众号:猿圈奇妙屋
🚫特别声明:原创不易,转载请附上原文出处链接和本文声明,谢谢配合。
🙏版权声明:文章里可能部分文字或者图片来源于互联网或者百度百科,如有侵权请联系bug菌处理。
一、前言🔥
环境:springboot2.3.1.REALSE + mysql5.6 + jdk1.8
二、报错排查🔥
这是一则典型报错。不知道有的小伙伴有没有一眼看出来,问题出在哪里?其实你看报错信息,特别是我如下圈起来的,已经非常明显了。
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'user': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'user.userName' in value "${user.userName}"
三、报错分析🔥
发生这种事其实很常见,因为项目不是一个人开发全程开发,多人线上协同,很多时候代码冲突会很常有,于是及其修改同一个配置文件,这回我刚更新远程代码,结果启动就直接报错。
如下是完整报错截图:
事故缘由很简单,那就是因为application.yaml的active没有更变,还是指定的dev配置,但是dev没有及时同步test环境的配置,所以才造成获取${user.userName} bean找不到而启动报错啦。
一般遇到这种报错,第一排查你所获取的参数是否存在于你所active指定的配置文件里,第二就是排查你获取的参数名是否与你所active指定的配置文件所定义的参数名一致。你只要排查这两步,八九不离十,就是上述这两问题造成的。
我是当前指定的是application-test.yaml配置,所以就只需要检查这个文件即可,你们依据你们的项目设定情况而定。
四、解决方案🔥
想要解决这一问题,其实是非常简单的。如下我可以提供两种有效解决方式,仅供参考。方式二虽是解决该启动不报错,但并不是非常优雅的。
1️⃣方式一
配置文件中,加上对应报错的那几个全局变量。比如我如下报错就是指没有默认值。配置文件这一块由于切换active 没有同步,test环境上没有这一块变量。于是才报错。
在我指定的配置文件application-test.yaml 中添加该参数。
user:
userName: 张三
sex: 男
age: 18
如上即可解决该报错问题。启动正常!
2️⃣方式二
在一般配置的时候,都会给定参数默认值。虽然也能有效避免你配置文件不存在变量时报错的事故发生。
@Value("${user.userName:张三}")
private String userName;
@Value("${user.sex:男}")
private String sex;
@Value("${user.age:18}")
private Integer age;
实际设置截图:仅供参考
总结:其实是两种方案结合起来才是最真的写法。以防万一配置忘了加或者active切换没有及时同步而导致报错比采用读取默认配置的方式代价要大,所以一般在配置全局变量时,都是会给定变量默认值,但一定要给定有意义的变量默认值,好在第一时间能提醒被排查到是默认变量在启用效果。
... ...
好啦,以上就是解决该报错的所有内容啦。如果对你有所帮助,还请不要忘记给bug菌[三连支持]哟。如果想获得更多的学习资源或者想和更多的技术爱好者一起交流,可以关注我的公众号『猿圈奇妙屋』,后台回复关键词领取学习资料、大厂面经、面试模板等海量资源,就等你来拿。
五、文末🔥
如果你还想要学习更多,小伙伴们大可关注bug菌专门为你们创建的专栏《bug调优》,都是我一手打下的江山,持续更新中,希望能帮助到更多小伙伴们。
我是bug菌,一名想走👣出大山改变命运的程序猿。接下来的路还很长,都等待着我们去突破、去挑战。来吧,小伙伴们,我们一起加油!未来皆可期,fighting!
最后送大家两句我很喜欢的话,与诸君共勉!
☘️做你想做的人,没有时间限制,只要愿意,什么时候都可以start。
🍀你能从现在开始改变,也可以一成不变,这件事,没有规矩可言,你可以活出最精彩的自己。
💌如果文章对您有所帮助,就请留下您的赞吧!(#^.^#);
💝如果喜欢bug菌分享的文章,就请给bug菌点个关注吧!(๑′ᴗ‵๑)づ╭❤~;
💗如果对文章有任何疑问,还请文末留言或者加群吧【QQ交流群:708072830】;
💞鉴于个人经验有限,所有观点及技术研点,如有异议,请直接回复参与讨论(请勿发表攻击言论,谢谢);
💕版权声明:原创不易,转载请附上原文出处链接和本文声明,版权所有,盗版必究!!!谢谢。
相关文章
- G面经Prepare: Search word delete sequence in dictionary
- [Nagios] Error: Template 'timman' specified in contact definition could not be not found (c
- 创建couchbase的bucket 提示RAM不够用 all the ram in the cluster is already allocated to existing bucekets,delete some buckets,,
- When to use Include in EF? Not needed in projection?
- How to check the Shutdown and Startup Log in Windows 11/10
- Elements in iteration expect to have 'v-bind:key' directives
- 关于微信小程序报错:Some selectors are not allowed in component wxss, including tag name selectors...
- C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,带状对角方程(Band-Diagonal)的求解算法与源程序
- MySQL 中 You can't specify target table '表名' for update in FROM clause错误解决办法
- 【Oracle报错】ORA-12514, TNS:listener does not currently know of service requested in conn
- volume create: k8s-volume: failed: Host 172.31.182.142 is not in 'Peer in Cluster' state
- And one thing may differ I found is that, it only compatible with python3,so when your default pip installed in python is 2.7 not python3 you need use "python3 -m pip install chefboost" instead
- NOT IN、NOT EXISTS的相关子查询改用LEFT JOIN--sql2000性能优化
- uniapp的微信小程序错误警告(uview项目)Some selectors are not allowed in component wxss, including tag name selectors, ID selectors, and attribute selectors。
- 杀手SQL- 一条关于 'Not in' SQL 的优化案例
- Linux C/C++ ------ “” and <> in the use of head include file(Pending Verification)
- Yii2 : Active Record add Not In condition
- 【LeetCode】84. Largest Rectangle in Histogram
- The bundle does not contain an app icon for iPhone / iPod Touch of exactly '120x120' pixels, in .pn
- uwsgi启动Django项目时:unable to load app 0 (mountpoint='') (callable not found or import error) *** no app loaded. going in full dynamic mode ***
- SqlServer where in 、where like参数化查询问题(转载)
- SSDT – Error SQL70001 This statement is not recognized in this context-摘自网络
- How to Test Controller Concerns in Rails 4
- [LeetCode] 1080. Insufficient Nodes in Root to Leaf Paths 根到叶路径上的不足节点
- @Query Annotation in Spring Data JPA--转
- Monitor and diagnose performance in Java SE 6--转载