in Spring

Spring util:map

Introduction

It is a common case to wire java.util.Map and then to inject the map into your bean. In this short tutorial i will show you how to do that.

XML

Here is how to configure your Map in XML.

Observe attribute map-class, here I have specified what Map implementation used is going to be used.

Ordered Map

In upper xml example I have used HashMap, it is important to notice that HashMap does not have ordered keys. This can be important im many cases , so here is how to set this up using LinkedHashMap.

 

Here is part from java doc Hash table and linked list implementation of the Map interface, with predictable iteration order. This implementation differs from HashMap in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-order). Note that insertion order is not affected if a key is re-inserted into the map. (A key k is reinserted into a map m if m.put(k, v) is invoked when m.containsKey(k) would return true immediately prior to the invocation.)

 

Injection

Here is how to inject valueMap in your bean.

 

Spring is awesome 🙂

Write a Comment

Comment