I want to inject my application.yml
into a nested Map
like this:
application.yml
components:
component1:
host: host1
port: 1111
component2:
host: host2
port: 2222
component3:
host: host3
port: 3333
MyComponents.java
@Component
@ConfigurationProperties
public class MyComponents {
Map<String, MyComponent> components;
public class MyComponent {
String host;
Integer port;
//empty constructor, getters, setters
}
//empty constructor, getters, setters
}
But even if I don't instantiate the object ( @Autowire
), I'm getting the following error message:
org.springframework.beans.NullValueInNestedPathException: Invalid property 'components[component1]' of bean class [package.path.MyComponents]: Could not instantiate property type [package.path.MyComponents$MyComponent] to auto-grow nested property path: java.lang.InstantiationException: package.path.MyComponents$MyComponent
How do I get the configuration into my class layout?
The inner class
MyComponent
has to be static
:
@Component
@ConfigurationProperties
public class MyComponents {
Map<String, MyComponent> components;
public static class MyComponent {
String host;
Integer port;
//empty constructor, getters, setters
}
//empty constructor, getters, setters
}
This answer was posted as an edit to the question Nested external configuration classes in Spring Boot by the OP Squall under CC BY-SA 3.0.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.