Spring 1
Spring 1
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
Dependency Injection (DI) is a design pattern that removes the dependency from
the programming code so that it can be easy to manage and test the application.
Dependency Injection makes our programming code loosely coupled. To understand
the DI better, Let's understand the Dependency Lookup (DL) first:
Dependency Lookup
The Dependency Lookup is an approach where we get the resource after demand.
There can be various ways to get the resource for example:
A obj = new AImpl();
In such way, we get the resource(instance of A class) directly by new keyword.
Another way is factory method:
A obj = A.getA();
This way, we get the resource (instance of A class) by calling the static factory
method getA().
Alternatively, we can get the resource by JNDI (Java Naming Directory Interface) as:
There can be various ways to get the resource to obtain the resource. Let's see the
problem in this approach.
class Employee{
Address address;
Employee(Address address){
this.address=address;
}
public void setAddress(Address address){
this.address=address;
}
In such case, instance of Address class is provided by external souce such as XML
file either by constructor or setter method.
Two ways to perform Dependency Injection in Spring framework
Spring framework provides two ways to inject dependency
By Constructor
By Setter method
void show(){
System.out.println(id+" "+name);
}
}
applicationContext.xml
We are providing the information into the bean by this file. The constructor-arg
element invokes the constructor. In such case, parameterized constructor of int
type will be invoked. The value attribute of constructor-arg element will assign the
specified value. The type attribute specifies that int parameter constructor will be
invoked.
<?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">
</beans>
Test.java
This class gets the bean from the applicationContext.xml file and calls the
show method.
package com.javatpoint;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.*;
Employee s=(Employee)factory.getBean("e");
s.show();
}
}
Injecting string-based values
If you don't specify the type attribute in the constructor-arg element,
by default string type constructor will be invoked.
....
<bean id="e" class="com.javatpoint.Employee">
<constructor-arg value="10"></constructor-arg>
</bean>
....
If you change the bean element as given above, string parameter
constructor will be invoked and the output will be 0 10.
Output:0 10
You may also pass the string literal as
following:
....
<bean id="e" class="com.javatpoint.Employee"
>
<constructor-
arg value="Sonoo"></constructor-arg>
</bean>
....
Output:0 Sonoo
You may pass integer literal and string both as following
....
<bean id="e" class="com.javatpoint.Employee">
<constructor-arg value="10" type="int" ></constructor-arg>
<constructor-arg value="Sonoo"></constructor-arg>
</bean>
....
Output:10 Sonoo