Spring Dependency Injection (DI)

  • By
  • March 5, 2020
  • JAVA Programming
Spring Dependency Injection (DI)

Spring Dependency Injection (DI)

The technology that Spring is most popular is with the concept of Dependency Injection (an abbreviation of DI) which is a flavor of Inversion of Control in programming frameworks. The Inversion of Control (an abbreviation of IoC) is a general concept and it can be expressed in easy and different ways of implementations.

In complex projects and java applications, the required classes should be as independent as possible of other classes to improve the code and features readability, reuse, and efficiency. So that these classes also can be tested independently of other classes. Dependency Injection helps in making these classes work together and at the same time keeping them separate and independent.

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

Let’s see the below example explain by Java Classes In Pune to understand in a better way:

An employee has a Project. With this statement, we can state that there is an association relation between Employee and Project entity. Normally in java, we would have written with below snippet. 

class Employee{

private Project project;

   public Employee() {

  project = new Project();

  }

}

class Project{}

In an inversion control we would have done above dependency in below way then:

n an inversion of control scenario, we would instead do something like this

public class Employee {

  private Project project;

  public Employee(Project project) {

      this.project = project;

  }

}

class Project{}

Here at Java Course In Pune , the Employee should not worry about Project implementation. The Project will be implemented independently and will be provided to the Employee at the time of Employee instantiation. This whole procedure from start to end of DI and bean life cycle is fully controlled by the Spring Framework.

Slo DI i.e. Dependency injection have multiple variants to do.

1 Constructor oriented dependency injection

Constructor oriented DI is invoked when the container asks for a class constructor with a number of arguments and each is representing a dependency of other class.

Example 1:

package com.myorg;

public class Employee {

  private Project Project;

  public Employee(Project Project) {

      System.out.println(“Inside Employee constructor.” );

      this.Project = Project;

  }

  public void projectCheck() {

      Project.checkProject();

  }

}

——————————————————————————————————

package com.myorg;

public class Project {

  public Project(){

      System.out.println(“Inside Project constructor.” );

  }

  public void checkProject() {

      System.out.println(“Inside check Project .” );

  }

}

——————————————————————————————————

bean.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”

   xsi:schemaLocation = “http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>

   <!– Definition for Employee bean –>

   <bean id = “employee” class = “com.myorg.Employee”>

      <constructor-arg ref = “project”/>

   </bean>

   <!– Definition for Project bean –>

   <bean id = “project” class = “com.myorg.Project”></bean>

</beans>

——————————————————————————————————

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

TestSpring.java

package com.myorg;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class TestSpring {  

public static void main(String[] args) {  

ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”);

       Employee e1 = (Employee) context.getBean(“employee”);

      e1.projectCheck();

}  

}  

Example 2:

package com.myorg;

public class Student{

  private Course Course;

 public Student(Course Course) {

      System.out.println(“Inside Studentconstructor.” );

      this.Course = Course;

  }

  public void CourseCheck() {

      Course.checkCourse();

  }

}

——————————————————————————————————–

package com.myorg;

public class Course {

  public Course(){

      System.out.println(“Inside Course constructor.” );

  }

  public void checkCourse() {

      System.out.println(“Inside check Course .” );

  }

}

——————————————————————————————————

bean.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”

   xsi:schemaLocation = “http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>

   <!– Definition for Studentbean –>

   <bean id = “Student” class = “com.myorg.Student”>

      <constructor-arg ref = “Course”/>

   </bean>

   <!– Definition for Course bean –>

   <bean id = “Course” class = “com.myorg.Course”></bean>

</beans>

——————————————————————————————————

TestSpring.java

package com.myorg;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class TestSpring {  

public static void main(String[] args) {  

ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”);      

      Studente1 = (Student) context.getBean(“Student”);

      e1.CourseCheck();

}  

}  

Example 3:

package com.myorg;

public class Teacher{

  private batch batch;

  public Teacher(batch batch) {

      System.out.println(“Inside Teacherconstructor.” );

      this.batch = batch;

  }

  public void batchCheck() {

      batch.checkbatch();

  }

}

—————————————————————————————————— 

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

package com.myorg;

public class batch {

  public batch(){

      System.out.println(“Inside batch constructor.” );

  }

  public void checkbatch() {

      System.out.println(“Inside check batch .” );

  }

}

——————————————————————————————————

bean.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”

   xsi:schemaLocation = “http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>

   <!– Definition for Teacherbean –>

   <bean id = “Teacher” class = “com.myorg.Teacher”>

      <constructor-arg ref = “batch”/>

   </bean>

   <!– Definition for batch bean –>

   <bean id = “batch” class = “com.myorg.batch”></bean>

</beans>

——————————————————————————————————

TestSpring.java

package com.myorg;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class TestSpring {  

public static void main(String[] args) {  

ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”);

      Teachere1 = (Teacher) context.getBean(“Teacher”);

      e1.batchCheck();

}  

}  

2 Setter based dependency injection

Setter based Dependency Injection is accomplished by the spring container calling setter methods of our beans after invoking a constructor without argument to instantiate your bean.

Example 1:

bean.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”

   xsi:schemaLocation = “http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>

<!– Definition for Employee bean –>

   <bean id = “employee” class = “com.myorg.Employee”>

      <property name = “project” ref = “project”/>

   </bean>

   <!– Definition for projectChecker bean –>

   <bean id = “project” class = “com.myorg.Project”></bean>

</beans>

——————————————————————————————————

Employee.java

package com.myorg;

public class Employee {

   private Project project;

  // a setter method to inject the dependency.

   public void setProject(Project project) {

      System.out.println(“Inside setProject.” );

      this.project = project;

   }

   // a getter method to return spellChecker

   public Project getProject() {

      return project;

   }

   public void projCheck() {

      project.checkProject();

   }

}

——————————————————————————————————-

TestSpring.java

package com.myorg;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class TestSpring {  

public static void main(String[] args) {  

ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”);

Employee e1 = (Employee) context.getBean(“employee”);

      e1.projCheck();

}  

Example 2:

bean.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”

   xsi:schemaLocation = “http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>

<!– Definition for student bean –>

   <bean id = “student” class = “com.myorg.student”>

      <property name = “course” ref = “course”/>

   </bean>

   <!– Definition for courseChecker bean –>

   <bean id = “course” class = “com.myorg.course”></bean>

</beans>

—————————————————————————————————— 

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

student.java

package com.myorg;

public class student {

   private course course;

   // a setter method to inject the dependency.

   public void setcourse(course course) {

      System.out.println(“Inside setcourse.” );

      this.course = course;

   }

   // a getter method to return spellChecker

   public course getcourse() {

      return course;

   }

   public void projCheck() {

      course.checkcourse();

   }

}

——————————————————————————————————-

TestSpring.java

package com.myorg;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class TestSpring {  

public static void main(String[] args) {  

ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”);

 student e1 = (student) context.getBean(“student”);

      e1.projCheck();

}  

Example 3:

bean.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”

   xsi:schemaLocation = “http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>

<!– Definition for teacher bean –>

   <bean id = “teacher” class = “com.myorg.teacher”>

      <property name = “batch” ref = “batch”/>

   </bean>

   <!– Definition for batchChecker bean –>

   <bean id = “batch” class = “com.myorg.batch”></bean>

</beans>

——————————————————————————————————-

teacher.java

package com.myorg;

public class teacher {

   private batch batch;

   // a setter method to inject the dependency.

   public void setbatch(batch batch) {

      System.out.println(“Inside setbatch.” );

      this.batch = batch;

   }

   // a getter method to return spellChecker

   public batch getbatch() {

      return batch;

   }

   public void projCheck() {

      batch.checkbatch();

   }

}

———————————————————————————————————–

TestSpring.java

package com.myorg;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class TestSpring {  

public static void main(String[] args) {  

ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”);

 teacher e1 = (teacher) context.getBean(“teacher”);

      e1.projCheck();

}  

  1. Constructor arguments resolution

To resolve ambiguity Java Training In Pune can maintain the order in which the dependency is passed as a parameter to the constructor.

Example 1:

package com.myorg;

public class Dept {

  public Dept(){

      System.out.println(“Inside Dept constructor.” );

  }

  public void checkDept() {

      System.out.println(“Inside check Dept .” );

  }

}

——————————————————————————————————–

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

Employee.java

package com.myorg;

public class Employee {

  private Project project;

  private Dept dept;

  public Employee(Project project, Dept dept) {

      System.out.println(“Inside Employee constructor.” );

      this.project = project;

      this.dept = dept;

  }

  public void projectCheck() {

      project.checkProject();

  }

  public void deptCheck() {

      dept.checkDept();

           }

}

——————————————————————————————————–

<?xml version = “1.0” encoding = “UTF-8”?>

<beans xmlns = “http://www.springframework.org/schema/beans”

   xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance”

   xsi:schemaLocation = “http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>

   <!– Definition for Employee bean –>

   <bean id = “employee” class = “com.myorg.Employee”>

      <constructor-arg ref = “project”/>

      <constructor-arg ref = “dept”/>

   </bean>

   <!– Definition for projectChecker bean –>

   <bean id = “project” class = “com.myorg.Project”></bean>

   <!– Definition for deptChecker bean –>

   <bean id = “dept” class = “com.myorg.Dept”></bean>

</beans>

——————————————————————————————————-

package com.myorg;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class TestSpring {  

public static void main(String[] args) {  

ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”);

    Employee e1 = (Employee) context.getBean(“employee”);

      e1.projectCheck();

      e1.deptCheck();

}  

——————————————————————————————————–

Passing type argument with value:

package com.myorg;

public class Employee {

  private Project project;

  private Dept dept;

  private int eid;

  private String ename;

  public Employee(Project project, Dept dept,int eid, String ename) {

      System.out.println(“Inside Employee constructor.” );

      this.project = project;

      this.dept = dept;

      this.eid = eid;

      this.ename = ename;

  }

  public void projectCheck() {

      project.checkProject();

  }

  public void deptCheck() {

      dept.checkDept();

  }

  public void empCheck() {

      System.out.println(“Eid:”+eid+” Ename:”+ename);

  }

}

——————————————————————————————————-

<?xml version = “1.0” encoding = “UTF-8”?>

<beans xmlns = “http://www.springframework.org/schema/beans”

   xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance”

   xsi:schemaLocation = “http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>

 <!– Definition for Employee bean –>

   <bean id = “employee” class = “com.myorg.Employee”>

      <constructor-arg ref = “project”/>

      <constructor-arg ref = “dept”/>

      <constructor-arg type = “int” value = “101”/>

      <constructor-arg type = “java.lang.String” value = “Sachin”/>

   </bean>

</beans>

——————————————————————————————————–

package com.myorg;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class TestSpring {  

public static void main(String[] args) {  

ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”);

    Employee e1 = (Employee) context.getBean(“employee”);

      e1.projectCheck();

      e1.deptCheck();

      e1.empCheck();

  Employee e2 = (Employee) context.getBean(“employee”);

      e2.projectCheck();

      e2.deptCheck();

      e2.empCheck();  

}  

Example 1:

package com.myorg;

public class Course {

  public Course(){

      System.out.println(“Inside Course constructor.” );

  }

  public void checkCourse() {

      System.out.println(“Inside check Course .” );

  }

}

—————————————————————————————————— 

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

Employee.java

package com.myorg;

public class Student{

  private Teacherproject;

  private Course Course;

  public Employee(Teacherproject, Course Course) {

      System.out.println(“Inside Studentconstructor.” );

      this.Teacher= project;

      this.Course = Course;

  }

  public void projectCheck() {

      project.checkProject();

  }

  public void CourseCheck() {

      Course.checkCourse();

           }

}

——————————————————————————————————

<?xml version = “1.0” encoding = “UTF-8”?>

<beans xmlns = “http://www.springframework.org/schema/beans”

   xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance”

   xsi:schemaLocation = “http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>

   <!– Definition for Studentbean –>

   <bean id = “employee” class = “com.myorg.Employee”>

      <constructor-arg ref = “project”/>

      <constructor-arg ref = “Course”/>

   </bean>

   <!– Definition for projectChecker bean –>

   <bean id = “project” class = “com.myorg.Project”></bean>

   <!– Definition for CourseChecker bean –>

   <bean id = “Course” class = “com.myorg.Course”></bean>

</beans>

——————————————————————————————————-

package com.myorg;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class TestSpring {  

public static void main(String[] args) {  

ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”);

       Studente1 = (Employee) context.getBean(“employee”);

      e1.projectCheck();

      e1.CourseCheck();

}  

——————————————————————————————————

Passing type argument with value:

package com.myorg;

public class Student{

  privateproject;

  private Course Course;

  private int eid;

  private String ename;

  public Employee(Teacherproject, Course Course,int eid, String ename) {

      System.out.println(“Inside Studentconstructor.” );

      this.Teacher= project;

      this.Course = Course;

      this.eid = eid;

      this.ename = ename;

  }

     public void projectCheck() {

      project.checkProject();

  }

  public void CourseCheck() {

      Course.checkCourse();

  }

  public void empCheck() {

      System.out.println(“Eid:”+eid+” Ename:”+ename);

  }

}

——————————————————————————————————

<?xml version = “1.0” encoding = “UTF-8”?>

<beans xmlns = “http://www.springframework.org/schema/beans”

   xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance”

   xsi:schemaLocation = “http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>

 <!– Definition for Studentbean –>

   <bean id = “Student” class = “com.myorg.Student”>

      <constructor-arg ref = “project”/>

      <constructor-arg ref = “Course”/>

      <constructor-arg type = “int” value = “101”/>

      <constructor-arg type = “java.lang.String” value = “Sachin”/>

   </bean>

</beans>

——————————————————————————————————

package com.myorg;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class TestSpring {  

public static void main(String[] args) {  

ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”);

     Studente1 = (Student) context.getBean(“employee”);

      e1.projectCheck();

      e1.CourseCheck();

      e1.empCheck();

  Studente2 = (Student) context.getBean(“employee”);

      e2.projectCheck();

      e2.CourseCheck();

      e2.empCheck();  

}  

package com.myorg;

public class Blog{

  public Course(){

      System.out.println(“Inside Blogconstructor.” );

  }

  public void checkCourse() {

      System.out.println(“Inside check Blog.” );

  }

}

——————————————————————————————————

Trainer.java

package com.myorg;

public class Trainer{

  private Teacherproject;

  private BlogCourse;

  public Trainer(Teacherproject, BlogCourse) {

      System.out.println(“Inside Trainerconstructor.” );

      this.Teacher= project;

      this.Blog= Course;

  }

  public void projectCheck() {

      project.checkProject();

  }

  public void CourseCheck() {

      Course.checkCourse();

           }

}

—————————————————————————————————— 

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

<?xml version = “1.0” encoding = “UTF-8”?>

<beans xmlns = “http://www.springframework.org/schema/beans”

   xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance”

   xsi:schemaLocation = “http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>

   <!– Definition for Trainerbean –>

   <bean id = “Trainer” class = “com.myorg.Trainer”>

      <constructor-arg ref = “project”/>

      <constructor-arg ref = “Course”/>

   </bean>

   <!– Definition for projectChecker bean –>

   <bean id = “project” class = “com.myorg.Project”></bean>

   <!– Definition for CourseChecker bean –>

   <bean id = “Course” class = “com.myorg.Course”></bean>

</beans>

——————————————————————————————————

package com.myorg;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class TestSpring {  

public static void main(String[] args) {  

ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”);

    Trainere1 = (Trainer) context.getBean(“Trainer”);

      e1.projectCheck();

      e1.CourseCheck();

}  

——————————————————————————————————

Passing type argument with value:

package com.myorg;

public class Trainer{

  private Teacherproject;

  private BlogCourse;

  private int eid;

  private String ename;

  public Trainer(Teacherproject, BlogCourse,int eid, String ename) {

      System.out.println(“Inside Trainerconstructor.” );

      this.Teacher= project;

      this.Blog= Course;

      this.eid = eid;

      this.ename = ename;

  }

 public void projectCheck() {

      project.checkProject();

  }

  public void CourseCheck() {

      Course.checkCourse();

  }

  public void empCheck() {

      System.out.println(“Eid:”+eid+” Ename:”+ename);

  }

}

——————————————————————————————————

<?xml version = “1.0” encoding = “UTF-8”?>

<beans xmlns = “http://www.springframework.org/schema/beans”

   xmlns:xsi = “http://www.w3.org/2001/XMLSchema-instance”

   xsi:schemaLocation = “http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>

 <!– Definition for Trainerbean –>

   <bean id = “Trainer” class = “com.myorg.Trainer”>

      <constructor-arg ref = “project”/>

      <constructor-arg ref = “Course”/>

      <constructor-arg type = “int” value = “101”/>

      <constructor-arg type = “java.lang.String” value = “Sachin”/>

   </bean>

</beans>

——————————————————————————————————

package com.myorg;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class TestSpring {  

public static void main(String[] args) {  

ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”);

  Trainere1 = (Trainer) context.getBean(“Trainer”);

      e1.projectCheck();

      e1.CourseCheck();

      e1.empCheck();

  Trainere2 = (Trainer) context.getBean(“Trainer”);

      e2.projectCheck();

      e2.CourseCheck();

      e2.empCheck();  

}  

Injecting collection:

Example:

StateCollection.java

package com.myorg;

import java.util.*;

public class StateCollection {

   List addressList;

   Set  addressSet;

   Map  addressMap;

   Properties addressProp;

   // a setter method to set List

   public void setAddressList(List addressList) {

      this.addressList = addressList;

   }

  // prints and returns all the elements of the list.

   public List getAddressList() {

      System.out.println(“List Elements :”  + addressList);

      return addressList;

   }

   // a setter method to set Set

   public void setAddressSet(Set addressSet) {

      this.addressSet = addressSet;

   }

// prints and returns all the elements of the Set.

   public Set getAddressSet() {

      System.out.println(“Set Elements :”  + addressSet);

      return addressSet;

   }

// a setter method to set Map

   public void setAddressMap(Map addressMap) {

      this.addressMap = addressMap;

   }

  // prints and returns all the elements of the Map.

   public Map 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;

   }

}

—————————————————————————————————— 

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

TestSpring.java

package com.myorg;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class TestSpring {

   public static void main(String[] args) {

      ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”);

      StateCollection sc=(StateCollection)context.getBean(“statecollection”);

      sc.getAddressList();

      sc.getAddressSet();

      sc.getAddressMap();

      sc.getAddressProp();

   }

}

——————————————————————————————————

Bean.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”

   xsi:schemaLocation = “http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>

  <!– Definition for javaCollection –>

   <bean id = “statecollection” class = “com.myorg.StateCollection”>

     <!– results in a setAddressList(java.util.List) call –>

      <property name = “addressList”>

         <list>

            <value>Maharashtra</value>

            <value>Goa</value>

            <value>Karnataka</value>

            <value>Keral</value>

         </list>

      </property>

<!– results in a setAddressSet(java.util.Set) call –>

      <property name = “addressSet”>

         <set>

            <value>Maharashtra</value>

            <value>Goa</value>

            <value>Karnataka</value>

            <value>Keral</value>

         </set>

      </property>

<!– results in a setAddressMap(java.util.Map) call –>

      <property name = “addressMap”>

         <map>

            <entry key = “1” value = “Maharashtra”/>

            <entry key = “2” value = “Goa”/>

            <entry key = “3” value = “Karnataka”/>

            <entry key = “4” value = “Keral”/>

         </map>

      </property>

<!– results in a setAddressProp(java.util.Properties) call –>

      <property name = “addressProp”>

         <props>

            <prop key = “one”>Maharashtra</prop>            

            <prop key = “two”>Goa</prop>

            <prop key = “three”>Karnataka</prop>

            <prop key = “four”>Keral</prop>

         </props>

      </property>

   </bean> 

</beans>

——————————————————————————————————

Assignment: 

Emp —> Certificate (Map certificates [1- SSC, 2-HSC, 3-UG, 4-PG])

Create 2 emps and add certificates (Setter DI) to them. Display all certificates of all emp’s.

Example 2: 

CountryCollection.java

package com.myorg;

import java.util.*;

public class CountryCollection {

   List ctryList;

   Set  ctrySet;

   Map  ctryMap;

   Properties ctryProp;

 // a setter method to set List

   public void setctryList(List ctryList) {

      this.ctryList = ctryList;

   }

  // prints and returns all the elements of the list.

   public List getctryList() {

      System.out.println(“List Elements :”  + ctryList);

      return ctryList;

   }

   // a setter method to set Set

   public void setctrySet(Set ctrySet) {

      this.ctrySet = ctrySet;

   }

// prints and returns all the elements of the Set.

   public Set getctrySet() {

      System.out.println(“Set Elements :”  + ctrySet);

      return ctrySet;

   }

// a setter method to set Map

   public void setctryMap(Map ctryMap) {

      this.ctryMap = ctryMap;

   }

// prints and returns all the elements of the Map.

   public Map getctryMap() {

      System.out.println(“Map Elements :”  + ctryMap);

      return ctryMap;

   }

// a setter method to set Property

   public void setctryProp(Properties ctryProp) {

      this.ctryProp = ctryProp;

   }

// prints and returns all the elements of the Property.

   public Properties getctryProp() {

      System.out.println(“Property Elements :”  + ctryProp);

      return ctryProp;

   }

}

—————————————————————————————————— 

TestSpring.java

package com.myorg;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class TestSpring {

   public static void main(String[] args) {

      ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”);

      CountryCollection sc=(CountryCollection)context.getBean(“CountryCollection”);

sc.getctryList();

      sc.getctrySet();

      sc.getctryMap();

      sc.getctryProp();

   }

}

—————————————————————————————————— 

Bean.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”

   xsi:schemaLocation = “http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>

<!– Definition for javaCollection –>

   <bean id = “CountryCollection” class = “com.myorg.CountryCollection”>

      <!– results in a setctryList(java.util.List) call –>

      <property name = “ctryList”>

         <list>

            <value>India</value>

            <value>South Africa</value>

            <value>USA</value>

            <value>England</value>

         </list>

      </property>

<!– results in a setctrySet(java.util.Set) call –>

      <property name = “ctrySet”>

         <set>

           <value>India</value>

            <value>South Africa</value>

            <value>USA</value>

            <value>England</value>

         </set>

      </property>

<!– results in a setctryMap(java.util.Map) call –>

      <property name = “ctryMap”>

         <map>

            <entry key = “1” value = “India”/>

            <entry key = “2” value = “South Africa”/>

            <entry key = “3” value = “USA”/>

            <entry key = “4” value = “England”/>

         </map>

      </property>

      <!– results in a setctryProp(java.util.Properties) call –>

      <property name = “ctryProp”>

         <props>

            <prop key = “one”>India</prop>            

            <prop key = “two”>South Africa</prop>

            <prop key = “three”>USA</prop>

            <prop key = “four”>England</prop>

         </props>

      </property>

   </bean> 

</beans>

—————————————————————————————————— 

Example 3:

RankCollection.java

package com.myorg;

import java.util.*;

public class RankCollection {

   List rnkList;

   Set  rnkSet;

   Map  rnkMap;

   Properties rnkProp;

// a setter method to set List

   public void setrnkList(List rnkList) {

      this.rnkList = rnkList;

   }

   // prints and returns all the elements of the list.

   public List getrnkList() {

      System.out.println(“List Elements :”  + rnkList);

      return rnkList;

   }

   // a setter method to set Set

   public void setrnkSet(Set rnkSet) {

      this.rnkSet = rnkSet;

   }

   // prints and returns all the elements of the Set.

   public Set getrnkSet() {

      System.out.println(“Set Elements :”  + rnkSet);

      return rnkSet;

   }

   // a setter method to set Map

   public void setrnkMap(Map rnkMap) {

      this.rnkMap = rnkMap;

   }

   // prints and returns all the elements of the Map.

   public Map getrnkMap() {

      System.out.println(“Map Elements :”  + rnkMap);

      return rnkMap;

   }

   // a setter method to set Property

   public void setrnkProp(Properties rnkProp) {

      this.rnkProp = rnkProp;

   }

   // prints and returns all the elements of the Property.

   public Properties getrnkProp() {

      System.out.println(“Property Elements :”  + rnkProp);

      return rnkProp;

   }

}

—————————————————————————————————— 

TestSpring.java

package com.myorg;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class TestSpring {

   public static void main(String[] args) {

      ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”);

      RankCollection sc=(RankCollection)context.getBean(“RankCollection”);

sc.getrnkList();

      sc.getrnkSet();

      sc.getrnkMap();

      sc.getrnkProp();

   }

}

—————————————————————————————————— 

Bean.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”

   xsi:schemaLocation = “http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>

<!– Definition for javaCollection –>

   <bean id = “RankCollection” class = “com.myorg.RankCollection”>

      <!– results in a setrnkList(java.util.List) call –>

      <property name = “rnkList”>

         <list>

            <value>A+</value>

            <value>A</value>

            <value>B</value>

            <value>C</value>

         </list>

      </property>

<!– results in a setrnkSet(java.util.Set) call –>

      <property name = “rnkSet”>

         <set>

           <value>A+</value>

            <value>A</value>

            <value>B</value>

            <value>C</value>

         </set>

      </property>

     <!– results in a setrnkMap(java.util.Map) call –>

      <property name = “rnkMap”>

         <map>

            <entry key = “1” value = “A+”/>

            <entry key = “2” value = “A”/>

            <entry key = “3” value = “B”/>

            <entry key = “4” value = “C”/>

         </map>

      </property>

       <!– results in a setrnkProp(java.util.Properties) call –>

      <property name = “rnkProp”>

         <props>

            <prop key = “one”>A+</prop>            

            <prop key = “two”>A</prop>

            <prop key = “three”>B</prop>

            <prop key = “four”>C</prop>

         </props>

      </property>

   </bean> 

</beans>

 

Author:

Mr. Sachin Patil | Sr. Technical Trainer
SevenMentor Pvt. Ltd.

 

Call the Trainer and Book your free demo Class for JAVA now!!!

call icon

© Copyright 2020 | Sevenmentor Pvt Ltd.

 

Submit Comment

Your email address will not be published. Required fields are marked *

*
*