Play框架加载配置项type OBJECT rather than问题

在使用Play框架时遇到配置错误,尝试通过Play.application().configuration().getString获取配置项,但报错无法注入。错误信息显示配置项play.test.url有类型问题,预期为STRING但实际为OBJECT。问题源于配置文件中play.test.url下存在多个类似play.test.url.testn的子配置,导致解析为对象而非字符串。解决方案是确保配置项名称的唯一性和避免类似父子级的配置结构。

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

前言

在使用play框架,通过

Play.application().configuration().getString

获取配置项信息时,运行报错不能注入。
Caused by: org.springframework.beans.factory.BeanCreationException:Could not autowire field

错误信息

Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [XXX]: Constructor threw exception; nested exception is Configuration error: Configuration error[reference.conf: 242-244: play.test.url has type OBJECT rather than STRING]

原因

检查了配置项的,发现有些配置项存在类似,但少一级的情况,如:

配置文件:

play.test.url=“url”
play.test.url.test1=“1”
play.test.url.test2=“2”
play.test.url.test3=“3”

程序引用:

    protected final String url1 = Play.application().configuration().getString("play.test.url");
    protected final String url2 = Play.application().configuration().getString("play.test.url.test1");
    protected final String url3 = Play.application().configuration().getString("play.test.url.test2");
    protected final String url4 = Play.application().configuration().getString("play.test.url.test3");

系统取"play.test.url" 的值时,连着后面跟的.testn都算进去了,所以成了个对象,相应的就报错:
has type OBJECT rather than STRING

注意

配置项的名称唯一,且不能存在有类似父子级关系的配置名称

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值