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.

java_geek
  • 17,585
  • 30
  • 91
  • 113
  • 1
    Looks like a @NoArgsConstructor is needed on the Source and Destination – java_geek Aug 07 '20 at 12:48
  • 1
    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 – Nasibulloh Yandashev Aug 07 '20 at 16:07

0 Answers0