How Spring container deals with Collections?

Here is the below example that shows how the spring container inject values to List and Set collection interfaces during run-time,

public class Shape {
private List shapes;
private Set diffTypeOfShapes;

//Navigating LIST values
public void drawAllShapes() {
for(Shape shape : shapes) {
shape.draw();
}
}

//Navigating SET values
public void printTypesOfShapes() {
for(String types : diffTypeOfShapes) {
System.out.println(types);
}
}
}

Here the below configuration files, shows how spring container injects the values to the LIST and SET instances in the above code

<bean id="shape" class="com.saravanansivaji.spring.Shape">
<property name="shapes">
<list>
<ref bean="triangle"/>
<ref bean="circle"/>
</list>
</property>

<property name="diffTypeOfShapes">
<set>
<value>Triangle</value>
<value>Circle</value>
</set>
</property>
</bean>

Happy coding 😀

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: