Java SpringBoot 2 元素未绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53167040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SpringBoot 2 the elements were left unbound
提问by runbird
i have a file application.yml with my Spring Boot application which is not willing to run.
我有一个文件 application.yml 和我的 Spring Boot 应用程序,它不愿意运行。
According to logs the reason of The elements [simulator.geo.b12,simulator.geo.b13,simulator.geo.b21,simulator.geo.c6,simulator.geo.host] were left unbound. This property however is set in application.yml and compiler is even returning it's value.
根据日志,元素 [simulator.geo.b12,simulator.geo.b13,simulator.geo.b21,simulator.geo.c6,simulator.geo.host] 未绑定的原因。然而,这个属性是在 application.yml 中设置的,编译器甚至会返回它的值。
I'll really appreciate if someone could lend me a hand with that issue.
如果有人能帮我解决这个问题,我将不胜感激。
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
and java class
和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;
}
when i start to run application, i got the error msg :
当我开始运行应用程序时,我收到错误消息:
**************************
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.
this problem had confuse me long time, i hope someone can give me some advice; i used springboot 2.0 thanks
这个问题困扰了我很久,希望有人能给我一些建议;我使用了 springboot 2.0 谢谢
采纳答案by M. Deinum
The problem is that you are using the @ConfigurationProperties
in a wrong way. You use either @ConfigurationProperties
or @Value
but not both.
问题是您@ConfigurationProperties
以错误的方式使用了。您使用其中之一@ConfigurationProperties
或@Value
但不使用两者。
The solution either fix your class to be usable for @ConfigurationProperties
or remove the @ConfigurationProperties
annotation.
该解决方案要么修复您的类以用于@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;
}
You need to fix the prefix
it should be simulator.geo
and your properties should be named after the keys in your file. You will also require setter next to the getter. However this will also need to change the rest of your configuration, to use the newly generated getters.
您需要修复prefix
它应该是的,simulator.geo
并且您的属性应该以文件中的键命名。您还需要 getter 旁边的 setter。但是,这还需要更改其余配置,以使用新生成的 getter。
For you it is probably easier to remove @ConfigurationProperties
as you weren't really using them in the first place.
对您来说,删除可能更容易,@ConfigurationProperties
因为您一开始并没有真正使用它们。