How to configure setbased collection as dependency to a spring bean

previous-button-hisubmit-hi

 

Que: -How to configure setbased collection as dependency to a spring bean? Give example

Step 1: –In the spring bean make a list based or set based collection variable as dependency.

e.g.: –

public class College
{
List<Student> students;
Or
Set<Student> students;
----------------
----------------
}

Step 2: –Make use of <set> tag in the beans-configuration file.

Note: –In case of list based collection, duplicates are allowed. Whereas in case of set based collections duplicates are not allowed.

e.g.: –

<set>
<ref bean=”s1”/>
<ref local=”s2”/>
<ref local=”s3”/>
<ref local=”s4”/>
< /set>

In a spring application multiple configuration files. While specifying the elements to a collection we make use of <ref> tag. For this tag we can use “local” attribute instead of “bean” attribute if the dependency bean is also configured in the same xml file. If the dependency bean is configured in another configuration file, “local” attribute can’t be used, we must use “bean” attribute only.

Que: -How to inject a map based collection into a spring bean?

Step 1: –In the spring bean class declare “java.util.Map” type variable as dependency.

e.g.: –

public class College
{
Map<Integer, Student> student;
-------------------
-------------------
}

Step 2: –Make use of <map> tag in spring configuration file as given in the example case.

e.g.: –

<map>
<entry key=”1001”>
<bean id=”s1” class=”com.nareshit.Students”>
<property name=”rollno” value=”1001”>
<property name=”name” value=”Rama”>
< /bean>
< /entry>
---------------
---------------
< /map>

previous-button-hisubmit-hi

Leave a Reply

Your email address will not be published. Required fields are marked *