Spring – Required 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

  • If we declare implemented classes like Vector, TreeSet, HashTable then we can’t able to assign values using <list>, <set>, and <map> tags.
<bean id="" class="">
	<property name="">
		<util:list list-class="java.util.Vector">
			<value></value>
		</util:list>
	<property>
	<property name="">
		<util:set set-class="java.util.TreeSet">
			<value></value>
		</util:set>
	<property>
	<property name="">
		<util:map map-class="java.util.HashTable">
			<entry key="IND" value="Delhi" />
		</util:map>
	<property>
</bean>
  • For above scenario we have to use xml schema instead of going through dtd based configuration. Through dtd it is not possible.
  • Here Core container should not use, only J2EE container is recommended.

Create Java Project

  • Open Eclipse
  • Go to File -> New -> Others… -> Java Project
  • Create DI-RequiredCollection 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 namespace information from spring-beans-X.X.X.jar -> META-INF -> spring.schemas

spring.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
   http://www.springframework.org/schema/util 
   http://www.springframework.org/schema/util/spring-util-4.3.xsd">
	
	<bean id="t" class="com.codeFactory.beans.Test">
		<property name="names">
			<util:list list-class="java.util.Vector">
				<value>Narendra</value>
				<value>Amit</value>
			</util:list>
		</property>
		<property name="companies">
			<util:set set-class="java.util.TreeSet">
				<value>Tata</value>
				<value>Mahindra</value>
				<value>Tata</value>
				<value>Jio</value>
			</util:set>
		</property>
		<property name="stateCapitals">
			<util:map map-class="java.util.Hashtable">
				<entry key="Gujarat" value="Gandhinagar" />
				<entry key="Maharastra" value="Mumbai" />
			</util:map>
		</property>
	</bean>
</beans>

Test.java

package com.codeFactory.beans;

import java.util.Hashtable;
import java.util.TreeSet;
import java.util.Vector;

/**
 * @author code.factory
 *
 */
public class Test {
	private Vector names;
	private TreeSet companies;
	private Hashtable stateCapitals;

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

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

	public void setStateCapitals(Hashtable 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]
Comapnies: [Jio, Mahindra, Tata]
State Capitals: {Maharastra=Mumbai, Gujarat=Gandhinagar}

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