簡體   English   中英

Spring配置嵌套對象列表不起作用

[英]Spring configuration nested object list not working

我正在嘗試設置 spring boot 以使用此文件配置我的應用程序:

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)

嘗試添加@EnableConfigurationProperties 也許你沒有在任何地方啟用它。

此外,如果這無助於將您的TemplateItem聲明為包私有類而不是像這樣的內部類(在當前項目中是相同的並且可以正常工作):

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM