Could not resolve placeholder ‘coupon.user.name‘ in value “${coupon.user.name}“

news/2024/12/29 16:50:26/

目录

场景:

问题描述:

原因分析:

解决方案:

方案一:

方案二:


场景:

我在学着怎么将nacos的配置拉取下来,但是很不幸的是,读取不到nacos上的配置,只能读取到本地的properties。

问题描述:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.smsCouponController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'coupon.user.name' in value "${coupon.user.name}"at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:405) ~[spring-beans-5.3.23.jar:5.3.23]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431) ~[spring-beans-5.3.23.jar:5.3.23]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619) ~[spring-beans-5.3.23.jar:5.3.23]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.23.jar:5.3.23]at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$1(AbstractBeanFactory.java:374) ~[spring-beans-5.3.23.jar:5.3.23]at org.springframework.cloud.context.scope.GenericScope$BeanLifecycleWrapper.getBean(GenericScope.java:376) ~[spring-cloud-context-3.1.5.jar:3.1.5]at org.springframework.cloud.context.scope.GenericScope.get(GenericScope.java:179) ~[spring-cloud-context-3.1.5.jar:3.1.5]at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:371) ~[spring-beans-5.3.23.jar:5.3.23]at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.23.jar:5.3.23]at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154) ~[spring-context-5.3.23.jar:5.3.23]at org.springframework.cloud.context.scope.refresh.RefreshScope.eagerlyInitialize(RefreshScope.java:126) ~[spring-cloud-context-3.1.5.jar:3.1.5]at org.springframework.cloud.context.scope.refresh.RefreshScope.start(RefreshScope.java:118) ~[spring-cloud-context-3.1.5.jar:3.1.5]at org.springframework.cloud.context.scope.refresh.RefreshScope.onApplicationEvent(RefreshScope.java:113) ~[spring-cloud-context-3.1.5.jar:3.1.5]at org.springframework.cloud.context.scope.refresh.RefreshScope.onApplicationEvent(RefreshScope.java:68) ~[spring-cloud-context-3.1.5.jar:3.1.5]at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176) ~[spring-context-5.3.23.jar:5.3.23]at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169) ~[spring-context-5.3.23.jar:5.3.23]at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143) ~[spring-context-5.3.23.jar:5.3.23]at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:421) ~[spring-context-5.3.23.jar:5.3.23]at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:378) ~[spring-context-5.3.23.jar:5.3.23]at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:938) ~[spring-context-5.3.23.jar:5.3.23]at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) ~[spring-context-5.3.23.jar:5.3.23]at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.13.jar:2.6.13]at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:745) [spring-boot-2.6.13.jar:2.6.13]at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:420) [spring-boot-2.6.13.jar:2.6.13]at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-2.6.13.jar:2.6.13]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1317) [spring-boot-2.6.13.jar:2.6.13]at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) [spring-boot-2.6.13.jar:2.6.13]at com.guli.GulimailCouponApplication.main(GulimailCouponApplication.java:14) [classes/:na]
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'coupon.user.name' in value "${coupon.user.name}"at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:180) ~[spring-core-5.3.23.jar:5.3.23]at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-5.3.23.jar:5.3.23]at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239) ~[spring-core-5.3.23.jar:5.3.23]at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210) ~[spring-core-5.3.23.jar:5.3.23]at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:191) ~[spring-context-5.3.23.jar:5.3.23]at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:936) ~[spring-beans-5.3.23.jar:5.3.23]at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1332) ~[spring-beans-5.3.23.jar:5.3.23]at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311) ~[spring-beans-5.3.23.jar:5.3.23]at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:656) ~[spring-beans-5.3.23.jar:5.3.23]at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:639) ~[spring-beans-5.3.23.jar:5.3.23]at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.23.jar:5.3.23]at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.23.jar:5.3.23]... 27 common frames omitted

原因分析:

无法解析值“${coupon.user.name}”中的占位符“coupon.user.name”,也就是找不到coupon.user.name,在nacos配置中心读取不到配置,启动类就一直报错。

解决方案:

方案一:

bootstrap是属于springcloud的,如果你的项目只是一个springboot项目的话,那么是只会去读取application.yml/application.properties文件的,所以要读取bootstrap需要有cloud相关的一些配置才能够去正常读取它。

如果springboot版本低于2.4.0版本,则需要在pom里添加依赖:

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-context</artifactId>
</dependency>

如果springboot版本高于2.4.0版本,则需要在pom里添加依赖:

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

方案二:

仔细检查一下是不是你的配置里面的

  application:name

中的name写错了

还有nacos配置文件中的Data ID,Data ID的格式(注意不要忘记加后缀):

${prefix}-${spring.profile.active}.${file-extension}

${prefix}:
默认为所属工程配置spring.application.name的值(这就是为什么平时我们直接用服务名就可以),也可以用spring.cloud.nacos.config.prefix来配置.

${spring.profile.active}:
spring.profile.active即为当前环境对应的profile.注意:当spring.profile.active为空的时候,对应的连接符 - 也将不存在,dataId的拼接格式变成prefix.{file-extension}

${file-extension}:
为配置内容的数据格式,可以通过配置项 spring.cloud.nacos.config.file-extension 来配置。目前只支持 properties 和 yaml 类型。
 

参考文章:nacos的dataId规则_nacos配置dataid_白帆瀚宇的博客-CSDN博客


http://www.ppmy.cn/news/74674.html

相关文章

带拉绳的按钮

看看效果&#xff1a; 再上代吗&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>Title</title><script src"https://unpkg.co/gsap3/dist/gsap.min.js"></scr…

PyCharm 点击运行,没有执行所有的py文件内容

&#xff08;JDD_KK原创&#xff09; 基于 Pycharm&#xff0c;python3 问题&#xff1a;编写了一个测试工程&#xff0c;创建了多个.py文件。 多个py文件为&#xff1a; data_test/test_datademo.py interface_test/test_interface1.py test_test/test_test.py web_test/test…

ESP32设备驱动-SHTC1数字湿度传感器驱动

SHTC1数字湿度传感器驱动 文章目录 SHTC1数字湿度传感器驱动1、SHTC1介绍2、硬件准备3、软件准备4、驱动实现1、SHTC1介绍 SHTC1 是一款数字湿度传感器,专为大容量消费电子应用而设计。这款湿度传感器经过严格设计,旨在克服传统的尺寸、功耗和性价比限制,以满足消费电子市场…

Java流程控制(一)

⭐ 控制语句⭐ 条件判断结构(选择结构)⭐ switch 语句 做任何事情事情都要遵循一定的原则&#xff0c;毕竟不以规矩&#xff0c;不成方圆&#xff0c;例如&#xff0c;到图书馆去借书&#xff0c;就必须要有借书证&#xff0c;并且借书证不能过期&#xff0c;这两个条件缺一不可…

信息安全-应用安全-SCA技术:SBOM应用实践初探

目录 软件供应链安全治理 供应链安全概述 风险治理重点 何为SBOM SBOM的元素 SBOM的格式 SBOM使用场景 如何使用SBOM 选择SBOM工具 SBOM与风险情报关联 围绕SBOM建立管理流程 关于SBOM的思考 现代软件都是组装的而非纯自研。随着开源组件在数字化应用中的使用比例越…

MySQL高级SQL语句

创建两个表用于演示 location store_info use kgc; create table location (Region char(20),Store_Name char(20)); insert into location values(East,Boston); insert into location values(East,New York); insert into location values(West,Los Angeles); insert into lo…

AI脚本插件开发-自定图层名及专色-插件制作源码-illustrator插件开发

文章目录 1.illustrator1.1.app.activeDocument1.2.selection2.模块分析3.源码工程4.功能描述5.作者答疑本文主要分析一款插件的源码,自定图层名及专色,代码一般较长,读者耐心阅读,对于学习插件开发具有不小的帮助。先介绍了一下基础资料,如有不懂的地方,就去这些资料里去…

Day5 模块(单元)测试——单元测试、自顶向上测试、自下向上测试

Day5 模块(单元)测试——单元测试、自顶向上测试、自下向上测试 文章目录 Day5 模块(单元)测试——单元测试、自顶向上测试、自下向上测试什么是单元测试?单元测试结构化程序时代单元测试与使用单元测试的误解单元测试过程成本效率不高结论单元测试的优点测试用例白盒覆盖…