Список вложенных объектов конфигурации Spring не работает - PullRequest
0 голосов
/ 22 октября 2018

Я пытаюсь настроить весеннюю загрузку, чтобы настроить мое приложение, используя этот файл:

templates.customTemplates[0].file=templates/loopwithpicturesandbasics.odt
templates.customTemplates[0].name=Simple look with pictures and multiple transforms
templates.customTemplates[0].transforms=mytransform

Вот прилагаемая конфигурация:

@Configuration
@ConfigurationProperties("templates")
public class TemplateConfiguration {

  private final Logger logger = LogManager.getLogger(this.getClass());

  public static class TemplateItem {
    private String file;
    private String name;
    private String transforms;

    public TemplateItem() {
    }

    public String getFile() {
      return file;
    }

    public void setFile(String file) {
      this.file = file;
    }

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }

    public String getTransforms() {
      return transforms;
    }

    public void setTransforms(String transforms) {
      this.transforms = transforms;
    }
  }

  public TemplateConfiguration() {
  }

  public TemplateConfiguration(List<TemplateItem> customTemplates) {
    this.customTemplates = customTemplates;
  }

  private List<TemplateItem> customTemplates = new ArrayList<>();

  public List<TemplateItem> getCustomTemplates() {
    return customTemplates;
  }

  public void setCustomTemplates(List<TemplateItem> customTemplates) {
    this.customTemplates = customTemplates;
  }
}

Прямо сейчас с этим кодом customTempaltesсписок пуст.Если я удаляю static из внутреннего класса, я получаю:

Binding to target [Bindable@6759f091 type = java.util.List<com.example.config.TemplateConfiguration$TemplateItem>, value = 'provided', annotations = array<Annotation>[[empty]]] failed:

    Property: templates.customtemplates[0].file
    Value: templates/loopwithpicturesandbasics.odt
    Origin: class path resource [application.yml]:4:15
    Reason: The elements [templates.customtemplates[0].file,templates.customtemplates[0].name,templates.customtemplates[0].transforms] were left unbound.
    Property: templates.customtemplates[0].name
    Value: Simple look with pictures and multiple transforms
    Origin: class path resource [application.yml]:5:15
    Reason: The elements [templates.customtemplates[0].file,templates.customtemplates[0].name,templates.customtemplates[0].transforms] were left unbound.
    Property: templates.customtemplates[0].transforms
    Value: mytransforms
    Origin: class path resource [application.yml]:6:21
    Reason: The elements [templates.customtemplates[0].file,templates.customtemplates[0].name,templates.customtemplates[0].transforms] were left unbound.

(я пробовал как со свойствами, так и с yml)

1 Ответ

0 голосов
/ 22 октября 2018

Попробуйте добавить @EnableConfigurationProperties.Возможно, вы нигде не включали его.

Кроме того, если это поможет, объявите ваш TemplateItem как закрытый класс пакета, а не внутренний класс, как этот (получил то же самое в текущем проекте, и он работает):

@Configuration
@ConfigurationProperties("templates")
@EnableConfigurationProperties
    public TemplateConfiguration {
        ///bolierplate body
        private List<TemplateItem> items;
    }

    TemplateItem{
    /// another body
    }
...