Reading:  

Quick to the point introduction to Spring Framework


Dependency Injection

The dependency Injection is a form that removes the dependency of the programs, which helps to gluing these classes together and at the same time keeping them independent. In Java Application classes should be as independent as possible of other classes to increase the possibility to reuse these classes and to test them independently of other classes while performing unit testing.

Advantages of Dependency Injection:

  • Responsibility of separation.
  • Loosely couple architecture.
  • Using configuration, without changing the dependent code, a different implementation can be supplied Configuration and code is separate.
  • By using mock objects testing can be performed.

Dependency Injection Types:

The classifications of Dependency injection are:

  • Constructor Injection
  • Setter Injection

Constructor Injection:

Constructor-based DI is accomplished when the container invokes a class constructor with a number of arguments, each representing a dependency on other class.

Example:

Till now we have seen many example without using constructor, let us see the example using constructor arguments:

To avoid ambiguity exist while arguments passing to the constructor in case there are more than single parameter. In which the constructor arguments are defined in a definition of bean is the order in which those arguments are supplied to the constructor.

Student.java file:

package pack1;  
  
public class Student {  
private int Stud_ id;  
private String sname;  
  
public Student() {
System.out.println("Inside default constructor");
      }  
  
public Student(int Stud_id) {
     this.Stud_id = Stud_id;
     }  
  
public Student(String sname) {  
this.sname = sname;
     }  
  
public Student(int Stud_id, String sname) {  
    this.Stud_id = Stud_id;  
    this.sname = sname;  
}  
  
void display(){  
   System.out.println(“Student id and name :+ Stude_id+" "+sname);  
    }  
}  
 

SampleTest.java file:

package pack1;  
  
import org.springframework.beans.factory.BeanFactory;  
import org.springframework.beans.factory.xml.XmlBeanFactory;  
import org.springframework.core.io.*;  
  
public class SampleTest {  
    public static void main(String[] args) {  
          
        Resource res = new ClassPathResource("Bean.xml");  
        BeanFactory bean = new XmlBeanFactory(res);  
          
        Student st=(Student)bean.getBean("bean");  
        st.display();  
          
    }  
}  
 

Bean.xml file:

<?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="bean" class="pack1.Student">  
<constructor-arg value="12" type="int"></constructor-arg>  
<constructor-arg value="Nimrit"></constructor-arg>  
    </bean>  
</beans>

 

Compile and run the program will get the below result:

Result:

Inside default constructor
Student id and name : 12 Nimrit

 

Dependency Injection by setter method:

Setter-based DI is accomplished by the container calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean.

Example:

Student.java file:

package pack1;  
  
public class Student {  
private int Stud_id;  
private String sname;  
private String college;  
  
public int getId() {  
    return Stud_id;  
}  
public void setId(int Stud_id) {  
    this.Stud_id = Stud_id;  
}  
public String getName() {  
    return sname;  
}  
public void setName(String sname) {  
    this.sname = sname;  
}  
  
public String getCollege() {  
    return college;  
}  
public void setCity(String college) {  
    this.college = college;  
}  
void display(){  
    System.out.println("Student details: "+Stud_id+" "+sname+" "+college);  
    }   
}  
 

SampleTest.java file:

package pack1;  
  
import org.springframework.beans.factory.BeanFactory;  
import org.springframework.beans.factory.xml.XmlBeanFactory;  
import org.springframework.core.io.*;  
  
public class SampleTest {  
    public static void main(String[] args) {  
          
        Resource res = new ClassPathResource("Bean.xml");  
        BeanFactory bean = new XmlBeanFactory(res);  
          
        Student st=(Student)bean.getBean("bean");  
        st.display();  
          
    }  
}  
 

 

Bean.xml file:

User can provide the data by this xml file in to bean. The element of property invokes the method setter. The sub-element value of property will assign the particular value.

<?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="bean" class="pack1.Student">  
<property name="Stud_id">  
<value>11</value>  
</property>  
<property name="sname">  
<value>Nimrit</value>  
</property>  
<property name="college">  
<value>Melbourne Uni</value>  
</property>  
   </bean>  
</beans>
 

Compile and run the program will get the below result:

Result:

Student details: 11  Nimrit Melbourne Uni

 

 

Description

This tutorial covers various topics releated to Spring Framework as listed below

  • Framework Overview
  • Modules
  • Environment Setup
  • Saying Hello World with Spring
  • IoC Containers
  • Bean Definition
  • Bean Scopes 
  • Bean Life Cycle
  • Bean Post Processors
  • Bean Definition Inheritance
  • Dependency Injection
  • Injecting Inner Beans
  • Injecting Collection
  • Beans Auto-Wiring
  • Annotation Based Configuration
  • Java Based Configuration
  • Event Handling in Spring
  • Custom Events in Spring
  • JDBC Framework Overview
  • Transaction Management

 

 



Prerequisites

Prior knowledge of Java is essential

Audience

Beginners or students seeking quick introduction to Spring

Learning Objectives

This tutorial is for beginners seeking quick introduction to Spring Framework.

Author: Subject Coach
Added on: 22nd Jun 2015

You must be logged in as Student to ask a Question.

None just yet!