gpt4 book ai didi

java - 无法使用Spring Boot通过java bean中的application.properties绑定(bind)属性

转载 作者:行者123 更新时间:2023-12-02 11:29:18 26 4
gpt4 key购买 nike

我的 java 类如下所示

@ConfigurationProperties(prefix = "resilience.component.io")
public class IO16BrickletProperties {

private List<IOBricklet> bricklets = new ArrayList<IOBricklet>();

public List<IOBricklet> getBricklets() {
return bricklets;
}

public void setBricklets(List<IOBricklet> bricklets) {
this.bricklets = bricklets;
}
}

public class IOBricklet extends Bricklet {

private List<Configuration> configuration = new ArrayList<Configuration>();

public List<Configuration> getConfiguration() {
return configuration;
}

public void setConfiguration(List<Configuration> configuration) {
this.configuration = configuration;
}

class Configuration {
private char port;
private char pin;
private String serviceName;

getters and setters...
toString()...
}
}

我想通过application.properties绑定(bind)其中的值。我这样做的方式是

resilience.component.io.bricklets[0].uid=DBv
resilience.component.io.bricklets[0].period=100
resilience.component.io.bricklets[0].configuration[0].port=a
resilience.component.io.bricklets[0].configuration[0].pin=1
resilience.component.io.bricklets[0].configuration[0].serviceName=symbology
resilience.component.io.bricklets[0].configuration[1].port=b
resilience.component.io.bricklets[0].configuration[1].pin=1
resilience.component.io.bricklets[0].configuration[1].serviceName=elkservice

我得到的错误是

2018-03-20 16:17:35.103  WARN 1344 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'IO16BrickletProperties': Could not bind properties to 'IO16BrickletProperties': prefix=resilience.component.io, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'resilience.component.io.bricklets[0].configuration' to java.util.List<netc.resilience.model.IOBricklet$Configuration>
2018-03-20 16:17:35.212 WARN 1344 --- [ restartedMain] o.s.b.f.support.DisposableBeanAdapter : Invocation of destroy method 'close' failed on bean with name 'eurekaRegistration': org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$RefreshableEurekaClientConfiguration': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)

有人能弄清楚我到底为什么会收到此错误吗?我提到external configuration spring 的但无法解决此错误。在属性文件中配置的正确方法是什么。

最佳答案

需要将内部类设为静态。请参阅spring docs .

所以更新代码将如下所示

public class IOBricklet extends Bricklet {

private List<Configuration> configuration = new ArrayList<Configuration>();

public List<Configuration> getConfiguration() {
return configuration;
}

public void setConfiguration(List<Configuration> configuration) {
this.configuration = configuration;
}

static class Configuration {
private char port;
private char pin;
private String serviceName;

getters and setters...
toString()...
}
}

关于java - 无法使用Spring Boot通过java bean中的application.properties绑定(bind)属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49388422/

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