91-9990449935 0120-4256464 |
Setter Injection with Collection ExampleWe can inject collection values by setter method in spring framework. There can be used three elements inside the property element. It can be:
In this example, we are taking the example of Forum where One question can have multiple answers. There are three pages:
In this example, we are using list that can have duplicate elements, you may use set that have only unique elements. But, you need to change list to set in the applicationContext.xml file and List to Set in the Question.java file. Question.javaThis class contains three properties with setters and getters and displayInfo() method that prints the information. Here, we are using List to contain the multiple answers. applicationContext.xmlThe list element of constructor-arg is used here to define the list. Test.javaThis class gets the bean from the applicationContext.xml file and calls the displayInfo method.
download this example (developed using MyEclipse IDE)
download this example (developed using Eclipse IDE) |