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



我正在尝试设置弹簧启动以使用此文件配置我的应用程序:

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

现在使用此代码,自定义时间列表为空。如果我从内部类中删除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
}

相关内容

  • 没有找到相关文章

最新更新