在Spring Boot

时间:2018-05-12 21:56:17

标签: spring-boot constructor initialization default-constructor configurationproperty

在ConfigurationProperties类中,我有一个这样的Map:

@ConfigurationProperties(prefix = "some")
public class SomeConfigurationProperties {
    private Map<String, SomeClass> map = new HashMap<>();
...
}

我想提到SomeClass所有字段都是 final ,所以我必须实现no-default构造函数,我很好。不是的人是spring初始化程序,因此在开始应用程序之前,我收到了这个错误:

 Caused by: java.lang.NoSuchMethodException: some.package.SomeClass.<init>()

我无法在SomeClass中设置默认构造函数,因为我将获得未初始化的最终字段:

The blank final field somefield may not have been initialized

我应该如何使用无默认构造函数设置bean SomeClass的初始化,但是如果没有XML Spring配置,那么因为我使用的是Spring Boot并且不想搞砸它。

更新: SomeClass示例:

public class SomeClass {
    private final int field1;
    private final String field2;

    // no default constructor

    public SomeClass(field1, field2) {
        this.field1 = field1;
        this.field2 = field2;
    }

    // getters & setters
}

0 个答案:

没有答案