怎么用springboot注入一个 属性 private MapString ListString headerSource ;试编写完整注入代码
可以使用Spring Boot的注解@Value来注入Map类型的属性。具体操作如下:
-
在类中定义私有属性private Map<String, List
> headerSource。 -
在属性上方添加@Value注解,指定属性的值。
-
在Spring Boot的配置文件application.properties或application.yml中配置属性的值。
完整代码如下:
@Component
public class MyComponent {
@Value("#{${headerSource}}")
private Map<String, List<String>> headerSource;
// ...
}
在上述代码中,@Value注解的值使用了SpEL表达式,表示将配置文件中名为headerSource的属性注入到属性headerSource中。
假设配置文件中headerSource的值为:
headerSource:
key1: value1,value2
key2: value3,value4,value5
则在MyComponent中,headerSource将被注入为:
{
"key1": ["value1", "value2"],
"key2": ["value3", "value4", "value5"]
}
``
原文地址: https://www.cveoy.top/t/topic/hfhM 著作权归作者所有。请勿转载和采集!