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:
App class: This class is client for our spring application. It loads bean by id. Please see below example-
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 + "]"; } }
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]
Helpful post, I would like to read more about dependency injection.
ReplyDeleteSpring Training in Chennai | Hibernate Training in Chennai | Struts Training in Chennai
Great, this article is quite awesome and I have bookmarked this page for my future reference.
ReplyDeleteTally course in Chennai
Tally classes in Chennai
Tally Training in Chennai
ccna course in Chennai
PHP Training in Chennai
Salesforce Training in Chennai
Web Designing Training in Chennai
Tally Course in Vadapalani
Tally Course in Anna Nagar
Tally Course in Velachery
I have read your blog its very attractive and impressive. I like it your blog
ReplyDeleteCyber Security Training Course in Chennai | Certification | Cyber Security Online Training Course | Ethical Hacking Training Course in Chennai | Certification | Ethical Hacking Online Training Course | CCNA Training Course in Chennai | Certification | CCNA Online Training Course | RPA Robotic Process Automation Training Course in Chennai | Certification | RPA Training Course Chennai | SEO Training in Chennai | Certification | SEO Online Training Course
Thanks for this blog keep sharing your thoughts like this...
ReplyDeleteWhat is Spring
Features of Spring Framework