Dependency Injection in Spring framework

Before understanding Dependency Injection(DI) in Spring framework, I recommend you to understand Dependency Injection design pattern

As explained in above link, Dependency Injection is a design pattern in which object's properties are set by external entity. External entity can be simple java class which configure object's properties. Spring core (IoC) container acts as external entity to configure object's properties.

There are 2 ways in Spring framework to achieve DI.
1. Setter Injection
2. Constructor Injection

Let's write simple example to understand DI in Spring framework. We will set primitive values in Employee class using setter injection.

I have used maven to add Spring framework related jars. You can add these external jars in eclipse by right click on project --> Build Path --> Configure Build Path... --> Add external JARs --> select jar --> Ok. 

Project Structure: 

POM.xml: 

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://maven.apache.org/POM/4.0.0" 
xsi:schemalocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelversion>4.0.0</modelversion>

 <groupid>com.javagladiator.core</groupid>
 <artifactid>spring-core-trainning</artifactid>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>spring-core-trainning</name>
 <url>http://maven.apache.org</url>

 <properties>
  <project .build.sourceencoding="">UTF-8</project>
 </properties>

 <dependencies>
  <dependency>
   <groupid>org.springframework</groupid>
   <artifactid>spring-core</artifactid>
   <version>4.3.11.RELEASE</version>
  </dependency>
  <dependency>
   <groupid>org.springframework</groupid>
   <artifactid>spring-context</artifactid>
   <version>4.3.11.RELEASE</version>
  </dependency>

 </dependencies>
</project>


application.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">

 <bean id="employee" 
 class="com.javagladiator.spring.core.trainning.Employee">
  <property name="empId" value="1001" />
  <property name="name" value="Parthiv Singh" />
  <property name="salary" value="75000" />
 </bean>
</beans>

Employee class:
package com.javagladiator.spring.core.trainning;

public class Employee {
 private Integer empId;
 private String name;
 private Double salary;
 public Integer getEmpId() {
  return empId;
 }
 public void setEmpId(Integer empId) {
  this.empId = empId;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public Double getSalary() {
  return salary;
 }
 public void setSalary(Double salary) {
  this.salary = salary;
 }

 @Override
 public String toString() {
  return "Employee [empId=" +
    empId + ", name=" + 
    name + ", salary=" + 
    salary + "]";
 }
}


App class: This class is client for our spring application. It loads bean by id. Please see below example-


package com.javagladiator.spring.core.trainning;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.
ClassPathXmlApplicationContext;

public class App 
{
    public static void main( String[] args ){    
     ApplicationContext applicationContext = 
     new ClassPathXmlApplicationContext("application.xml"); 
     Employee employee = 
    (Employee) applicationContext.getBean("employee");
     System.out.println(employee);
    }
}

Output: 
Employee [empId=1001, name=Parthiv Singh, salary=75000.0]

Comments

Post a Comment

Popular posts from this blog

How to define Auxiliary Constructor in case class in Scala

equals() method of Object class in Java