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

时间:2018-10-22 07:41:58

标签: java spring spring-boot spring-config

我正在尝试使用该文件设置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)

1 个答案:

答案 0 :(得分:0)

尝试添加 @EnableConfigurationProperties。也许您没有在任何地方启用它。

此外,如果这样做有助于将您的TemplateItem声明为包私有类,但不能这样声明内部类(在当前项目中也是如此,那么它就起作用了):

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

    TemplateItem{
    /// another body
    }