Program of Spring 2023
Program of Spring 2023
This is simple bean class, containing only one property name with its
getters and setters method. This class contains one extra method named
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
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;
student s=(student)f.getBean("st");
s.display();
}
}
z.xml
<beans>
</bean>
</beans>
Employee.java
package javatpoint;
public class Employee
{
private int id;
private String 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
<beans>
<bean id="st" class="javatpoint.student">
<constructor-arg value="234" type="int"></contructor-arg>
<constructor-arg value="mscit1"></contructor-arg>
</bean>
</beans>
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.*;
Employee s=(Employee)f.getBean("e");
s.show();
}
}
z.xml
Java Iterator
Que.java
package com.javatpoint;
import java.util.Iterator;
import java.util.List;
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;
Que q=(Que)factory.getBean("q");
q.displayInfo();
}
}
z.xml
</beans>
Question.java
package com.javatpoint;
import java.util.Iterator;
import java.util.List;
public Question() {}
super();
this.id = id;
this.name = name;
this.answers = answers;
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 Answer() {}
super();
this.id = id;
this.name = name;
this.by = 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;
Question q=(Question)factory.getBean("q");
q.displayInfo();
}
z.xml
<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">
<constructor-arg value="1"></constructor-arg>
<constructor-arg value="John"></constructor-arg>
</bean>
<constructor-arg value="2"></constructor-arg>
<constructor-arg value="Ravi"></constructor-arg>
</bean>
<constructor-arg>
<list>
<ref bean="ans1"/>
<ref bean="ans2"/>
</list>
</constructor-arg>
</bean>
</beans>
JavaCollection.java
package com.tutorialspoint;
import java.util.*;
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
<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">
</beans>
Que.java
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
public Que() {}
public Que(int id, String name, Map<String, String> answers)
{
super();
this.id = id;
this.name = name;
this.answers = answers;
}
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;
Que q=(Que)factory.getBean("q");
q.display();
}
}
z.xml
</beans>
Que.java
package com.javatopoint;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
public Que() {}
public Que(int id, String name, Map<Answer, User> answers)
super();
this.id = id;
this.name = name;
this.answers = answers;
System.out.println("id..."+id);
System.out.println("question......"+name);
System.out.println("Answers....");
while(itr.hasNext())
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 Answer() {}
super();
this.answer = answer;
}
public String toString()
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;
Que q=(Que)factory.getBean("q");
q.display();
User.java
package com.javatopoint;
public User() {}
super();
this.name = name;
this.email = email;
}
z.xml
<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">
<constructor-arg value="1"></constructor-arg>
</bean>
<constructor-arg value="2"></constructor-arg>
</bean>
<bean id="user1" class="com.javatopoint.User">
<constructor-arg value="[email protected]"></constructor-arg>
</bean>
<constructor-arg value="[email protected]"></constructor-arg>
</bean>
<constructor-arg value="1"></constructor-arg>
<constructor-arg>
<map>
</map>
</constructor-arg>
</bean>
</beans>