Injecting Collections in Spring-We can inject collection values by constructor in spring framework. Spring Collections (List, Set, Map, and Properties) example. Spring examples to show you how to inject values into collections type (List, Set, Map, and Properties).
@ImageSource-SlideShare.net
<property name="listOfShape"> <list> <value>Triangle</value> <value>Circle</value> <value>Rectangle</value> <value>Square</value> </list> </property>
<property name="listOfShape"> <list> <value>Triangle</value> <value>Circle</value> <ref bean="rectangle"></ref> <bean class="com.dineshonjava.sdnext.Square"> <property name="width" value="20"></property> </bean> </list> </property>
<property name="setOfShape"> <set> <value>Triangle</value> <value>Circle</value> <value>Rectangle</value> <value>Square</value> </set> </property>
<property name="setOfShape"> <set> <value>Triangle</value> <value>Circle</value> <ref bean="rectangle"></ref> <bean class="com.dineshonjava.sdnext.Square"> <property name="width" value="20"></property> </bean> </set> </property>
<property name="mapOfShape"> <map> <entry key="1" value="Triangle"> <entry key="2" value="Circle"> <entry key="3" value="Rectangle"> <entry key="4" value="Square"> </entry></entry></entry></entry> </map> </property>
<property name="mapOfShape"> <map> <entry key="1" value="Triangle"></entry> <entry key="2" value="Circle"></entry> <entry key="3" value-ref="rectangle"></entry> <entry key="4"> <bean class="com.dineshonjava.sdnext.Square"> <property name="width" value="20"></property> </bean> </entry> </map> </property>
<property name="proprtyOfShape"> <props> <prop key="triangle">Triangle</prop> <prop key="circle">Circle</prop> <prop key="rectangle">Rectangle</prop> <prop key="sqare">Square</prop> </props> </property>
ShapeCollection.java
package com.dineshonjava.sdnext.injectingCollection; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class ShapeCollection { private List<string> shapeOfList; private Set<string> shapeOfSet; private Map<string string=""> shapeOfMap; private Properties shapeOfProperties; /** * @param shapeOfList the shapeOfList to set */ public void setShapeOfList(List<string> shapeOfList) { this.shapeOfList = shapeOfList; } /** * @param shapeOfSet the shapeOfSet to set */ public void setShapeOfSet(Set<string> shapeOfSet) { this.shapeOfSet = shapeOfSet; } /** * @param shapeOfMap the shapeOfMap to set */ public void setShapeOfMap(Map<string string=""> shapeOfMap) { this.shapeOfMap = shapeOfMap; } /** * @param shapeOfProperties the shapeOfProperties to set */ public void setShapeOfProperties(Properties shapeOfProperties) { this.shapeOfProperties = shapeOfProperties; } public String toString() { return "List Elements :" + shapeOfList+"nSet Elements :" + shapeOfSet+"n" + "Map Elements :" + shapeOfMap+"nProperty Elements :" + shapeOfProperties; } }
spring.xml
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean class="com.dineshonjava.sdnext.injectingCollection.ShapeCollection" id="shapeCollection"> <property name="shapeOfList"> <list> <value>Triangle</value> <value>Circle</value> <value>Circle</value> <value>Rectangle</value> </list> </property> <property name="shapeOfSet"> <set> <value>Triangle</value> <value>Circle</value> <value>Circle</value> <value>Rectangle</value> </set> </property> <property name="shapeOfMap"> <map> <entry key="1" value="Triangle"> <entry key="2" value="Circle"> <entry key="3" value="Circle"> <entry key="4" value="Rectangle"> </entry></entry></entry></entry></map> </property> <property name="shapeOfProperties"> <props> <prop key="triangle">Triangle</prop> <prop key="circle1">Circle</prop> <prop key="circle2">Circle</prop> <prop key="rectangle">Rectangle</prop> </props> </property> </bean> </beans>
DrawingApp.java
package com.dineshonjava.sdnext.injectingCollection.tutorial; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.dineshonjava.sdnext.injectingCollection.ShapeCollection; /** * @author Dinesh Rajput * */ public class DrawingApp { /** * @param args */ public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); ShapeCollection shapeCollection = (ShapeCollection) context.getBean("shapeCollection"); System.out.println(shapeCollection); } }
List Elements :[Triangle, Circle, Circle, Rectangle]
Set Elements :[Triangle, Circle, Rectangle]
Map Elements :{1=Triangle, 2=Circle, 3=Circle, 4=Rectangle}
Property Elements :{rectangle=Rectangle, circle2=Circle, triangle=Triangle, circle1=Circle}
In the following bean configuration file has understand how to inject bean references as one of the collection’s element.
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean class="com.dineshonjava.sdnext.injectCollection.Triangle" id="triangle"> <property name="height" value="30"></property> <property name="type" value="Eqiletral"></property> </bean> <bean class="com.dineshonjava.sdnext.injectingCollection.ShapeCollection" id="shapeCollection"> <property name="shapeOfList"> <list> <value>Circle</value> <value>Circle</value> <ref bean="triangle"></ref> <bean class="com.dineshonjava.sdnext.injectCollection.Rectangle"> <property name="height" value="30"></property> <property name="width" value="50"></property> </bean> </list> </property> <property name="shapeOfSet"> <set> <value>Circle</value> <value>Circle</value> <ref bean="triangle"></ref> <bean class="com.dineshonjava.sdnext.injectCollection.Rectangle"> <property name="height" value="30"></property> <property name="width" value="50"></property> </bean> </set> </property> <property name="shapeOfMap"> <map> <entry key="2" value="Circle"></entry> <entry key="3" value="Circle"></entry> <entry key="1" ref-value="triangle"></entry> <entry key="4"> <bean class="com.dineshonjava.sdnext.injectCollection.Rectangle"> <property name="height" value="30"></property> <property name="width" value="50"></property> </bean> </entry> </map> </property> <property name="shapeOfProperties"> <props> <prop key="triangle">Triangle</prop> <prop key="circle1">Circle</prop> <prop key="circle2">Circle</prop> <prop key="rectangle">Rectangle</prop> </props> </property> </bean> </beans>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…