Bean initialization with no-default constructor and final fields in Spring Boot

747 views Asked by At

In ConfigurationProperties class I have a Map like this:

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

And I want in mentioned SomeClass all fields to be final, so I had to implement no-default constructor and I am fine with that. The one who is not, is spring initializer, so before starting application, I got this error:

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

I can't set default constructor in SomeClass cause I will get final fields uninitialized:

The blank final field somefield may not have been initialized

How I should setup initialization of bean SomeClass with no-default constructor, but if it can be without XML Spring configuration, because I am using Spring Boot and don't want to mess up with that.

UPDATED: Example of 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

There are 0 answers