Spring Boot @ConfigurationProperties для вложенной структуры - PullRequest
2 голосов
/ 07 августа 2020

У меня конфигурация, аналогичная приведенной ниже

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

Я пытаюсь прочитать приведенную выше конфигурацию с указанными ниже классами

@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;
}

Однако при этой настройке я получаю сообщение об ошибке .

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

Пожалуйста, дайте мне знать, как анализировать вложенную конфигурацию с использованием вложенных объектов.

...