Program of spring
1)Write a program to hello world in spring.
Student.java
This is simple java bean class having only name property.
This is simple bean class, containing only one property name with its
getters and setters method. This class contains one extra method named
display() that prints the student name by the hello message.
package javatpoint;
public class student
{
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public void display()
{
System.out.println("Hello how r u: "+name);
}
}
Test.java
The Resource object represents the information of a.xml file.
The Resource is the interface and the ClassPathResource is
the implementation class of the Reource interface.
The BeanFactory is responsible to return the bean.
The XmlBeanFactory is the implementation class of the BeanFactory.
There are many methods in the BeanFactory interface.
One method is getBean(), which returns the object of the associated class.
package javatpoint;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Test
{
public static void main(String[] args)
{
Resource r=new ClassPathResource("a.xml");
BeanFactory f=new XmlBeanFactory(r);
student s=(student)f.getBean("st");
s.display();
}
}
z.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="st" class="javatpoint.student">
<property name="name" value="HNS"></property>
</bean>
</beans>
2)write a program to print data of employee id and name using constuctor
in spring.
Employee.java
package javatpoint;
public class Employee
{
private int id;
private String name;
public Employee() {System.out.println("HNS COL");}
public Employee(int id)
{this.id = id;}
public Employee(String name)
{ this.name = name;}
public Employee(int id, String name)
{
this.id = id;
this.name = name;
}
void show()
{
System.out.println(id+" "+name);
}
}
Test.java
package javatpoint;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Test
{
public static void main(String[] args)
{
Resource r=new ClassPathResource("z2.xml");
BeanFactory f=new XmlBeanFactory(r);
Employee s=(Employee)f.getBean("s2");
s.display();
}
}
Z2.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="st" class="javatpoint.student">
<constructor-arg value="234" type="int"></contructor-arg>
<constructor-arg value="mscit1"></contructor-arg>
</bean>
</beans>
3) Write a progarm to employee data in spring.
Employee.java
package com.javatpoint;
public class Employee
{
private int id;
private String name;
private Address address;
public Employee(int id, String name, Address address)
{
super();
this.id = id;
this.name = name;
this.address = address;
}
void show()
{
System.out.println(id+" "+name);
System.out.println(address.toString());
}
}
Address.java
package com.javatpoint;
public class Address
{
private String city;
private String state;
private String country;
public Address(String city, String state, String country)
{
super();
this.city = city;
this.state = state;
this.country = country;
}
public String toString()
{
return city+" "+state+" "+country;
}
}
Test.java
package com.javatpoint;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.*;
public class Test
{
public static void main(String[] args)
{
Resource r=new ClassPathResource("applicationContext.xml");
BeanFactory f=new XmlBeanFactory(r);
Employee s=(Employee)f.getBean("e");
s.show();
}
}
z.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="a1" class="Proo3.Address">
<constructor-arg value="rajkot"></constructor-arg>
<constructor-arg value="gujarat"></constructor-arg>
<constructor-arg value="india"></constructor-arg>
</bean>
<bean id="a2" class="Proo3.Employee">
<constructor-arg value="12" type="int"></constructor-arg>
<constructor-arg value="nirali"></constructor-arg>
<constructor-arg >
<ref bean="a1"/>
</constructor-arg>
</bean>
</beans>
4) Wirte a program to collectionlist of employee in spring.
Itrator .txt file is only reference for packges
Java Iterator
It is used to traverse a collection object elements one by one.
It is available in java.util package.
It is applicable for all Collection classes.
It supports both READ and REMOVE Operations.
Compare to Enumeration interface, Iterator method names are simple
and easy to use.
Java List
Some of the important points about Java List are;
Java List interface is a member of the java.util package.
List allows you to add duplicate elements.
List allows you to have ‘null’ elements.
List indexes start from 0, just like arrays.
List supports Generics.
Que.java
package com.javatpoint;
import java.util.Iterator;
import java.util.List;
public class Que
{
private int id;
private String name;
private List<String> answers;
public Que() {}
public Que(int id, String name, List<String> answers)
{
super();
this.id = id;
this.name = name;
this.answers = answers;
}
public void display()
{
System.out.println(id+" "+name);
System.out.println("answers are:");
Iterator<String> itr=answers.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
Test.java
package com.javatpoint;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Test
{
public static void main(String[] args)
{
Resource r=new ClassPathResource("z.xml");
BeanFactory factory=new XmlBeanFactory(r);
Que q=(Que)factory.getBean("q");
q.displayInfo();
}
}
z.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="q" class="com.javatpoint.Que">
<constructor-arg value="111"></constructor-arg>
<constructor-arg value="Who is mad?"></constructor-arg>
<constructor-arg>
<list>
<value>Java is a programming language</value>
<value>Java is a Platform</value>
<value>Java is an Island of Indonasia</value>
</list>
</constructor-arg>
</bean>
</beans>
5) Write a program to non string in spring.
Question.java
package com.javatpoint;
import java.util.Iterator;
import java.util.List;
public class Question {
private int id;
private String name;
private List<Answer> answers;
public Question() {}
public Question(int id, String name, List<Answer> answers) {
super();
this.id = id;
this.name = name;
this.answers = answers;
public void displayInfo(){
System.out.println(id+" "+name);
System.out.println("answers are:");
Iterator<Answer> itr=answers.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
Answer.java
package com.javatpoint;
public class Answer {
private int id;
private String name;
private String by;
public Answer() {}
public Answer(int id, String name, String by) {
super();
this.id = id;
this.name = name;
this.by = by;
public String toString(){
return id+" "+name+" "+by;
Test.java
package com.javatpoint;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Test {
public static void main(String[] args) {
Resource r=new ClassPathResource("applicationContext.xml");
BeanFactory factory=new XmlBeanFactory(r);
Question q=(Question)factory.getBean("q");
q.displayInfo();
}
z.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="ans1" class="com.javatpoint.Answer">
<constructor-arg value="1"></constructor-arg>
<constructor-arg value="Java is a programming language"></constructor-arg>
<constructor-arg value="John"></constructor-arg>
</bean>
<bean id="ans2" class="com.javatpoint.Answer">
<constructor-arg value="2"></constructor-arg>
<constructor-arg value="Java is a Platform"></constructor-arg>
<constructor-arg value="Ravi"></constructor-arg>
</bean>
<bean id="q" class="com.javatpoint.Question">
<constructor-arg value="111"></constructor-arg>
<constructor-arg value="What is java?"></constructor-arg>
<constructor-arg>
<list>
<ref bean="ans1"/>
<ref bean="ans2"/>
</list>
</constructor-arg>
</bean>
</beans>
6) Write a program to get element using collection in spring.
JavaCollection.java
package com.tutorialspoint;
import java.util.*;
public class JavaCollection
{
List<String> addressList;
Set<String> addressSet;
Map<String,String> addressMap;
Properties addressProp;
public void setAddressList(List<String> addressList)
{
this.addressList = addressList;
}
public List<String> getAddressList()
{
System.out.println("List Elements :" + addressList);
return addressList;
}
// a setter method to set Set
public void setAddressSet(Set<String> addressSet) {
this.addressSet = addressSet;
}
// prints and returns all the elements of the Set.
public Set<String> getAddressSet() {
System.out.println("Set Elements :" + addressSet);
return addressSet;
}
// a setter method to set Map
public void setAddressMap(Map<String,String> addressMap) {
this.addressMap = addressMap;
}
// prints and returns all the elements of the Map.
public Map<String,String> getAddressMap() {
System.out.println("Map Elements :" + addressMap);
return addressMap;
}
// a setter method to set Property
public void setAddressProp(Properties addressProp) {
this.addressProp = addressProp;
}
// prints and returns all the elements of the Property.
public Properties getAddressProp() {
System.out.println("Property Elements :" + addressProp);
return addressProp;
}
}
Test.java
package com.tutorialspoint;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Test
{
public static void main(String[] args)
{
Resource r=new ClassPathResource("Beans.xml");
BeanFactory f=new XmlBeanFactory(r);
JavaCollection jc=(JavaCollection)f.getBean("jc1");
jc.getAddressList();
jc.getAddressSet();
jc.getAddressMap();
jc.getAddressProp();
}
}
Beans.xml
<?xml version = "1.0" encoding = "UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Definition for javaCollection -->
<bean id = "jc1" class = "com.tutorialspoint.JavaCollection">
<!-- results in a setAddressList(java.util.List) call -->
<property name = "addressList">
<list>
<value>INDIA</value>
<value>Pakistan</value>
<value>USA</value>
<value>USA</value>
</list>
</property>
<!-- results in a setAddressSet(java.util.Set) call -->
<property name = "addressSet">
<set>
<value>INDIA</value>
<value>Pakistan</value>
<value>USA</value>
<value>USA</value>
</set>
</property>
<!-- results in a setAddressMap(java.util.Map) call -->
<property name = "addressMap">
<map>
<entry key = "1" value = "INDIA"/>
<entry key = "2" value = "Pakistan"/>
<entry key = "3" value = "USA"/>
<entry key = "4" value = "USA"/>
</map>
</property>
<!-- results in a setAddressProp(java.util.Properties) call -->
<property name = "addressProp">
<props>
<prop key = "one">INDIA</prop>
<prop key = "one">INDIA</prop>
<prop key = "two">Pakistan</prop>
<prop key = "three">USA</prop>
<prop key = "four">USA</prop>
</props>
</property>
</bean>
</beans>
7) Write a program using CI MAP in spring.
Que.java
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
public class Que
{
private int id;
private String name;
private Map<String,String> answers;
public Que() {}
public Que(int id, String name, Map<String, String> answers)
{
super();
this.id = id;
this.name = name;
this.answers = answers;
}
public void display()
{
System.out.println("question id:"+id);
System.out.println("question name:"+name);
System.out.println("Answers....");
Set<Entry<String, String>> set=answers.entrySet();
Iterator<Entry<String, String>> itr=set.iterator();
while(itr.hasNext())
{
Entry<String,String> entry=itr.next();
System.out.println("Answer:"+entry.getKey()+" Posted
By:"+entry.getValue());
}
}
}
Test.java
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Test
{
public static void main(String[] args)
{
Resource r=new ClassPathResource("z.xml");
BeanFactory factory=new XmlBeanFactory(r);
Que q=(Que)factory.getBean("q");
q.display();
}
}
z.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="q" class="com.javatpoint.Question">
<constructor-arg value="11"></constructor-arg>
<constructor-arg value="What is Java?"></constructor-arg>
<constructor-arg>
<map>
<entry key="Java is a Programming Language" value="Ajay
Kumar"></entry>
<entry key="Java is a Platform" value="John Smith"></entry>
<entry key="Java is an Island" value="Raj Kumar"></entry>
</map>
</constructor-arg>
</bean>
</beans>
8) Write a program to Cl with non string map in spring.
Que.java
package com.javatopoint;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
public class Que {
private int id;
private String name;
private Map<Answer,User> answers;
public Que() {}
public Que(int id, String name, Map<Answer, User> answers)
super();
this.id = id;
this.name = name;
this.answers = answers;
public void display()
System.out.println("id..."+id);
System.out.println("question......"+name);
System.out.println("Answers....");
Set<Entry<Answer, User>> set=answers.entrySet();
Iterator<Entry<Answer, User>> itr=set.iterator();
while(itr.hasNext())
Entry<Answer, User> entry=itr.next();
Answer ans=entry.getKey();
User user=entry.getValue();
System.out.println("");
// System.out.println("Answer Information:");
System.out.println(ans);
System.out.print("Posted By:");
System.out.println(user);
Answer.java
package com.javatpoint;
public class Answer
private String answer;
public Answer() {}
public Answer(String answer)
super();
this.answer = answer;
}
public String toString()
return " Answer:"+answer";
Test.java
package com.javatopoint;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class Test
public static void main(String[] args)
Resource r=new ClassPathResource("z.xml");
BeanFactory factory=new XmlBeanFactory(r);
Que q=(Que)factory.getBean("q");
q.display();
User.java
package com.javatopoint;
public class User
private String name,email;
public User() {}
public User( String name, String email)
super();
this.name = name;
this.email = email;
public String toString()
return name+"....... Email Id: "+email;
}
z.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="answer1" class="com.javatopoint.Answer">
<constructor-arg value="1"></constructor-arg>
<constructor-arg value="Java is a Programming Language"></constructor-
arg>
</bean>
<bean id="answer2" class="com.javatopoint.Answer">
<constructor-arg value="2"></constructor-arg>
<constructor-arg value="Java is a Platform"></constructor-arg>
</bean>
<bean id="user1" class="com.javatopoint.User">
<constructor-arg value="Jayesh patel"></constructor-arg>
<constructor-arg value="hnsjpsir@gmail.com"></constructor-arg>
</bean>
<bean id="user2" class="com.javatopoint.User">
<constructor-arg value="Vyas Mayur"></constructor-arg>
<constructor-arg value="mayurhns@gmail.com"></constructor-arg>
</bean>
<bean id="q" class="com.javatopoint.Que">
<constructor-arg value="1"></constructor-arg>
<constructor-arg value="What is Java?"></constructor-arg>
<constructor-arg>
<map>
<entry key-ref="answer1" value-ref="user1"></entry>
<entry key-ref="answer2" value-ref="user2"></entry>
</map>
</constructor-arg>
</bean>
</beans>