Add following depenedency in pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.5</version>
</dependency>
applicationContext.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="a" class="com.deepsingh44.model.Address">
<property name="street" value="1"></property>
<property name="pincode" value="887837"></property>
<property name="district" value="Gzb"></property>
</bean>
<bean id="std" class="com.deepsingh44.model.Student">
<constructor-arg name="roll" value="123"></constructor-arg>
<constructor-arg name="name" value="Sujeet"></constructor-arg>
<constructor-arg name="address" ref="a"></constructor-arg>
<constructor-arg name="certificates">
<set>
<ref bean="c1" />
<ref bean="c2" />
</set>
</constructor-arg>
<constructor-arg name="books">
<map>
<entry key="1001" value-ref="b1"></entry>
<entry key="1002" value-ref="b2"></entry>
</map>
</constructor-arg>
<constructor-arg name="properties">
<props>
<prop key="bike">Tvs Apache</prop>
<prop key="car">Audi</prop>
</props>
</constructor-arg>
<constructor-arg name="pens">
<list>
<ref bean="p1" />
<ref bean="p2" />
</list>
</constructor-arg>
<constructor-arg name="images">
<array>
<value>timeline.jpg</value>
<value>profile.jpg</value>
</array>
</constructor-arg>
</bean>
<bean id="c1" class="com.deepsingh44.model.Certificate">
<property name="year" value="2009"></property>
<property name="name" value="bca"></property>
</bean>
<bean id="c2" class="com.deepsingh44.model.Certificate">
<property name="year" value="2012"></property>
<property name="name" value="mca"></property>
</bean>
<bean id="b1" class="com.deepsingh44.model.Book">
<property name="id" value="1001"></property>
<property name="name" value="Java"></property>
</bean>
<bean id="b2" class="com.deepsingh44.model.Book">
<property name="id" value="1002"></property>
<property name="name" value="Android"></property>
</bean>
<bean id="p1" class="com.deepsingh44.model.Pen">
<property name="name" value="Cello"></property>
<property name="price" value="5.0"></property>
</bean>
<bean id="p2" class="com.deepsingh44.model.Pen">
<property name="name" value="Gell"></property>
<property name="price" value="10.0"></property>
</bean>
</beans>
Student.java
package com.deepsingh44.model;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Student {
private int roll;
private String name;
private Address address;
private Set<Certificate> certificates;
private Map<Integer, Book> books;
private Properties properties;
private List<Pen> pens;
private String[] images;
public Student(int roll, String name,
Address address,
Set<Certificate> certificates,
Map<Integer, Book> books,
Properties properties,
List<Pen> pens, String[] images) {
super();
this.roll = roll;
this.name = name;
this.address = address;
this.certificates = certificates;
this.books = books;
this.properties = properties;
this.pens = pens;
this.images = images;
}
public String[] getImages() {
return images;
}
public List<Pen< getPens() {
return pens;
}
public Properties getProperties() {
return properties;
}
public Map<Integer, Book> getBooks() {
return books;
}
public Set<Certificate> getCertificates() {
return certificates;
}
public int getRoll() {
return roll;
}
public String getName() {
return name;
}
public Address getAddress() {
return address;
}
}
Address.java
package com.deepsingh44.model;
public class Address {
private int street;
private int pincode;
private String district;
public int getStreet() {
return street;
}
public void setStreet(int street) {
this.street = street;
}
public int getPincode() {
return pincode;
}
public void setPincode(int pincode) {
this.pincode = pincode;
}
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
}
Book.java
package com.deepsingh44.model;
public class Book {
private String name;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Certificate.java
package com.deepsingh44.model;
public class Certificate {
private String name;
private int year;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}
Pen.java
package com.deepsingh44.model;
public class Pen {
private String name;
private float price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
Test.java
package com.deepsingh44.model;
import org.springframework.
context.ApplicationContext;
import org.springframework.
context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new
ClassPathXmlApplicationContext("applicationContext.xml");
Student std = (Student) ac.getBean("std");
System.out.println(std.getRoll());
System.out.println(std.getName());
System.out.println(std.getAddress().getStreet());
System.out.println(std.getAddress().getPincode());
System.out.println(std.getAddress().getDistrict());
std.
getCertificates().
stream()
.forEach((certificate) -> System.out.println(certificate.
getYear()
+ "\t" + certificate.getName()));
std.getBooks().
entrySet().
stream().
forEach(x -> System.out.println(x.getKey()
+ "\t" + x.getValue().getName()));
System.out.println(std.getProperties().
getProperty("bike"));
System.out.println(std.getProperties().
getProperty("car"));
std.getPens().
stream().
forEach(x -> System.out.println(x.getName()
+ "\t" + x.getPrice()));
String[] images = std.getImages();
System.out.println(images[0] + "\t" + images[1]);
}
}
No comments:
Post a Comment