绑定ConfigurationProperties(Spring Boot)时出现NullPointerException

时间:2018-03-13 18:25:31

标签: java spring spring-boot nullpointerexception

我正在努力解决一些代码,我不知道为什么...... 这是我的代码:

@SpringBootApplication
@EnableConfigurationProperties(WorkflowRootProperties.class)
@ComponentScan
@ActiveProfiles("test")
public class TestApplication {
    public static void main(String[] args) {    
        SpringApplication.run(TestApplication.class, args);
    }
}

我的属性bean

@ConfigurationProperties("workflow")
public class WorkflowRootProperties {
    private List<Datacenter> datacenter = new ArrayList<>();

    public List<Datacenter> getDatacenter() {
        return datacenter;
    }

    public void setDatacenter(List<Datacenter> datacenter) {
        this.datacenter = datacenter;
    }
}

哪个引用另一个bean

public class EnvProperties {
    public static class Datacenter {
        private List<Env> env = new ArrayList<>();

        public List<Env> getEnv() {
            return env;
        }

        public void setEnv(List<Env> env) {
            this.env = env;
        }
    }

    public static class Env {
        private List<Instance> instance = new ArrayList<>();

        public List<Instance> getInstance() {
            return instance;
        }

        public void setInstance(List<Instance> instance) {
            this.instance = instance;
        }
    }

    public static class Instance {
        private String port;

        public String getPort() {
            return port;
        }

        public void setPort(String port) {
            this.port= port;
        }
    }
}

最后,我的属性是

workflow.datacenter[1].env[1].instance[1].port=8080
workflow.datacenter[2].env[1].instance[1].port=8082
workflow.datacenter[1].env[1].instance[2].port=8080

我的错误是在初始化,我得到了一个我无法理解的NPE。

  

java.lang.IllegalStateException:无法加载ApplicationContext ...

     

引起:   org.springframework.boot.context.properties.bind.BindException:失败   将“workflow.datacenter”下的属性绑定到   java.util.List at   org.springframework.boot.context.properties.bind.Binder.handleBindError(Binder.java:250)     在   org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:226)     在   org.springframework.boot.context.properties.bind.Binder.lambda $ bindBean $ 4(Binder.java:331)     在   org.springframework.boot.context.properties.bind.JavaBeanBinder.bind(JavaBeanBinder.java:72)     在   org.springframework.boot.context.properties.bind.JavaBeanBinder.bind(JavaBeanBinder.java:61)     在   org.springframework.boot.context.properties.bind.JavaBeanBinder.bind(JavaBeanBinder.java:53)...

     

引起:java.lang.NullPointerException:null at   java.util.TreeMap.compare(TreeMap.java:1294)at   java.util.TreeMap.put(TreeMap.java:538)at   java.util.TreeSet.add(TreeSet.java:255)at   java.util.stream.ReduceOps $ 3ReducingSink.accept(ReduceOps.java:169)     在   java.util.LinkedList中的$ LLSpliterator.forEachRemaining(LinkedList.java:1235)     在   java.util.stream.ReferencePipeline $ Head.forEach(ReferencePipeline.java:580)     在   java.util.stream.ReferencePipeline $ 7 $ 1.accept(ReferencePipeline.java:270)     在java.util.Iterator.forEachRemaining(Iterator.java:116)at   java.util.Spliterators $ IteratorSpliterator.forEachRemaining(Spliterators.java:1801)     在   java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)     在   java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)     在   java.util.stream.ReduceOps $ ReduceOp.evaluateSequential(ReduceOps.java:708)     在   java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)     在   java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)     在   org.springframework.boot.context.properties.bind.IndexedElementsBinder.assertNoUnboundChildren(IndexedElementsBinder.java:137)...

你能帮我吗?

1 个答案:

答案 0 :(得分:1)

如果从0开始编制索引,似乎可以正常工作:

workflow.datacenter[0].env[0].instance[0].port=8080
workflow.datacenter[1].env[0].instance[0].port=8082
workflow.datacenter[0].env[0].instance[1].port=8080