首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SpringBoot 2元素未绑定

SpringBoot 2元素未绑定
EN

Stack Overflow用户
提问于 2018-11-06 14:55:08
回答 2查看 30.6K关注 0票数 3

我的Spring Boot应用程序中有一个application.yml文件,它不愿意运行。

根据日志,元素simulator.geo.b12、simulator.geo.b13、simulator.geo.b21、simulator.geo.c6、simulator.geo.host未绑定的原因。然而,这个属性是在application.yml中设置的,编译器甚至会返回它的值。

如果有人能帮我解决这个问题,我将不胜感激。

代码语言:javascript
复制
simulator:
    geo:
        host: http://localhost:8080/
        b12: http://localhost:8080/geo/b12
        b13: http://localhost:8080/geo/b13
        b21: http://localhost:8080/geo/b21
        c6: http://localhost:8080/geo/c6

和java类

代码语言:javascript
复制
@Getter
@Configuration
@ConfigurationProperties(prefix = "simulator",ignoreUnknownFields = false)
public class VendorSimulatorProperties {

    @Value("${simulator.geo.host:http://localhost:8080/}")
    private String initUrl;

    @Value("${simulator.geo.b12}")
    private String geoB12Url;

    @Value("${simulator.geo.b13}")
    private String geoB13Url;

    @Value("${simulator.geo.b21}")
    private String geoB21Url;

    @Value("${simulator.geo.c6}")
    private String geoC6Url;
}

当我开始运行应用程序时,我得到了错误消息:

代码语言:javascript
复制
**************************
APPLICATION FAILED TO START
***************************

Description:

Binding to target [Bindable@1c140c7c type = com.mathartsys.dlc.thirdparty.vendor.config.VendorSimulatorProperties$$EnhancerBySpringCGLIB$$eb0a550b, value = 'provided', annotations = array<Annotation>[@org.springframework.boot.context.properties.ConfigurationProperties(prefix=simulator, value=simulator, ignoreUnknownFields=false, ignoreInvalidFields=false)]] failed:

    Property: simulator.geo.b12
    Value: http://localhost:8080/geo/b12
    Origin: class path resource [config/application-dev.yml]:204:14
    Reason: The elements [simulator.geo.b12,simulator.geo.b13,simulator.geo.b21,simulator.geo.c6,simulator.geo.host] were left unbound.
    Property: simulator.geo.b13
    Value: http://localhost:8080/geo/b13
    Origin: class path resource [config/application-dev.yml]:205:14
    Reason: The elements [simulator.geo.b12,simulator.geo.b13,simulator.geo.b21,simulator.geo.c6,simulator.geo.host] were left unbound.
    Property: simulator.geo.b21
    Value: http://localhost:8080/geo/b21
    Origin: class path resource [config/application-dev.yml]:206:14
    Reason: The elements [simulator.geo.b12,simulator.geo.b13,simulator.geo.b21,simulator.geo.c6,simulator.geo.host] were left unbound.
    Property: simulator.geo.c6
    Value: http://localhost:8080/geo/c6
    Origin: class path resource [config/application-dev.yml]:207:13
    Reason: The elements [simulator.geo.b12,simulator.geo.b13,simulator.geo.b21,simulator.geo.c6,simulator.geo.host] were left unbound.
    Property: simulator.geo.host
    Value: http://localhost:8080/
    Origin: class path resource [config/application-dev.yml]:203:15
    Reason: The elements [simulator.geo.b12,simulator.geo.b13,simulator.geo.b21,simulator.geo.c6,simulator.geo.host] were left unbound.

这个问题困扰了我很长时间,希望有人能给我一些建议,我用的是Springboot2.0谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-11-06 15:05:24

问题是您使用@ConfigurationProperties的方式是错误的。您可以使用@ConfigurationProperties@Value中的一个,但不能同时使用两者。

该解决方案要么将您的类修复为可用于@ConfigurationProperties,要么删除@ConfigurationProperties注释。

代码语言:javascript
复制
@Getter
@Setter
@Component
@ConfigurationProperties(prefix = "simulator.geo",ignoreUnknownFields = false)
public class VendorSimulatorProperties {

    private String host = "http://localhost:8080/";
    private String b12;
    private String b13;
    private String b21;
    private String c6;

}

您需要修复prefix,它应该是simulator.geo,并且您的属性应该以文件中的键命名。在getter旁边还需要setter。但是,这也需要更改配置的其余部分,以使用新生成的getter。

对你来说,删除@ConfigurationProperties可能更容易,因为你一开始并没有真正使用它们。

票数 5
EN

Stack Overflow用户

发布于 2021-06-09 10:38:12

我得到了同样的问题,但在我的例子中,这是因为我添加了@AllArgsConstructor,但没有添加@NoArgConstructor。因此,Spring无法创建配置对象(使用默认构造函数),因此它只是跳过处理配置并打印出警告。

例如:

代码语言:javascript
复制
@Getter
@Setter
@AllArgsConstructor
@NoArgConstructor // need to add this if you have @AllArgsConstructor
public class ServerConfiguration {
    private String host;
    private int port;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53167040

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档