Saturday, 24 February 2018

4.0 Dependency Injection

































Dependency Injection is a fundamental aspect of the Spring framework, through which the Spring container "injects" objects into other objects or "dependencies". 
  
Simply put, this allows for loose coupling of components and moves the responsibility of managing components onto the container. 


Dependency injection - We want to drive car , we are only dealing with the intent(interface - object factory). Implementation doesn't matter for us. Mini car , micro car, prime car are the dependency injections which applicationContext.xml is injecting to our objectFactory.


There are two types of injection in Spring

  1. Constructor Injection
  2. Setter Injection

CONSTRUCTOR INJECTION
1. Define a dependency interface and class
2. Create a constructor in your class
3. Configure the dependency injection in config file.





















-------------------------------------------------------------------------------------

Interface.java
-------------------------------------------------------------------------------------
package com.spring.demo;

public interface Swap {

public void doSwap(int a,int b);
public void anotherLogic(int a, int b);
}



-------------------------------------------------------------------------------------
FirstMethodSwap.java
-------------------------------------------------------------------------------------
package com.spring.demo;

public class FirstMethodSwap implements Swap{

private Logic logic;
public FirstMethodSwap(Logic logic){
logic=this.logic;
}
public void doSwap(int a, int b) {
int c;
c=a;
a=b;
b=c;
System.out.println("Swap using 3rd variable : a = "+a+" and b= "+b);
}

public void anotherLogic(int a, int b) {
logic.anotherLogic(a, b);
}

}


-------------------------------------------------------------------------------------
SecondMethodSwap.java
-------------------------------------------------------------------------------------
package com.spring.demo;

public class SecondMethodSwap implements Swap{

private Logic logic;
public SecondMethodSwap(Logic thelogic){
logic=thelogic;
}
public void doSwap(int a, int b) {
a=a+b;
b=a-b;
a=a-b;
System.out.println("Swap using 2 variable : a = "+a+" and b= "+b);
}

public void anotherLogic(int a, int b) {
logic.anotherLogic(a,b);
}

}



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

<bean id="anotherSwap" class="com.spring.demo.AnotherLogic">
</bean>

<bean id="Swapme" class="com.spring.demo.SecondMethodSwap">
<constructor-arg ref="anotherSwap" />
</bean>



</beans>



-------------------------------------------------------------------------------------
Logic
-------------------------------------------------------------------------------------
package com.spring.demo;

public interface Logic {
public void anotherLogic(int a,int b);

}


-------------------------------------------------------------------------------------
AnotherLogic.java
-------------------------------------------------------------------------------------
package com.spring.demo;

public class AnotherLogic implements Logic{

public void anotherLogic(int a, int b) {
a=a*b;
b=a/b;
a=a/b;
System.out.println("Swap using 3rd variable one more logic : a = "+a+" b = "+b);
}

}


-------------------------------------------------------------------------------------
MyApp.java
-------------------------------------------------------------------------------------
package com.spring.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyApp {

public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Swap obj=(Swap) context.getBean("Swapme",Swap.class);
obj.doSwap(3, 5);
obj.anotherLogic(4, 5);
context.close();

}

}




-------------------------------------------------------------------------------------
OUTPUT
-------------------------------------------------------------------------------------
Feb 25, 2018 2:11:00 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Sun Feb 25 14:11:00 IST 2018]; root of context hierarchy
Feb 25, 2018 2:11:00 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Swap using 2 variable : a = 5 and b= 3
Swap using 3rd variable one more logic : a = 5 b = 3
Feb 25, 2018 2:11:01 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Sun Feb 25 14:11:00 IST 2018]; root of context hierarchy





SETTER INJECTION
1. Define a dependency interface and class
2. Create a setter method
3. Configure the dependency injection in config file.





















In the above mentioned codes do the below changes




-------------------------------------------------------------------------------------
DontSwap.java (add this new class)
-------------------------------------------------------------------------------------

package com.spring.demo;


public class DontSwap implements Swap {


private Logic logic;

public DontSwap(){
System.out.println("DontSwap - inside no-arg construtor");
}
public void doSwap(int a, int b) {
System.out.println("I don't swap : a = "+a+" b = "+b);
}

public void setLogic(Logic logic) {
System.out.println("DontSwap : inside setLogic setter");
this.logic = logic;
}
public void anotherLogic(int a, int b) {
logic.anotherLogic(a, b);
}


}


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

<bean id="anotherSwap" class="com.spring.demo.AnotherLogic">
</bean>

<bean id="Swapme" class="com.spring.demo.SecondMethodSwap">
<constructor-arg ref="anotherSwap" />
</bean>

<bean id="Dontswap" class="com.spring.demo.DontSwap">
<property name="logic" ref="anotherSwap"></property>
</bean>

</beans>



-------------------------------------------------------------------------------------
MyApp.java
-------------------------------------------------------------------------------------
package com.spring.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyApp {

public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Swap obj=(Swap) context.getBean("Swapme",Swap.class);
obj.doSwap(3, 5);
obj.anotherLogic(3, 5);
Swap obj1=(Swap) context.getBean("Dontswap", Swap.class);
obj1.doSwap(3, 5);
obj1.anotherLogic(3, 5);
context.close();

}

}



-------------------------------------------------------------------------------------
OUTPUT
-------------------------------------------------------------------------------------
Feb 25, 2018 2:11:00 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Sun Feb 25 14:11:00 IST 2018]; root of context hierarchy
Feb 25, 2018 2:11:00 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
DontSwap - inside no-arg constructor
DontSwap : inside setLogic setter
Swap using 2 variable : a = 5 and b= 3
Swap using 3rd variable one more logic : a = 5 b = 3
I don't swap : a = 3 b = 5
Swap using 3rd variable one more logic : a = 5 b = 3
Feb 25, 2018 2:11:01 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Sun Feb 25 14:11:00 IST 2018]; root of context hierarchy


























LITERALS SETTER INJECTION




-------------------------------------------------------------------------------------
DontSwap.java
-------------------------------------------------------------------------------------
package com.spring.demo;

public class DontSwap implements Swap {

private String email;
private String name;

private Logic logic;

public DontSwap(){
System.out.println("DontSwap - inside no-arg construtor");
}
public void doSwap(int a, int b) {
System.out.println("I don't swap : a = "+a+" b = "+b);
}

public String getEmail() {
return email;
}
public void setEmail(String email) {
System.out.println("DontSwap - inside setEmail setter");
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("DontSwap - inside setName setter");
this.name = name;
}
public void setLogic(Logic logic) {
System.out.println("DontSwap : inside setLogic setter");
this.logic = logic;
}
public void anotherLogic(int a, int b) {
logic.anotherLogic(a, b);
}



}


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

<bean id="anotherSwap" class="com.spring.demo.AnotherLogic">
</bean>

<bean id="Swapme" class="com.spring.demo.SecondMethodSwap">
<!-- Constructor injection -->
<constructor-arg ref="anotherSwap" />
</bean>

<bean id="Dontswap" class="com.spring.demo.DontSwap">
<!-- Setter injection -->
<property name="logic" ref="anotherSwap"></property>
<!-- literals injection -->
<property name="email" value="bikesh.kumar@coder.com"></property>
<property name="name" value="Bikesh Kumar"></property>
</bean>

</beans>



-------------------------------------------------------------------------------------
MyApp.java
-------------------------------------------------------------------------------------
package com.spring.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyApp {

public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Swap obj=(Swap) context.getBean("Swapme",Swap.class);
obj.doSwap(3, 5);
obj.anotherLogic(3, 5);
Swap obj1=(Swap) context.getBean("Dontswap", Swap.class);
obj1.doSwap(3, 5);
obj1.anotherLogic(3, 5);
DontSwap obj2=(DontSwap) context.getBean("Dontswap",DontSwap.class);
System.out.println(obj2.getEmail());
System.out.println(obj2.getName());
context.close();

}

}


-------------------------------------------------------------------------------------
OUTPUT
-------------------------------------------------------------------------------------Feb 25, 2018 3:10:32 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Sun Feb 25 15:10:32 IST 2018]; root of context hierarchy
Feb 25, 2018 3:10:32 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
DontSwap - inside no-arg construtor
DontSwap : inside setLogic setter
DontSwap - inside setEmail setter
DontSwap - inside setName setter
Swap using 2 variable : a = 5 and b= 3
Swap using 3rd variable one more logic : a = 5 b = 3
I don't swap : a = 3 b = 5
Swap using 3rd variable one more logic : a = 5 b = 3
bikesh.kumar@coder.com
Bikesh Kumar
Feb 25, 2018 3:10:32 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Sun Feb 25 15:10:32 IST 2018]; root of context hierarchy






LITERALS SETTER INJECTION  USING PROPERTY FILE





















-------------------------------------------------------------------------------------
bikesh.properties
-------------------------------------------------------------------------------------
foo.email=bikesh.kumar@coder.com
foo.name=Bikesh Kumar


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

<!-- load properties file : bikesh.properties -->
<context:property-placeholder location="classpath:bikesh.properties" />

<bean id="anotherSwap" class="com.spring.demo.AnotherLogic">
</bean>

<bean id="Swapme" class="com.spring.demo.SecondMethodSwap">
<!-- Constructor injection -->
<constructor-arg ref="anotherSwap" />
</bean>

<bean id="Dontswap" class="com.spring.demo.DontSwap">
<!-- Setter injection -->
<property name="logic" ref="anotherSwap"></property>
<!-- literals injection -->
<property name="email" value="${foo.email}"></property>
<property name="name" value="${foo.name}"></property>
</bean>

</beans>



-------------------------------------------------------------------------------------
OUTPUT
-------------------------------------------------------------------------------------
Feb 25, 2018 3:19:57 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Sun Feb 25 15:19:57 IST 2018]; root of context hierarchy
Feb 25, 2018 3:19:57 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
DontSwap - inside no-arg construtorf
DontSwap : inside setLogic setter
DontSwap - inside setEmail setter
DontSwap - inside setName setter
Swap using 2 variable : a = 5 and b= 3
Swap using 3rd variable one more logic : a = 5 b = 3
I don't swap : a = 3 b = 5
Swap using 3rd variable one more logic : a = 5 b = 3
bikesh.kumar@coder.com
Bikesh Kumar
Feb 25, 2018 3:19:58 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Sun Feb 25 15:19:57 IST 2018]; root of context hierarchy

No comments:

Post a Comment