Understanding Spring

Understanding Spring Dependency Injection


Many people think why spring,what is the usefulness if we use spring,why spring came and why it became populer for only dependency injection..
Here I am giving a simple example in which we are not using spring .i mean we are using core java only

  In the below example i have a interface  called Bean .java with two implementation class known as HelloBean.java and DemoBean.java.

Bean.java(Interface)

package com.student.spring.bean;
 
public interface Bean
{
 public void displayUser();
}

HelloBean.java(class)
package com.student.spring.impl;
 
import com.student.spring.Bean;
 
public class HelloBean implements Bean
{
 public void displayUser(){
  System.out.println("Hey i m free user");
 }
}
 

DemoBean.java (class)

package com.student.spring.impl;
 
import com.student.spring.Bean;
 
public class DemoBean implements Bean
{
 public void displayUser(){
  System.out.println("I m  commercial user");
 }
}
 
Here i m calling the method of interface via implementation classes object .

MyApp.java

package com.student.spring.main;
 
import com.student.spring.impl.*;
 
import com.student.spring.Bean;

 
 public class MyApp 
{
    public static void main( String[] args )
    {
     Bean bean1 = new HelloBean();
     bean.displayUser();
     Bean bean2=new DemoBean();
      bean2.displayUser(); 
    }
}
 
output will be
 Hey i m free user
I m  commercial user 
 
 
Instead of calling directly i can call  via  a helper to make flexible like..

AdopterBean.java

package com.student.spring.adopter;
 
import com.student.spring.Bean;
import com.student.spring.impl.*;
 
public class AdopterBean
{
 Bean bean1;
 Bean bean2;
 public AdopterBean(){
  bean1 = new DemoBean();
  bean2=new HelloBean(); 
 }
 
 public void showUser(){
  bean1.displayUser();
  bean2.displayUser(); 
 }
 
}
 
I can create the helper class object and can  call the helper class method though 
i can call the interface method indirectly like ...

MyApp.java

package com.student.spring.main;
 
import com.student.spring.adopter.*;
 
public class MyApp 
{
    public static void main( String[] args )
    {
      AdopterBean adopter = new AdopterBean();
       adopter.showUser(); 
    }
}
The above example creates tight coupling ,low cohesion , crate maintainability and
flexibility problem when we will have more bean classes.  
 

The following example shows how we are passing dependency object to the dependent

reference ...


 
Bean.java(Interface)

package com.student.spring.bean;
 
public interface Bean
{
 public void displayUser();
}

HelloBean.java(class)
package com.student.spring.impl;  
import com.student.spring.Bean;
  public class HelloBean
 {
  public void displayUser()
 {
  System.out.println("Hey i m free user inside HelloBean class");
  }
  }

DemoBean.java (class)

package com.student.spring.impl;
import com.student.spring.Bean;
 
  public class DemoBean implements Bean
 {
  Bean bean1;    
  public void displayUser(Bean bean1)
 {
  this.bean1=bean1;
  bean1.displayUser();
  System.out.println(" Hey I m commercial user inside DemoBean class");
  }
  }
 MyApp.java
package com.student.spring.main;
 
import com.student.spring.impl.*;
 
import com.student.spring.Bean;

 
 public class MyApp 
{
    public static void main( String[] args )
    {
     Bean bean1 = new HelloBean();
     Bean bean2 =new DemoBean();
     bean2.displayUser(bean1) 
 }
}
 
 Spring says you do not create object and do not pass the object as a dependency.
Just configure that object in the spring configuration file,i will pass that object
to your java class via dependency injection.
 

Spring follows one Hollywood principle called "You do not call me,i will call you."

  In spring we just create the java class object in the form of bean and pass the dependency object via  the setter method to dependent .

 The above example we can write in spring as follows..
 
 
Bean.java(Interface)

package com.student.spring.bean;
 
public interface Bean
{
 public void displayUser();
}

HelloBean.java(class)
package com.student.spring.impl;
import com.student.spring.Bean;
  public class HelloBean
 {

   public void displayUser()
 {
  System.out.println("Hey i m free user inside HelloBean class");
  }
  }

DemoBean.java (class)

package com.student.spring.impl;
import com.student.spring.Bean;
 
  public class DemoBean implements Bean
 {
  Bean bean1;  //dependent
  public void setBean(Bean bean1)
  {
   this.bean1=bean1;
  }
  public void displayUser(Bean bean1)
 {
  bean1.displayUser();
  System.out.println(" Hey I m commercial user inside DemoBean class");
  }
  }
 
 

 MyApp.java
package com.student.spring.main; 
import org.springframework.context.support.*;
 import com.student.spring.impl.*; 
 import com.student.spring.Bean;
 public class MyApp 
{
 public static void main( String[] args )
 {
ApplicationContext  ac=new ClassPathXmlApplicationContext("applicationContect.xml");
  Bean bean=(Bean)ac.getBean("db");
 bean.displayUser();
 }
 }

applicationContext.xml 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC  "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>


<bean id="db" class="
com.student.spring.impl.DemoBean">
<property name="bean">
<ref bean="hb"/>
</property>
</bean>


<bean id="hb" class="com.student.spring.impl.HelloBean">



</bean>


<beans>


 
 
 
 
 
 
 

1 comment: