IllegalArgumentException: Could not resolve placeholder ‘from‘ in value “${from}“

本文分析了Spring Cloud Config配置中心启动后,客户端调用时出现的配置变量未解析问题及解决办法,包括检查URI配置、Git仓库认证和安全验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



项目场景:

学习Spring Cloud Config的时候出现的问题, 因此记录下问题希望帮助到有缘人




问题描述:

启动config-server 配置中心后成功, 启动调用的微服务 config-client的时候提示

Could not resolve placeholder 'from' in value "${from}"

//异常信息如下
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.configController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'from' in value "${from}"
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]




原因分析:

 第一个可能原因:

如果config-client客户端的启动日志中uri地址是http://localhost:8888, 那么问题可能是bootstrap.properties中的配置的config.uri配置不正确 引起

如下图:启动日志中打印的信息表示config server的地址信息

         可以查看下ConfigServicePropertySourceLocator 对象处理,locate方法中从通过ConfigClientProperties对象获取配置中心的config server 地址,然后通过日志打印,查看ConfigClientProperties对象会发现, 默认uri地址是"http://localhost:8888", 参考源码如下

public class ConfigServicePropertySourceLocator implements PropertySourceLocator{

@Override
	@Retryable(interceptor = "configServerRetryInterceptor")
	public org.springframework.core.env.PropertySource<?> locate(
			org.springframework.core.env.Environment environment) {

        //从ConfigClientProperties对象中获取配置信息 properties
		ConfigClientProperties properties = this.defaultProperties.override(environment);
		CompositePropertySource composite = new CompositePropertySource("configService");
		RestTemplate restTemplate = this.restTemplate == null ? getSecureRestTemplate(properties)
				: this.restTemplate;
		Exception error = null;
		String errorBody = null;

        //这里打印了配置中的 Uri 
        //查看ConfigClientProperties对象会发现uri的默认值为"http://localhost:8888"
		logger.info("Fetching config from server at: " + properties.getRawUri());

        // 省略后面的代码
    }
}

        所以, 如果启动日志打印地址为 http://localhost:8888 且你的配置中心地址不是它,那么表示读取bootstrap.properties中的配置的spring.cloud.config.uri出现了问题,需要检查是否漏配置或者KEY配置字符错误,我第一次配置的时候spring 错误写成了spiring ,不仔细看还真不容易发现。 正确配置spring.cloud.config.uri 后, 启动config-client 的日志中的信息参考下图

 第二个可能原因:

 config-server中配置的Git地址错误或者Git用户密码缺失或者不正确,可以通过CURL工具验证请求 config-server是否成功, 如果不成功需要检查git配置,最常见的是GIT仓库用户名密码没有写对

#GIT仓库用户名
spring.cloud.config.server.git.username=
#GIT仓库密码
spring.cloud.config.server.git.password=

 第三个可能原因:

config-client端启动日志获取到对的server地址, 但是第二行爆出401错误

INFO 7496 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://127.0.0.1:8006/
WARN 7496 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: 401 null
INFO 7496 --- [           main] c.configclient.ConfigClientApplication   : No active profile set, falling back to default profiles: default
INFO 7496 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded

检查config-server是不是添加了security安全验证,如果添加了验证, 那么客户端config-client需要在配置文件中bootstrap.properties 增加验证的用户名密码,如果用户名或者密码错误,或者用户名密码配置在application.xml文件中的时候会出现401错误

spring.cloud.config.username=配置服务器上security配置的用户名
spring.cloud.config.password=配置服务器上security配置的用户密码

  上一篇:Spring Cloud Config快速入门

### 关于 `xss.excludes` 占位符无法解析的问题 在遇到 `java.lang.IllegalArgumentException: Could not resolve placeholder 'xss.excludes' in value "${xss.excludes}"` 错误时,表明Spring框架未能找到名为 `xss.excludes` 的属性值[^1]。 #### 可能的原因分析 - **配置文件缺失或路径不正确**:如果指定的配置文件不存在或者其路径设置有误,则会导致占位符无法被替换。例如,可能存在拼写错误或者是环境变量未正确定义的情况[^5]。 - **Profile 配置不当**:当项目中有多个激活 profile 时,需确认当前使用的profile名称与实际存在的配置文件相匹配。任何细微差异(如多余的连字符)都可能导致加载失败。 - **重复定义 PropertyPlaceholderConfigurer Bean**:根据描述,在同一个应用上下文中不应同时存在超过一个 `PropertyPlaceholderConfigurer` 实例;否则会阻止后续同类型的bean初始化过程,从而影响其他占位符的正常解析[^4]。 #### 解决方法建议 ##### 方法一:检查并修正配置文件及其引用 确保所有外部资源文件的位置准确无误,并且它们确实包含了所需的键值对。对于Maven构建工具而言,还需核对POM文件内的profiles部分是否正确指定了目标资源配置项。 ##### 方法二:统一管理属性源 考虑将分散于不同位置的properties文件合并成单一入口点来简化维护工作量的同时减少潜在冲突风险。这可以通过调整application.properties 或 application.yml 文件结构实现。 ##### 方法三:审查 Spring 上下文配置类 如果有自定义的 Java Config 类负责注册 EnvironmentPostProcessor 或者 PropertySourcesPlaceholderConfigurer bean ,则应仔细排查这些组件逻辑是否存在漏洞[^3]。 ```java @Configuration public class AppConfig { @Bean public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() { return new PropertySourcesPlaceholderConfigurer(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

=PNZ=BeijingL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值