Here is my HashMap in spring context which uses value-ref :
<util:map id="generalDAOMap" map-class="java.util.HashMap">
<entry key="1" value-ref="userDAO" />
<entry key="2" value-ref="accountsDAO" />
<entry key="3" value-ref="settingsDAO" />
</util:map>
<bean id="userDAO"
class="com.test.myproj.dao.impl.UserDAOImpl" />
<bean id="accountsDAO"
class="com.test.myproj.dao.impl.AccountsDAOImpl" />
<bean id="settingsDAO"
class="com.test.myproj.dao.impl.SettingsDAOImpl" />
In my Java class i have autowired the "generalDAOMap" as below:
@AutoWired
private Map<String,GeneralDAO> generalDAOMap;
//getters and setters follows
When i print the map value its as follows :
{userDAO = com.test.myproj.dao.impl.UserDAOImpl@164d6ae,accountsDAO = com.test.myproj.dao.impl.AccountsDAOImpl@ed9382 , settingsDAO = com.test.myproj.dao.impl.SettingsDAOImpl@ed9586}
but the Key values should be "1","2" and "3" , why its getting replaced as "userDAO ","settingsDAO " and "accountsDAO ", due to which when i use getGeneralDAOMap.get("1") it returns null. Kindly help.
Can you try using @Resource instead of using @Autowired ?
Spring docs says this,
As a specific consequence of this semantic difference, beans that are themselves defined as a collection or map type cannot be injected through @Autowired, because type matching is not properly applicable to them. Use @Resource for such beans, referring to the specific collection or map bean by unique name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With