Reputation: 13
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.
Upvotes: 1
Views: 3310
Reputation: 4365
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:
example-unit:
confiList:
- string1
- string2
- hello22
@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;
}
}
Upvotes: 0
Reputation: 39978
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());
}
}
Upvotes: 1
Reputation: 3678
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]
Upvotes: -1
Reputation: 9377
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
Upvotes: 1