1

I just created a really basic spring boot application using spring initializer and am trying things out. I want to load a list from a yaml configuration file, but it always returns empty.

I have a custom configuration class

@ConfigurationProperties("example-unit")
@EnableConfigurationProperties
public class ConfigurationUnit {

    public List<String> confiList = new ArrayList<>();

    public List<String> getConfiList() {
        return this.confiList;
    }

}

And my main class looks like this

@SpringBootApplication
public class DemoApplication {

    static ConfigurationUnit configurationUnit = new ConfigurationUnit();


    public static void main(String[] args) {

        SpringApplication.run(DemoApplication.class, args);

        List<String> hello = configurationUnit.getConfiList();

        System.out.print("");
    }

}

I have put the application.yaml into resources folder.

example-unit:
  - string1
  - string2
  - hello22

I searched here and online, but can't figure out what's the issue and nothing I changed helped. I know I must be doing something wrong.

4 Answers 4

1

This statement is wrong static ConfigurationUnit configurationUnit = new ConfigurationUnit(); you should not create the object

Spring only injects the properties into the beans that are handled by application context, and spring creates beans of classes that are annotated with @ Configuration

ConfigurationUnit

@Configuration
@ConfigurationProperties("example-unit")
public class ConfigurationUnit {

public List<String> confiList;

public List<String> getConfiList() {
    return this.confiList;
    }

 }

DemoApplication In the spring boot main get the bean from applicationcontext and from it get the list object

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {

    ApplicationContext context = SpringApplication.run(DemoApplication.class, args);
     ConfigurationUnit unit = context.getBean("configurationUnit"):

    System.out.print(unit. getConfiList());
   }

}
Sign up to request clarification or add additional context in comments.

2 Comments

good point. I oversaw the static ConfigurationUnit declaration in Application class. That of course disturbs the whole bean-injection magic Spring is known for.
Thanks, yeah that is the main mistake I made. Not using the bean spring boot creates with @Configuration. And also it seems to be sensitive to naming so I had to add example-unit.confi-list: to the header of yaml file so that it recognizes the list by its name, so it can instantiate it with values from the config.
1

Put your list under prefix.property. In your case example-unit.confi-list:. Usually provide a setter for your property: setConfiList(List<String> strings). But since you already initialized it as empty Array list this setter is obsolete says this. There is also advice to add Enable-annotation to Application class:

Application class should have @EnableConfigurationProperties annotation

1 Comment

Tried this and it still returns an empty list, but thanks for answering.
0

Here is the reference on how Spring Bboot Configurtion Binding works.

Specifically for your question, this is an example of app that achives your goal:

  • application.yml
example-unit:
  confiList:
    - string1
    - string2
    - hello22
  • sources
@SpringBootApplication
@EnableConfigurationProperties(ConfigurationUnit.class)
public class DemoApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
        ConfigurationUnit configurationUnit = context.getBean(ConfigurationUnit.class);
        System.out.println(configurationUnit.getConfiList());
    }

}

@ConfigurationProperties("example-unit")
public class ConfigurationUnit {

    public List<String> confiList = new ArrayList<>();

    public List<String> getConfiList() {
        return this.confiList;
    }

}

Comments

-1

Here is an example :

Application.yml:

example-unit: string1,string2,hello22

ConfigurationUnit.class:

@Component
@PropertySource(value="classpath:application.yml")
public class ConfigurationUnit {

    @Value("#{'${example-unit}'.split(',')}")
    private List<String> confiList;

    public List<String> getConfiList() {
        return confiList;
    }
}

DemoFileLoadApplication.class:

@SpringBootApplication
public class DemoFileLoadApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(DemoFileLoadApplication.class, args);
        ConfigurationUnit configurationUnit = context.getBean(ConfigurationUnit.class);
        System.out.println(configurationUnit.getConfiList());
    }
}

Output:

[string1, string2, hello22]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.