Monday, March 24, 2014

Dependency Injection

" If The Underlying Technology or server or run-time environment or Framework is assigning dependent values to resources Dynamically even though the resources are not satisfying any contract,then it is called Dependency Injection".

This type of approach is also called Inversion of Control.

Majorly there are 2 types of Dependency-Injections.
                             
  • Setter Injection.                                                       
  • Constructor Injection.

setter Injection:
  •  In setter Injection approach we use setter & getter Methods to assign dependent values to resources dynamically. 
  • To Configure these methods in spring configuration file we use '<property>' tag.
  • These setter methods will be called after Constructor  creation of that resource(POJO) class.
Eg configuration:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"................>

 <bean id="fb" class="firstbean">

<property name="msg" value="hai..!"/>
(or)<!-- You Can Use Either one of these ways. -->
<property name="msg">  
<value>     hello    </value>
</property>

</bean>
</beans>

in the above example   'msg'  is the name of the property of resource.

Example application on spring setter injection.
In this application i have
jar files:
commons-logging.jar
spring-beans-3.2.2.RELEASE.jar
spring-context-3.2.2.RELEASE.jar
spring-core-3.2.2.RELEASE.jar
spring-expression-3.2.2.RELEASE.jar

You can get all the spring jars from spring 3.2.2 software and the commons-logging from spring 2.5.

UserImpl class:
It Contains Business Logic.

package com.javagreat.springSI;

import java.util.Calendar;
import com.javagreat.springSI.Interface.User;

public class UserImpl implements User {
String msg;

/**
* @return the msg
*/
public String getMsg() {
return msg;
}

/**
* @param msg the msg to set
*/
public void setMsg(String msg) {
this.msg = msg;
}

@Override
public void displayMsg(String user_name) {
Calendar c=Calendar.getInstance();
int h=c.get(Calendar.HOUR_OF_DAY);
if(h<12){
System.out.println(msg+" Good Morning "+user_name);
}else if(h<16){
System.out.println(msg+" Good Afternoon "+user_name);
}else if(h<20){
System.out.println(msg+" Good Evening "+user_name);
}else {
System.out.println(msg+" Good Night "+user_name);
}
}
}


Spring Configuration file:
To Configure Beans.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
.......................>

<bean id="userbean" class="com.javagreat.springSI.UserImpl">
<!-- <property name="msg">
<value>Hello...!</value>
</property>
-->
<!-- You Can Use Either one of these ways. -->
<property name="msg" value="Hai...!"></property>
</bean>
</beans>

Userdemo class:
This is to run the Application.

package com.javagreat.springSI;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.javagreat.springSI.Interface.User;

public class UserDemo {
public static void main(String s[]){
ApplicationContext context=new FileSystemXmlApplicationContext("/Context.xml");
User ui= (UserImpl) context.getBean("userbean");
ui.displayMsg("raju");
}
}

With this Application we can inject dependent values to resource from Outside(xml file). In This demo application we injected a string value But,in real time we need to inject 'Datasource','connection'  etc objects.

Constructor Injection:

  • In Constructor Injection we use parameterized  constructors to assign dependent values to resources.
  • To pass dependent values to these parameterized  constructors we use '<constructor-arg>' in spring configure   file. 
  • In Constructor Injection dependent values will be assigned to resources at the time of object creation.
Example Configuration:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"................>

 <bean id="fb" class="firstbean">

<constructor-arg  value=''hello"/>

(or)<!-- You Can Use Either one of these ways. -->

<constructor-arg >
<value>     hai    </value>
</constructor-arg>

</bean>
</beans>

When Spring Container reads the above data it generates a 1-param constructor and assigns hello/hai to property of the 'firstbean' class.

Constructor-Injection based example application is contains  same files as setter-injection based application.But small changes are need in spring configuration file and  UserImpl class.

UserImpl class:
It Contains Business Logic.

package com.javagreat.springSI;

import java.util.Calendar;
import com.javagreat.springSI.Interface.User;

public class UserImpl implements User {
String msg;

public UserImpl(String msg){
this.msg=msg;
}//This is the only change needed in this file.

@Override
public void displayMsg(String user_name) {
Calendar c=Calendar.getInstance();
int h=c.get(Calendar.HOUR_OF_DAY);
if(h<12){
System.out.println(msg+" Good Morning "+user_name);
}else if(h<16){
System.out.println(msg+" Good Afternoon "+user_name);
}else if(h<20){
System.out.println(msg+" Good Evening "+user_name);
}else {
System.out.println(msg+" Good Night "+user_name);
}
}
}

Spring Configuration file:
To Configure Beans.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
.......................>

<bean id="userbean" class="com.javagreat.springSI.UserImpl">
<!-- <constructor-arg>
<value>Hello...!</value>
</constructor-arg>
-->
<!-- You Can Use Either one of these ways. -->
<constructor-arg value="Hai...!"/>
</bean>
</beans>

Except these 2 changes Everything is same as above example. If you run the above application with these changes you can assign dependent value through constructor.

0 comments:

Post a Comment