Dependency Injection In Spring using constructor
* Dependency Injection refers to remove the dependency and make the code loosely coupled so that it is easy to test the code.
In such a case, we provide the information from the external source such as XML file. It makes our code loosely coupled and easier for testing. In such cases, we write the code as:
Two types of dependency injection are there.
1.Constructor Injection
2.Setter Injection
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Constructor Injection (primitive type injection)
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
This is the base class(abc.java)
-------------------------------------
package com.sandeep.springDemo;
public class abc {
//String name;
int id;
public abc()
{
System.out.println("I am in abc");
}
public abc(int id)
{
//this.name=name;
this.id=id;
}
public void PrintDetail()
{
//System.out.println("name is" +name);
System.out.println("Id is "+id);
}
}
-----------------------------
explanation(Background work by spring):-
------------------------------
In such a case, we provide the information from the external source such as XML file. It makes our code loosely coupled and easier for testing. In such cases, we write the code as:
Two types of dependency injection are there.
1.Constructor Injection
2.Setter Injection
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Constructor Injection (primitive type injection)
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
This is the base class(abc.java)
-------------------------------------
package com.sandeep.springDemo;
public class abc {
//String name;
int id;
public abc()
{
System.out.println("I am in abc");
}
public abc(int id)
{
//this.name=name;
this.id=id;
}
public void PrintDetail()
{
//System.out.println("name is" +name);
System.out.println("Id is "+id);
}
}
Main Class (MyApp.java)
-----------------------------------------------------------------------------------------------------
package com.sandeep.springDemo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyApp {
public static void main(String[] args)
{
ClassPathXmlApplicationContext context=
new ClassPathXmlApplicationContext("applicationContext.xml");
abc abc=context.getBean("MyABC",abc.class);
abc.PrintDetail();
context.close();
}
}
Spring configuration (applicationContext.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Define your beans here -->
<bean id="MyABC"
class="com.sandeep.springDemo.abc">
<constructor-arg value="25" type="int" />
</bean></beans>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
In the above code when we execute the MyApp.java ,it will show the output Id is 25.
-----------------------------
explanation(Background work by spring):-
------------------------------
<bean id="MyABC"
class="com.sandeep.springDemo.abc">
<constructor-arg value="25" type="int" />
</bean></beans>
In the above code,
<bean id="MyABC" class="com.sandeep.springDemo.abc">
is simillar to
abc MyABC=new abc();
In the above code,
<bean id="MyABC" class="com.sandeep.springDemo.abc">
is simillar to
abc MyABC=new abc();
<bean id="MyABC"
class="com.sandeep.springDemo.abc">
<constructor-arg value="25" type="int" />
</bean></beans>
is simillar to
abc MyABC=new abc(25);
If we did it normally then MyApp.java would contain
is simillar to
abc MyABC=new abc(25);
If we did it normally then MyApp.java would contain
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
package com.sandeep.springDemo;
public class MyAppNormal {
public static void main(String[] args) {
abc a=new abc(25);
a.PrintDetail();
}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
In the above code, we are hardcoding the object which is tightly coupled.Our intension is to make it loosely coupled using spring framework.
#Dependency Injection
#DependencyInjectionUsingSpring
#constructorDependencyInjection
#DependencyInjectionUsingConstructor
#constructorDependencyInjection
#DependencyInjectionUsingConstructor
Comments
Post a Comment