gpt4 book ai didi

java - SpringBoot 2 元素未绑定(bind)

转载 作者:行者123 更新时间:2023-11-29 08:25:55 24 4
gpt4 key购买 nike

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

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

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

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类

@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;
}

当我开始运行应用程序时,我收到错误信息:

**************************
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.

这个问题困扰了我很久,希望有人能给我一些建议;我用的是springboot 2.0谢谢

最佳答案

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

该解决方案要么修复您的类以供 @ConfigurationProperties 使用,要么删除 @ConfigurationProperties 注释。

@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 可能更容易,因为您一开始并没有真正使用它们。

关于java - SpringBoot 2 元素未绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53167040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com