Spring Injecting Collection


You have perceived how to design crude information type utilizing value characteristic and item references utilizing ref quality of the <property> tag in your Bean setup record. Both the cases manage passing solitary incentive to a bean.

Now imagine a scenario in which you need to pass plural qualities like Java Collection types, for example, List, Set, Map, and Properties. To deal with the circumstance, Spring offers four kinds of accumulation design components which are as per the following −

Sr.No Element & Description
1

<list>

This helps in wiring ie infusing a rundown of qualities, permitting duplicates.

2

<set>

This helps in wiring a lot of esteems however with no duplicates.

3

<map>

This can be utilized to infuse an accumulation of name-esteem sets where name and esteem can be of any type.

4

<props>

This can be utilized to infuse a gathering of name-esteem sets where the name and esteem are both Strings.

You can utilize either <list> or <set> to wire any execution of java.util.Collection or a array.

You will go over two circumstances (a) Passing immediate estimations of the gathering and (b) Passing a reference of a bean as one of the accumulation elements.

Example

Let us have a working Eclipse IDE set up and find a way to make a Spring application −

Steps Description
1 Create a task with a name SpringExample and make a bundle com.tutorialspoint under the src envelope in the made project.
2 Add required Spring libraries utilizing Add External JARs alternative as clarified in the Spring Hello World Example chapter.
3 Create Java classes JavaCollection, and MainApp under the com.tutorialspoint package.
4 Create Beans arrangement record Beans.xml under the src folder.
5 The last advance is to make the substance of all the Java documents and Bean Configuration record and run the application as clarified below.

Here is the substance of JavaCollection.java record −

package com.tutorialspoint; 

import java.util.*; 

public class JavaCollection { 

List addressList; 

Set addressSet; 

Map addressMap; 

Properties addressProp; 

//a setter technique to set List 

public void setAddressList(List addressList) { 

this.addressList = addressList; 

}

//prints and returns every one of the components of the list. 

public List getAddressList() { 

System.out.println("List Elements :" + addressList); 

return addressList; 

} 

//a setter strategy to set Set 

public void setAddressSet(Set addressSet) { 

this.addressSet = addressSet; 

} 

//prints and returns every one of the components of the Set. 

public Set getAddressSet() { 

System.out.println("Set Elements :" + addressSet); 

return addressSet; 

} 

//a setter strategy to set Map 

public void setAddressMap(Map addressMap) { 

this.addressMap = addressMap; 

} 

//prints and returns every one of the components of the Map. 

public Map getAddressMap() { 

System.out.println("Map Elements :" + addressMap); 

return addressMap; 

} 

//a setter strategy to set Property 

public void setAddressProp(Properties addressProp) { 

this.addressProp = addressProp; 

}
    //prints and returns every one of the components of the Property. 

public Properties getAddressProp() { 

System.out.println("Property Elements :" + addressProp); 

return addressProp; 

} 

}

Following is the substance of the MainApp.java record −

package com.tutorialspoint; 

import org.springframework.context.ApplicationContext; 

import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class MainApp { 

public static void main(String[] args) { 

ApplicationContext setting = new ClassPathXmlApplicationContext("Beans.xml"); 

JavaCollection jc=(JavaCollection)context.getBean("javaCollection"); 

jc.getAddressList(); 

jc.getAddressSet(); 

jc.getAddressMap(); 

jc.getAddressProp(); 

} 

}

Following is the arrangement document Beans.xml which has design for all the kind of accumulations −

<?xml form = "1.0" encoding = "UTF-8"?> 

<beans xmlns = "http://www.springframework.org/mapping/beans" 

xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation = "http://www.springframework.org/mapping/beans 

http://www.springframework.org/mapping/beans/spring-beans-3.0.xsd">
    <!- - Definition for javaCollection - > 

<bean id = "javaCollection" class = "com.tutorialspoint.JavaCollection"> 

<!- - results in a setAddressList(java.util.List) call - > 

<property name = "addressList"> 

<list> 

<value>INDIA</value> 

<value>Pakistan</value> 

<value>USA</value> 

<value>USA</value> 

</list> 

</property> 

<!- - results in a setAddressSet(java.util.Set) call - > 

<property name = "addressSet"> 

<set> 

<value>INDIA</value> 

<value>Pakistan</value> 

<value>USA</value> 

<value>USA</value> 

</set> 

</property> 

<!- - results in a setAddressMap(java.util.Map) call - > 

<property name = "addressMap"> 

<map> 

<entry key = "1" value = "INDIA"/> 

<entry key = "2" value = "Pakistan"/> 

<entry key = "3" value = "USA"/> 

<entry key = "4" value = "USA"/> 

</map> 

</property> 

<!- - results in a setAddressProp(java.util.Properties) call - > 

<property name = "addressProp"> 

<props> 

<prop key = "one">INDIA</prop> 

<prop key = "one">INDIA</prop> 

<prop key = "two">Pakistan</prop> 

<prop key = "three">USA</prop> 

<prop key = "four">USA</prop> 

</props> 

</property> 

</bean> 

</beans>

Once you are finished making the source and bean setup records, let us run the application. On the off chance that all is well with your application, it will print the accompanying message −

List Elements :[INDIA, Pakistan, USA, USA] 

Set Elements :[INDIA, Pakistan, USA] 

ap Elements :{1 = INDIA, 2 = Pakistan, 3 = USA, 4 = USA} 

Property Elements :{two = Pakistan, one = INDIA, three = USA, four = USA} 

Injecting Bean References

The following Bean definition will enable you to see how to infuse bean references as one of the gathering's component. Indeed, even you can combine references and qualities all as appeared in the accompanying code bit −

<?xml adaptation = "1.0" encoding = "UTF-8"?> 

<beans xmlns = "http://www.springframework.org/composition/beans" 

xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation = "http://www.springframework.org/composition/beans 

http://www.springframework.org/composition/beans/spring-beans-3.0.xsd"> 

<!- - Bean Definition to deal with references and qualities - > 

<bean id = "..." class = "..."> 

<!- - Passing bean reference for java.util.List - > 

<property name = "addressList"> 

<list> 

<ref bean = "address1"/> 

<ref bean = "address2"/> 

<value>Pakistan</value> 

</list> 

</property> 

<!- - Passing bean reference for java.util.Set - > 

<property name = "addressSet"> 

<set> 

<ref bean = "address1"/> 

<ref bean = "address2"/> 

<value>Pakistan</value> 

</set> 

</property>
<!- - Passing bean reference for java.util.Map - > 

<property name = "addressMap"> 

<map> 

<entry key = "one" value = "INDIA"/> 

<entry key = "two" value-ref = "address1"/> 

<entry key = "three" value-ref = "address2"/> 

</map> 

</property> 

</bean> 

</beans>

To utilize the above bean definition, you have to characterize your setter techniques so that they ought to most likely handle references as well.

Injecting invalid and void string values

If you have to pass an unfilled string as an esteem, at that point you can pass it as pursues −

<bean id = "..." class = "exampleBean"> 

<property name = "email" value = ""/> 

</bean>

The going before model is proportionate to the Java code: exampleBean.setEmail("")

If you have to pass a NULL esteem, at that point you can pass it as pursues −

<bean id = "..." class = "exampleBean"> 

<property name = "email"><null/></property> 

</bean>

The going before precedent is proportional to the Java code: exampleBean.setEmail(null)







© Javacodegeeks 2018 -2019
All Right Reserved and you agree to have read and accepted our term and condition.

Python 3