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
Injecting Collections in the Spring Application
- List — using– <list> – </list>
- Set — using– <set> – </set>
- Map — using– <map> – </map>
- Properties –using– <props> – </props>
Using List
<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>
Using Set
<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>
Using Map
<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>
Using Properties file
<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}
Injecting Bean References to the Collection
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>
- Spring Interview Questions and Answers
- Spring AOP Interview Questions and Answers
- Spring MVC Interview Questions
- Spring Security Interview Questions and Answers
- Spring REST Interview Questions and Answers
- Spring Boot Interview Questions and Answers
- Spring Boot Microservices Interview Questions and Answers
- Dependency Injection (DI) in Spring
- Spring IoC Container
- What is Bean Factory in Spring
- ApplicationContext in Spring
- Bean Autowiring in Spring
- Spring Bean Scopes
- Create Custom Bean Scope in Spring Example
- Using ApplicationContextAware in Spring
- Spring Bean Life Cycle and Callbacks
- BeanPostProcessor in Spring
- BeanFactoryPostProcessor in Spring
- Annotations in Spring and Based Configuration
- Spring JSR-250 Annotations
- JSR 330 Annotations in Spring
- Spring @Component, @Repository, @Service and @Controller Stereotype Annotations
- Method injection with Spring using Lookup method property
- Spring AOP-Introduction to Aspect Oriented Programming
- @Aspect Annotation in Spring
- Spring AOP AspectJ @Before Annotation Advice Example
- Spring AOP Before Advice Example using XML Config
- Spring AOP AspectJ @After Annotation Advice Example
- Spring AOP After Advice Example using XML Config
- Spring AOP AspectJ @AfterReturning Annotation Advice Example
- Spring AOP After-Returning Advice Example using XML Config
- Spring AOP AspectJ @AfterThrowing Annotation Advice Example
- Spring AOP After Throwing Advice Example using XML Config
- Spring AOP AspectJ @Around Annotation Advice Example
- Spring AOP Around Advice Example using XML Config
- Spring AOP Proxies in Spring
- Spring AOP Transaction Management in Hibernate
- Spring Transaction Management
- Spring Declarative Transaction Management Example
- Spring AOP-Ordering of Aspects with Example
- Spring Security Java Based Configuration with Example
- Spring Security XML Namespace Configuration Example