我想把我application.yml
注入这样的嵌套Map
:
application.yml
components:
component1:
host: host1
port: 1111
component2:
host: host2
port: 2222
component3:
host: host3
port: 3333
Run Code Online (Sandbox Code Playgroud)
MyComponents.java
@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
}
Run Code Online (Sandbox Code Playgroud)
但即使我没有实例化object(@Autowire
),我收到以下错误信息:
org.springframework.beans.NullValueInNestedPathException:bean类的无效属性'components [component1]'[package.path.MyComponents]:无法实例化属性类型[package.path.MyComponents $ MyComponent]以自动增长嵌套属性路径:java .lang.InstantiationException:package.path.MyComponents $ MyComponent
如何将配置添加到我的类布局中?
解:
我自己找到了解决方案:内部类必须是静态的.
内部class
MyComponent
必须是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
}
Run Code Online (Sandbox Code Playgroud)
这个答案是由OP Squall在CC BY-SA 3.0下作为对Spring Boot中的嵌套外部配置类问题的编辑发布的。
归档时间: |
|
查看次数: |
1083 次 |
最近记录: |