Spring config Map overrides Keys with value bean names

Viewed 796

I'm having trouble trying to create a map in Spring config, since the keys I define are replaced when the map is autowired.

I defined the map in my spring-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<context:component-scan base-package="basePackageWhereMyBeansAre"/>

<util:map id="beanMapper" map-class="java.util.HashMap">
   <entry key="QueryType1" value-ref="bean1"/>
   <entry key="QueryType2" value-ref="bean2"/>
</util:map>


</beans>

I try to autowire the map in my Java code:

@Resource
Map<String, BeanSuperClass> beanMapper;

@Autowired
DefaultBeanClass defaultBean;

public String callMethod(Query query) {
    BeanSuperClass bean = beanMapper.getOrDefault(query.getTypeName(), defaultBean);
    ...
}

The problem is that when I debug the Java code and I check the beanMapper object, I can see that the keys for the entries are replaced with the bean names:

beanMapper {
    entry: key="bean1" (the bean name), value=bean1 (the actual bean)
    entry: key="bean2" (the bean name), value=bean2 (the actual bean)
}

so beanMapper.getOrDefault() always returns the defaultBean.

Does anyone knows why the keys are replaced? What am I doing wrong?

Thanks in advance

0 Answers
Related