Spring(혹은 전자정부프레임워크)에서 자주 사용되는 개념인 DI(Dependency Injection)에 대해서 간단한 예제를 통해 살펴보고자 한다.


DI란 '의존성 주입'이라고 번역이 되는데 표현 자체가 거창해서 그렇지 사실은 그동안 프로그래밍에서 사용되어 오던 것이다.

예를들어 업로드 파일을 서버에 저장하는 SaveFile이라는 클래스가 있을 때 저장되는 파일 이름을 모두 대문자로 변환해서 저장하는 ToUpper라는 클래스를 SaveFile에서 사용한다고 하면 SaveFile은 ToUpper 클래스에 의존되어 있다고 표현한다.


class SaveFile

{

private File file;

private ToUpper upper;


private void saveFile() {

//여기서 upper객체를 사용해서 파일이름을 대문자로 변환후 저장하는 코드

}

.......

}


이럴 경우 ToUpper 클래스를 SaveFile 클래스 내부에서 new로 생성해서 사용할수도 있지만 ToUpper 클래스를 외부에서 주입해서(이걸 DI라고 한다) 사용할수 있을 것이다.

가장 보편적인 방법은 SaveFile의 생성자에서 매개인자(파라미터)로 ToUpper 클래스의 객체를 받아서 사용하는 형태가 있을 것이다.


class SaveFile

{

private File file;

private ToUpper upper;


public SaveFile(ToUpper upper){ //생성자를 통해 의존성 주입(DI)하는 방식

this.upper = upper;

}


private void saveFile() {

//여기서 upper객체를 사용해서 파일이름을 대문자로 변환후 저장하는 코드

}

.......

}


그런데 이 방식외에 Spring 컨테이너의 도움을 받아서 XML 설정 파일의 property 태그를 이용하는 방법도 있다.

이때 사용되는 것은 setter 메소드이다.



class SaveFile

{

private File file;

private ToUpper upper;


//setter만드는 규약에 맞게 setter 메소드를 만들어 두면 Spring 컨테이너가 

//XML 설정 파일의 property 태그를 이용해서 ToUpper 클래스의 객체를

//아래 메소드의 파라미터인 mUpper에 주입(DI)해 준다.

//따라서 ToUpper 클래스의 객체를 new로 생성하지 않아도 정상적으로 사용할수 있는것이다.

//setter를 만드는 규칙은 XML 설정 파일의 property name가 upper로 되어 있는 것을 근거로

//upper의 첫 글자를 대문자로(U) 바꾼 후 앞에 set을 추가한 형태인 setUpper로 만들면

//Spring 컨테이너가 알아서 이 setter를 찾아서 해당 property의 ref가 지정한 bean을

//mUpper에 주입해 준다. 이것이 프라퍼티를 이용한 DI 방식이다.

public void setUpper(ToUpper mUpper) {

this.upper = mUpper;

}


private void saveFile() {

//여기서 upper객체를 사용해서 파일이름을 대문자로 변환후 저장하는 코드

}

.......

}


※ ToUpper 클래스의 내용은 생략(해당 클래스가 잘 만들어져 있다고 가정...)


아래는 XML 설정파일의 정보이다. 파일 이름이 MyBean.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"

   xsi:schemaLocation = "http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


   <!-- SaveFile 클래스를 임의 클래스에서 객체로 사용하도록 bean 정보 설정 -->

   <bean id = "mySaveFile" class = "com.joe.SaveFile">

      <!-- SaveFile 클래스의 setUpper()에게로 ref가 지정하는 bean(객체)를 Spring 컨테이너가 주입해준다

즉 SaveFile 클래스에서 setUpper()의 매개인자로 ref가 지정하는 bean을 주입해 준다.

즉 setUpper(myUpper)식이 되는 것이다.

 -->

      <property name = "upper" ref = "myUpper"/>

   </bean>


   <!-- SaveFile 클래스가 사용하게 될 ToUpper 클래스에 대한 bean -->

   <bean id = "myUpper" class = "com.joe.ToUpper"></bean>

</beans>


이제 사용하는 방법은 다음과 같다.


package com.joe;


import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class MainApp {

   public static void main(String[] args) {

      ApplicationContext context = new ClassPathXmlApplicationContext("MyBean.xml");


      SaveFile sf = (SaveFile) context.getBean("mySaveFile");

      sf.saveFile();

   }

}

+ Recent posts