3

I have a configuration similar to below

spring:
  source:
    prop1: ['abc', 'def']
    prop2: some-value
  destination:
    prop1: some-other-value

I am trying to read the above configuration with the below Classes

@Configuration
@ConfigurationProperties(prefix = "spring")
@Getter
@Setter
public class ClientConfiguration {
  private Source source;
  private Destination destination;
}

@Getter
@Setter
@AllArgsConstructor
@Configuration
@ConfigurationProperties(prefix = "source")
public class Source {
  private List<String> prop1;
  private String prop2;
}

@AllArgsConstructor
@Getter
@Setter
@Configuration
@ConfigurationProperties(prefix = "destination")
public class Destination {
  private String prop1;
}

However, I am getting an error with this setup.

Could not bind properties to ClientConfiguration (prefix=spring, ignoreInvalidFields=false, ignoreUnknownFields=true, ignoreNestedProperties=false); nested exception is org.springframework.beans.NullValueInNestedPathException: Invalid property 'source' of bean class [ClientConfiguration$$EnhancerBySpringCGLIB$$f1eacf3e]: Could not instantiate property type [Source] to auto-grow nested property path; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [Source]: Is it an abstract class?; nested exception is java.lang.InstantiationException: Source

Please let me know how to parse the nested configuration using nested objects.

2
  • 1
    Looks like a @NoArgsConstructor is needed on the Source and Destination Commented Aug 7, 2020 at 12:48
  • 2
    If you use inner class, you have to create an instance of Object like Source source=new Source(); Or another option. Create static inner class, then you don't need to create an instance of the class Commented Aug 7, 2020 at 16:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.