Spring – Default Collection DI Parameters | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

This image has an empty alt attribute; its file name is image-1.png

Spring Tutorial Index Page: Link

  • For List, Set, Map collection we use <list>, <set>, and <map> tags.
  • We have to define List, Set, Map interface to use these tags.
  • * Default it will create ArrayList for list, LinkedHashSet for set and LinkedHashMap for map tag.
<bean id="" class="">
	<property name="">
		<list>
			<value></value>
		</list>
	<property>
</bean>

Create Java Project

  • Open Eclipse
  • Go to File -> New -> Others… -> Java Project
  • Create DI-DefaultCollection project
  • Right click on project -> Build Path -> Configure Build Path -> Libraries tab -> Add External JARs
    • commons-logging-X.X.jar
    • spring-beans-X.X.X.jar
    • spring-context-X.X.X.jar
    • spring-core-X.X.X.jar
    • spring-expression-X.X.X.jar
  • * You can find dtd information from spring-beans-X.X.X.jar -> org -> springframework -> beans -> factory -> xml -> spring-beans.dtd (line no 36 & 37)

spring.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
	"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
	<bean id="t" class="com.codeFactory.beans.Test">
		<property name="names">
			<list>
				<value>Narendra</value>
				<value>Amit</value>
				<value>Yogi</value>
			</list>
		</property>
		<property name="companies">
			<set>
				<value>Tata</value>
				<value>Mahindra</value>
				<value>Tata</value>
				<value>Jio</value>
			</set>
		</property>
		<property name="stateCapitals">
			<map>
				<entry key="Gujarat" value="Gandhinagar" />
				<entry key="Maharastra" value="Mumbai" />
			</map>
		</property>
	</bean>
</beans>

Test.java

package com.codeFactory.beans;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author code.factory
 *
 */
public class Test {
	private List names;
	private Set companies;
	private Map stateCapitals;

	public void setNames(List names) {
		this.names = names;
	}

	public void setCompanies(Set companies) {
		this.companies = companies;
	}

	public void setStateCapitals(Map stateCapitals) {
		this.stateCapitals = stateCapitals;
	}

	public void print() {
		System.out.println("Names: " + names);
		System.out.println("Comapnies: " + companies);
		System.out.println("State Capitals: " + stateCapitals);
	}
}

Client.java

package com.codeFactory.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.codeFactory.beans.Test;

/**
 * @author code.factory
 *
 */
public class Client {
	public static void main(String... args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("com/codeFactory/resources/spring.xml");
		
		Test t = (Test) context.getBean("t");
		t.print();
	}
}

Output:

Names: [Narendra, Amit, Yogi]
Comapnies: [Tata, Mahindra, Jio]
State Capitals: {Gujarat=Gandhinagar, Maharastra=Mumbai}

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 )

Connecting to %s