Spring无法自动连线Map

Spring无法自动连线Map

本文介绍了Spring无法自动连线Map bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我已经在春季定义了一个地图: < util:map id =AdditionalParamsscope = prototypemap-class =java.util.HashMap key-type =java.lang.Stringvalue-type =java.lang.String> < entry key =Startvalue =12345/> < entry key =Finishvalue =12365/> < / util:map> 然后我将这个bean自动连接到一个定义为: private @Autowired @Qualifier(value =AdditionalParams)Map< String,String> additionalParams; 当这样做时,抛出异常,说:但是使用较低的Spring版本,您无法自动连接一个集合在。但是,您可以执行以下操作: @Resource(name =AdditionalParams) private Map< String,串GT; additionalParams; 甚至: @Value(#{AdditionalParams}) private Map< String,String> additionalParams; 检查 spring docs ,提示部分: I've defined a map in spring as such:<util:map id="AdditionalParams" scope="prototype" map-class="java.util.HashMap" key-type="java.lang.String" value-type="java.lang.String"> <entry key="Start" value="12345" /> <entry key="Finish" value="12365" /></util:map>And then I'm autowiring this bean to a property defined as:private @Autowired @Qualifier(value = "AdditionalParams") Map<String, String> additionalParams;When doing this, the an exception get's thrown saying that:Any ideas?Cheers. 解决方案 Starting with Spring 4.3, @Autowired can inject lists and maps and the given code in the question would work:But with a lower Spring version, you can't autowire a collection like that. However, you can do the following:@Resource(name="AdditionalParams")private Map<String, String> additionalParams;or even:@Value("#{AdditionalParams}")private Map<String, String> additionalParams;Check the spring docs, the tips section: 这篇关于Spring无法自动连线Map bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-06 04:11