有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring配置嵌套对象列表不工作

我正在尝试设置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.

(我尝试了properties和yml)


共 (1) 个答案

  1. # 1 楼答案

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

    另外,如果这无助于将TemplateItem声明为包私有类,而不是像这样的内部类(在当前项目中得到了相同的结果,并且可以使用):

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