无法使用springboot通过java bean中的application.properties绑定属性

时间:2018-03-20 15:38:18

标签: java spring spring-boot

我的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绑定其中的值。我这样做的方式是

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的春天但无法解决这个错误。在属性文件中配置的正确方法是什么。

1 个答案:

答案 0 :(得分:0)

需要将内部类设为静态。请参阅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()...
    }
}