0

I'm trying to setup spring boot to configure my app using this file:

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

Here is the attached configuration:

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

Right now with this code the customTempaltes list is empty. If I remove the static from the inner class I get:

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.

(I tried both with properties and yml)

Vinz243
  • 9,654
  • 10
  • 42
  • 86

1 Answers1

0

Try adding @EnableConfigurationProperties. Maybe you didn't enable it anywhere.

Also, if that won't help declare your TemplateItem as package private class but not inner class like this (got the same in current project and it works):

@Configuration
@ConfigurationProperties("templates")
@EnableConfigurationProperties
    public TemplateConfiguration {
        ///bolierplate body
        private List<TemplateItem> items;
    }
    
    TemplateItem{
    /// another body
    }
Dylan
  • 2,161
  • 2
  • 27
  • 51
Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • Where should I add it too ? My main class ? – Vinz243 Oct 22 '18 at 07:53
  • Add it to bootstrap class, or here, above your configuration. Would work in both cases – Antoniossss Oct 22 '18 at 07:53
  • I tried adding the annotation, but it still doesn't work. The inner class is used in another package so it needs to be public – Vinz243 Oct 22 '18 at 07:57
  • Then don't make it inner class as it is not "inner" but public. Make it public. But that is kind of offtopic as "should work" i think anyway. – Antoniossss Oct 22 '18 at 07:57
  • Moved it into its own class and it's still not loading unfortunately – Vinz243 Oct 22 '18 at 07:59
  • As I wrote, its kind of topic and should not influence that much, Check if `@Value{${templates.customTemplates[0].name}}` resolves to anything meaningfull. If so, idk what is happening, if not - properties are not read. – Antoniossss Oct 22 '18 at 08:05
  • It turns out the configuration is not being loaded. I tried adding a new string to my TemplateConfig and it's null – Vinz243 Oct 22 '18 at 10:20
  • If it is in application.properties it should be loaded no matter what IMHO. Try to inject some of those properties with `@Value` or at least debug and see what is present in `Environment` as those properties will be as plain map. If they are not there - you are excluding it somewhere. – Antoniossss Oct 22 '18 at 11:33