Interview Questions

Spring - Dependency Injection

Spring’s Core basically follows the Dependency Injection (DI) principle.

Generally, each object is responsible for obtaining its own references to the objects it collaborates with its dependencies. This can lead to tightly coupled and hard-to-test code.

When applying Dependency Injection, objects are given their dependencies at creation time by some external entity that coordinates each object in the system. In other words, dependencies are injected into objects. So, DI means an inversion of responsibility with regard to how an object obtains references to collaborating objects.

There are three types of Injection to objects
1. Constructor Injection
2. Setter Injection
3. Method Injection

In Spring framework, the objects to be created are declared in Spring’s configuration file and at run time Spring’s Container creates those objects.
Consider the following class Employee.

Employee.java

package in.techdive.bean;

public class Employee
{
        private long    empId;

        private String  empName;

        private int        age;

        private long    salary;

        private long    deptId;

        public Employee()
        {

        }

        public Employee(long empId, String empName, long deptId)
        {
                this.empId = empId;
                this.empName = empName;
                this.deptId = deptId;
        }

        public long getEmpId()
        {
                return empId;
        }

        public void setEmpId(long empId)
        {
                this.empId = empId;
        }

        public String getEmpName()
        {
                return empName;
        }

        public void setEmpName(String empName)
        {
                this.empName = empName;
        }

        public int getAge()
        {
                return age;
        }

        public void setAge(int age)
        {
                this.age = age;
        }

        public long getSalary()
        {
                return salary;
        }

        public void setSalary(long salary)
        {
                this.salary = salary;
        }

        public long getDeptId()
        {
                return deptId;
        }

        public void setDeptId(long deptId)
        {
                this.deptId = deptId;
        }
}

The above class contains certain properties like empId, empName , deptId etc,. To create an object for the same class we need to create a spring configuration file and define a bean for Employee class as follows.

Application-Context.xml



<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-2.5.xsd">

<bean id="employee1" class="in.techdive.bean.Employee"/>

<bean id="employee2" class="in.techdive.bean.Employee">
<property name="empId">1001</property>
<property name="empName">Edwards</property>
<property name="salary">10000</property>
<property name="age">32</property>
<property name="deptId">405</property>
</bean>

<bean id="employee3" class="in.techdive.bean.Employee">
<constructor-arg index="0"><value>1004</value></constructor-arg>
<constructor-arg index="1"><value>Richards</value></constructor-arg>
<constructor-arg index="2"><value>406</value></constructor-arg>
</bean>

</beans>


As you see the configuration file we have three beans with different Ids.
1. First bean with no argument constructor.
2. Second bean with no-argument constructor and all properties set using setter injection.
3. Third bean with constructor injection. You can use any form of dependency injection as applicable.

To run the application, use the following code.

public static void main(String[] args)
{
        ApplicationContext ctx = new FileSystemXmlApplicationContext("Application-Context.xml");

        Employee employee1 = (Employee) ctx.getBean("employee1");
        Employee employee2 = (Employee) ctx.getBean("employee2");
        Employee employee3 = (Employee) ctx.getBean("employee3");
}

When the application is run, Spring container starts up and loads all the beans specified in the configuration file. By default all the bean created are singleton.

The ApplicationContext object that we have used in the above code is nothing more than a bean factory which provides our required beans on request. In this way objects are created and maintained by spring container in a spring based application.

Since the developer need not worry about creating, injecting dependencies and maintaining objects in the application, the concept of Dependency Injection is also known as Inversion of Control(IOC), as the control of object creation is moved from developer to the Spring container.