Spring Collection Dependency List Example

Till now, we inject the dependencies from the spring configuration file as simple types like primitives or wrapper classes (Objects) through the <properties> tag. But in Spring we can define the dependencies for three different types.

Simple Type

Object Type and

Collection Type

In the all previous Spring examples, we have injected the dependencies by using the simple (normal properties) and Object (any property as a type of object). But in this tutorial, we are going to implement spring collection dependency.

Spring Collection Dependency :

In spring we can inject the dependencies as a type of collection. But we have some limitations on that. i.e. Spring container can inject collection type, if and only if the collection type is one of the below 4.

java.util.List

java.util.Set

java.util.Map

java.util.Properties

We can say that above four are spring collection types. Apart from those four types, we can not implement any collection type in spring.

Spring Collection List Example :

If the dependency type is java.util.List, then in spring configuration file we should configure the List type with <list> tag.

If that list contains the normal properties like String or int or float etc.. we can define the values by using the <value> tag.

If the list contains the Object type, we can assign the bean to that object by using the <ref> tag.

Below is the example for Lists contains normal properties and Object types.

Hi
Folks,
you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,