Senin, 22 November 2010

Spring : Depedency Injection Framework

Spring merupakan salah satu DI (Depedency Injection) Framework yang sukses dipakai dalam pembangunan Software berbasis Java.
Spring menjembatani class yang berkaitan erat dengan class dependency-nya melalui konfigurasi sehingga lebih loosely couple / tidak terikat.

Berikut contoh penggunaan Spring sebagai DI framework
Class Car dan Plane merupakan parameter constructor untuk class TransportationTest. Dengan menggunakan file external yang dimanage oleh Spring maka keterikatan class TransportationTest dapat dikonfigurasi di file external(xml).

public abstract class Vehicle { 
 public abstract void move(); 
}

public class Car extends Vehicle{ 
 public void move() {
  System.out.println("Driving fast..");  
 } 
}

public class Plane extends Vehicle{ 
 public void move() {
  System.out.println("Flying fast");  
 }
}

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class TransportationTest {
 private Vehicle vehicle;
 
 public TransportationTest(Vehicle vehicle) {
  this.vehicle = vehicle;
 }
 
 public void testVehicle() {
  vehicle.move();
 }
 
 public static void main(String[] args) {
  /*  
  // dependency injection tanpa spring menyebabkan 
  // class TransportationTest terikat erat dengan class Car
  // pada saat instantiation  
  Car car = new Car();
  TransportationTest test = new TransportationTest(car);
  test.testVehicle();
  
  // dependency injection tanpa spring menyebabkan 
  // class TransportationTest terikat erat dengan class Plane
  // pada saat instantiation  
  Plane plane = new Plane();
  TransportationTest test2 = new TransportationTest(plane);
  test2.testVehicle();  
  */
  
  // load spring context
  AbstractApplicationContext context = new FileSystemXmlApplicationContext(
    new String[] {"ApplicationContext.xml"});
  
  // gunakan method ini utk mematikan spring container 
  // di lingkungan pengembangan desktop 
  context.registerShutdownHook();
  
  final BeanFactory bf = (BeanFactory)context;
  
  // dependency injection versi spring tidak menyebabkan 
  // class TransportationTest terikat erat dengan class yang 
  // menjadi parameter contructornya (Car ataupun Plane).
  // Parameter constructor dikonfigurasi di file yang di-manage oleh Spring
  TransportationTest test1 = (TransportationTest) bf.getBean("transportationTest1");  
  test1.testVehicle(); 
  
  TransportationTest test2 = (TransportationTest) bf.getBean("transportationTest2");  
  test2.testVehicle();
 }
}

file configurasi : ApplicationContext.xml, letakan di direktori yang sama dengan class TransportationTest
<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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
    <bean id="car" class="Car" />
    <bean id="plane" class="Plane" />
    
    <bean id="transportationTest1" class="TransportationTest">
        <constructor-arg ref="car" />
    </bean>
    
    <bean id="transportationTest2" class="TransportationTest">
        <constructor-arg ref="plane" />
    </bean>        
</beans>

Tidak ada komentar:

Posting Komentar