Let me write a piece of code which shows completely loosely coupled, Then you can easily understand how Spring core manages the dependency internally. Consider a scenario, Online business Flipkart is there, it uses sometimes DTDC or Blue Dart courier service, So let me design an application which shows complete loosely coupled. The
Eclipse Directory as follows:

//Interface

package com.sdp.component;

public interface Courier {

public String deliver(String iteams,String address);

}

//implementation classes

package com.sdp.component;

public class BlueDart implements Courier {



public String deliver(String iteams, String address) {

return iteams+ "Shiped to Address "+address +"Through BlueDart"; 
}

}
package com.sdp.component;

public class Dtdc implements Courier {

public String deliver(String iteams, String address) {

return iteams+ "Shiped to Address "+address +"Through Dtdc";                    }

}

//Component classes

package com.sdp.service;

import com.sdp.component.Courier;

public class FlipKart {

private Courier courier;

public void setCourier(Courier courier) { 
          this.courier = courier;

}

public void shopping(String iteams,String address) 
{

String status=courier.deliver(iteams, address); System.out.println(status);

}

}

//Factory classes to create and return Object package com.sdp.util;

import java.io.IOException; 
import java.util.Properties;

import com.sdp.component.Courier;

public class ObjectFactory {

private static Properties props; 
static{

props=new Properties(); 
try {

props.load(ObjectFactory.class.getClassLoader().getResourceAsStream("com//sdp//common//app.properti 
es"));

} catch (IOException e) {

// TODO Auto-generated catch block e.printStackTrace();

}

}

public static Object getInstance(String logicalclassName) 
{

Object obj = null;

String originalclassName=props.getProperty(logicalclassName); 
try {

obj=Class.forName(originalclassName).newInstance(); } catch (InstantiationException e) {

// TODO Auto-generated catch block e.printStackTrace();

} catch (IllegalAccessException e) { 
          // TODO Auto-generated catch block

e.printStackTrace();

} catch (ClassNotFoundException e) { 
          // TODO Auto-generated catch block

e.printStackTrace();

}

return obj; 
}

}

//properties file

BlueDart.class=com.sdp.component.BlueDart 
Dtdc.class=com.sdp.component.Dtdc 
FlipKart.class=com.sdp.service.FlipKart

//Test class

package com.sdp.test;

import com.sdp.component.Courier; 
import com.sdp.service.FlipKart; 
import com.sdp.util.ObjectFactory;

public class FlipKartTest {

public static void main(String[] args) {

Courier courier=(Courier)ObjectFactory.getInstance("Dtdc.class");

FlipKart flipkart=(FlipKart)ObjectFactory.getInstance("FlipKart.class"); flipkart.setCourier(courier);

flipkart.shopping("Hp Laptop", "SR Nagar,Hyderabad");

}

}

If we write this code then we can manually achieve loose coupling, this is applicable if all the classes want either BlueDart or Dtdc, But if some class want BlueDart and some other classes want Dtdc then again it will be tightly coupled.

So instead of we creating and managing the dependency injection Spring core takes the responsibility of creating and managing the beans, Hope This will help, in next example we will see the  1st application on Spring core with details.

 

<< Introduction to Spring Core

Spring Expression Language (SpEL) >>

By Tell Me How

It is a technology blog and admin has excellent experience in programming from 5+ year. You can contact us at ceo.tellmehow@gmail.com

Share your thoughts

Leave a Reply

Loading Facebook Comments ...
Loading Disqus Comments ...