Map Collection Wiring in Spring
For the System requirements, project
required jars and project creation setup steps please refer the below links:-
http://www.javatechtipssharedbygaurav.com/2013/06/collection-injection-in-spring.html
http://www.javatechtipssharedbygaurav.com/2013/06/collection-injection-in-spring.html
Example of Spring Map property or Map Collection injection in spring.
WorldWonders.java
package
com.spring.gaurav.collection.map.use.example;
public class WorldWonders {
private
String wondersName;
private
String wondersCountriesName;
public
String getWondersName() {
return
wondersName;
}
public
void setWondersName(String wondersName) {
this.wondersName
= wondersName;
}
public
String getWondersCountriesName() {
return
wondersCountriesName;
}
public
void setWondersCountriesName(String wondersCountriesName) {
this.wondersCountriesName
= wondersCountriesName;
}
}
WondersEra.Java
package
com.spring.gaurav.collection.map.use.example;
import java.util.Map;
public class WondersEra {
private
Map<Object, Object> ancientAndModernWonders;
public
Map<Object, Object> getAncientAndModernWonders() {
return
ancientAndModernWonders;
}
public
void setAncientAndModernWonders(
Map<Object,
Object> ancientAndModernWonders) {
this.ancientAndModernWonders
= ancientAndModernWonders;
}
}
collection-map-use-in-spring-framework.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean
id="wondersEraBeanId" class="com.spring.gaurav.collection.map.use.example.WondersEra">
<property
name="ancientAndModernWonders">
<map>
<entry
key="EGYPT" value="GREAT PYRAMID OF GIZA" />
<entry
key="IRAQ" value="HANGING GARDENS OF BABYLON"
/>
<entry
key="GREECE" value="STATUE OF ZEUS AT OLYMPIA"
/>
<entry
key="TURKEY" value="TEMPLE OF ARTEMIS" />
<entry
key="BODRUM, TURKEY" value="MAUSOLEUM AT
HALICARNASSUS" />
<entry
key="RHODES, GREEK ISLAND" value="COLOSSUS OF
RHODES" />
<entry
key="ALEXANDRIA, EGYPT" value="LIGHTHOUSE OF
ALEXANDRIA" />
<!-- We can add
key - value pair in the map in the below two formats,
first
by directly adding the bean or second by adding the referenced bean -->
<entry
key="modernWorldWondersId1">
<bean
class="com.spring.gaurav.collection.map.use.example.WorldWonders">
<property
name="wondersName" value="GREAT WALL OF CHINA"
/>
<property
name="wondersCountriesName" value="CHINA" />
</bean>
</entry>
<entry
key="modernWorldWondersId2" value-ref="modernWorldWonders2"
/>
<entry
key="modernWorldWondersId3" value-ref="modernWorldWonders3"
/>
<entry
key="modernWorldWondersId4" value-ref="modernWorldWonders4"
/>
<entry
key="modernWorldWondersId5" value-ref="modernWorldWonders5"
/>
<entry
key="modernWorldWondersId6" value-ref="modernWorldWonders6"
/>
<entry
key="modernWorldWondersId7" value-ref="modernWorldWonders7"
/>
</map>
</property>
</bean>
<bean
id="modernWorldWonders2"
class="com.spring.gaurav.collection.map.use.example.WorldWonders">
<property
name="wondersName" value="PETRA" />
<property
name="wondersCountriesName" value="JORDAN" />
</bean>
<bean
id="modernWorldWonders3"
class="com.spring.gaurav.collection.map.use.example.WorldWonders">
<property
name="wondersName" value="CHRIST THE REDEEMER"
/>
<property
name="wondersCountriesName" value="BRAZIL" />
</bean>
<bean
id="modernWorldWonders4"
class="com.spring.gaurav.collection.map.use.example.WorldWonders">
<property
name="wondersName" value="MACHU PICCHU" />
<property
name="wondersCountriesName" value="PERU" />
</bean>
<bean
id="modernWorldWonders5"
class="com.spring.gaurav.collection.map.use.example.WorldWonders">
<property
name="wondersName" value="CHICHEN ITZA" />
<property
name="wondersCountriesName" value="MEXICO" />
</bean>
<bean
id="modernWorldWonders6"
class="com.spring.gaurav.collection.map.use.example.WorldWonders">
<property
name="wondersName" value="COLOSSEUM" />
<property
name="wondersCountriesName" value="ITALY" />
</bean>
<bean
id="modernWorldWonders7"
class="com.spring.gaurav.collection.map.use.example.WorldWonders">
<property
name="wondersName" value="TAJ MAHAL" />
<property
name="wondersCountriesName" value="INDIA" />
</bean>
</beans>
MapCollectionUseInSpringFrameworkClient.java
package
com.spring.gaurav.collection.map.use.example;
import java.util.Map;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class
MapCollectionUseInSpringFrameworkClient {
public
static void main(String args[]) {
WorldWonders
worldWonders = null;
ApplicationContext
context = new ClassPathXmlApplicationContext(
"collection-map-use-in-spring-framework.xml");
WondersEra
wondersEra = (WondersEra) context
.getBean("wondersEraBeanId");
Map<Object,
Object> worldWondersMap = wondersEra
.getAncientAndModernWonders();
System.out.println("ANCIENT
AND MODERNS WONDERS OF THE WORLD ");
System.out.println("********************************************");
for
(Map.Entry<Object, Object> entry : worldWondersMap.entrySet()) {
if
(entry.getKey().equals("modernWorldWondersId1")
||
entry.getKey().equals("modernWorldWondersId2")
||
entry.getKey().equals("modernWorldWondersId3")
||
entry.getKey().equals("modernWorldWondersId4")
||
entry.getKey().equals("modernWorldWondersId5")
||
entry.getKey().equals("modernWorldWondersId6")
||
entry.getKey().equals("modernWorldWondersId7")) {
worldWonders
= (WorldWonders) entry.getValue();
System.out.println("\nCOUNTRY
NAME = "
+
worldWonders.getWondersCountriesName()
+
" || MODERN WOLRD WONDERS = " + worldWonders.getWondersName());
}
else {
System.out.println("COUNTRY
NAME = " + entry.getKey()
+
" || ANCIENT WOLRD WONDERS = " + entry.getValue());
}
}
}
}
Result:-
log4j:WARN No appenders
could be found for logger
(org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please
initialize the log4j system properly.
ANCIENT AND MODERNS WONDERS OF THE WORLD
********************************************
COUNTRY NAME = EGYPT
|| ANCIENT WOLRD WONDERS = GREAT PYRAMID OF GIZA
COUNTRY NAME = IRAQ
|| ANCIENT WOLRD WONDERS = HANGING GARDENS OF BABYLON
COUNTRY NAME = GREECE
|| ANCIENT WOLRD WONDERS = STATUE OF ZEUS AT OLYMPIA
COUNTRY NAME = TURKEY
|| ANCIENT WOLRD WONDERS = TEMPLE OF ARTEMIS
COUNTRY NAME =
BODRUM, TURKEY || ANCIENT WOLRD WONDERS = MAUSOLEUM AT HALICARNASSUS
COUNTRY NAME =
RHODES, GREEK ISLAND || ANCIENT WOLRD WONDERS = COLOSSUS OF RHODES
COUNTRY NAME =
ALEXANDRIA, EGYPT || ANCIENT WOLRD WONDERS = LIGHTHOUSE OF ALEXANDRIA
COUNTRY NAME = CHINA
|| MODERN WOLRD WONDERS = GREAT WALL OF CHINA
COUNTRY NAME = JORDAN
|| MODERN WOLRD WONDERS = PETRA
COUNTRY NAME = BRAZIL
|| MODERN WOLRD WONDERS = CHRIST THE REDEEMER
COUNTRY NAME = PERU
|| MODERN WOLRD WONDERS = MACHU PICCHU
COUNTRY NAME = MEXICO
|| MODERN WOLRD WONDERS = CHICHEN ITZA
COUNTRY NAME = ITALY
|| MODERN WOLRD WONDERS = COLOSSEUM
COUNTRY NAME = INDIA
|| MODERN WOLRD WONDERS = TAJ MAHAL
No comments:
Post a Comment