10:57 am - Thursday April 27, 2017

Spring Collection map Example Program in Eclipse

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

Project Structure

Jar Files

How  To  Add Jar Files:

 spring-config.xml

 

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
	<bean id="stringId" class="java.lang.String">
		<constructor-arg>
			<value>Course</value>
		</constructor-arg>
	</bean>

	<bean id="student" class="com.candidjava.Student">


		<property name="studentDetails">
			<map>
				<entry key="Name">
					<value>Deepa</value>
				</entry>
				<entry key="Course">
					<value>JAVA</value>
				</entry>
				<entry key="Address">
					<value>Chennai</value>
				</entry>
				<entry key="">
					<ref local="stringId" />
				</entry>
			</map>
		</property>

	</bean>
</beans>


Student.java

package com.candidjava;

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

public class Student {

	private Map studentDetails = null;

	public void setStudentDetails(Map studentMap) {
		this.studentDetails = studentMap;
	}

	public Map getStudentDetails() {
		return studentDetails;
	}

}

StudentMain.java

package com.candidjava;

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

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class StudentMain {
	public static void main(String a[]) {

		BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
				"config\\spring-config.xml"));
		Student student = (Student) factory.getBean("student");

		Map studentMap = student.getStudentDetails();
		Iterator iterator2 = studentMap.values().iterator();
		System.out.println("Map in Spring");
		System.out.println();
		while (iterator2.hasNext()) {
			Object obj = iterator2.next();

			System.out.println(obj);
			System.out.println();
		}
	}

}

 OUTPUT

DOWNLOAD SOURCE CODE+JAR FILES

Filed in: Z-A Spring Codes

Comments are closed.