관리 메뉴

JIHYUN JEONG

[스프링 프레임워크/Spring Framework] Spring Framework container Factory기능 예제 03 본문

Information Technology/Spring

[스프링 프레임워크/Spring Framework] Spring Framework container Factory기능 예제 03

StopHyun 2013. 3. 13. 18:04

[예제]

1 패키지 생성

 

2 GreetingServiceKO Class 생성 > 마찬가지고 GreetingServiceEn Class 생성

 

3 GreetingService Interface 생성

 

4 완성된 소스들

 

○ good 패키지의 경우

 

package com.asianaidt.greeting.good;

 

public interface GreetingService {

    

    // 인터페이스는 자동으로 public abstract 추가가 (생략할 )

    void sayHello(String name);

 

}

 

package com.asianaidt.greeting.good;

 

public class GreetingServiceEn implements GreetingService{

    public void sayHello(String name){

        System.out.println("hello"+name);

    }

 

}

 

package com.asianaidt.greeting.good;

 

public class GreetingServiceKO implements GreetingService{

    public void sayHello(String name){

        System.out.println("안녕"+name);

    }

 

}

 

package com.asianaidt.greeting.good;

 

public class GreetingTest {

 

    public static void main(String[] args) {

 

        // big >= small

        GreetingService greetingService = new GreetingServiceEn();

      

        greetingService.sayHello("이동욱");

    }

 

}

 

○ 컨테이너를 사용하는 경우

- 달라지는 것은 GreetingTest 부분만 달라짐 (유의해서 볼 것)

 

package com.asianaidt.greeting.excellent;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

 

public class GreetingTest {

 

    public static void main(String[] args) {

        

        ApplicationContext container = new ClassPathXmlApplicationContext("applicationContext.xml");

        

 

        // big >= small

        GreetingService greetingService = (GreetingService)container.getBean("greeting");

    

        greetingService.sayHello("이동욱");

        

    }

 

}

 

 

[과정 설명]

 

1 pom.xml > Dependencies

 

 

2 add > spring-beans

 

 

 

 

 

3 프로젝트(0313_step01_basic) 오른쪽 마우스 클릭 > configure > convert to maven project > dependency > add > spring-context

 

 

 

두가지 설정 완료

 

 

4 src > 오른쪽 마우스 클릭 > new Configuration File

 

 

 

 

 

 

 

5. src > applicationContext.xml > beans > New Bean

 

 

 

6 설정파일(GreetingServiceKO) 검색

 

7 이름설정

 

8 완료화면

 

9 완성소스

 

실행화면

 

실행화면 우리가 Factory에 저장해 두었던 속성대로 GreetingServiceKO로 가서 안녕이동이라는 결과값을 띄우게 된다.

Comments