Spring Setter Dependency Injection

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">
        <property name="roll" value="123"></property>
        <property name="name" value="Rohit"></property>
        <property name="address" ref="a"></property>

        <property name="certificates">
        <set>
        <ref bean="c1" />
        <ref bean="c2" />
        </set>
        </property>

        <property name="books">
        <map>
        <entry key="1001" value-ref="b1"></entry>
        <entry key="1002" value-ref="b2"></entry>
        </map>
        </property>

        <property name="properties">
        <props>
        <prop key="bike">Tvs Apache</prop>
        <prop key="car">Audi</prop>
        </props>
        </property>

        <property name="pens">
        <list>
        <ref bean="p1"/>
        <ref bean="p2"/>
        </list>
        </property>

        <property name="images">
        <array>
        <value>timeline.jpg</value>
        <value>profile.jpg</value>
        </array>
        </property>

        </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 String[] getImages() {
            return images;
        }
    
        public void setImages(String[] images) {
            this.images = images;
        }
    
        public List<Pen> getPens() {
            return pens;
        }
    
        public void setPens(List<Pen> pens) {
            this.pens = pens;
        }
    
        public Properties getProperties() {
            return properties;
        }
    
        public void setProperties(Properties properties) {
            this.properties = properties;
        }
    
        public Map<Integer, Book> getBooks() {
            return books;
        }
    
        public void setBooks(Map<Integer, Book> books) {
            this.books = books;
        }
    
        public Set<Certificate> getCertificates() {
            return certificates;
        }
    
        public void setCertificates(Set<Certificate> certificates) {
            this.certificates = certificates;
        }
    
        public int getRoll() {
            return roll;
        }
    
        public void setRoll(int roll) {
            this.roll = roll;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Address getAddress() {
            return address;
        }
    
        public void setAddress(Address address) {
            this.address = 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: