둘러보기 생략.
 

1 Minute 가이드

1 Minute 가이드를 통해 Anyframe Core 기반 하에서 간단한 서비스를 생성하고 실행시켜 보도록 할 것이다. 1 Minute 가이드를 시작하기 전에 Anyframe Core를 설치했는지 확인하자.

Step 1 : 서비스 인터페이스 클래스 생성

  1. 서비스의 인터페이스 클래스를 생성하고, 인터페이스 클래스에 서비스 식별자 역할을 수행할 변수로써, ROLE을 정의한다.
  2. 또한 서비스 인터페이스 클래스 내에 해당 서비스가 제공해야 하는 기능 목록을 정의한다.

정의된 여러 서비스들 중 해당 서비스를 식별할 수 있게 하기 위한 식별자로써 정의한 ROLE 변수의 이름과 값은 해당 프로젝트의 표준에 따라 유일성을 보장받을 수 있도록 정의해야 한다. Anyframe Core에서는 ROLE 변수의 값을 해당 인터페이스 클래스의 패키지명을 포함한 클래스명으로 정의할 것을 가이드하고 있다.

package anyframe.examples;

public interface Cheese {
    /** role 식별자. : 선택 가능 */
    String ROLE = Cheese.class.getName();

    /**
     * Cheese 서비스 제공 기능 1
     * Slices the cheese for apportioning onto crackers.
     * @param slices the number of slices
     */
    void slice( int slices );

    /**
     * Cheese 서비스 제공 기능 2
     * Get the description of the aroma of the cheese.
     * @return the aroma
     */
    String getAroma();
}

Step 2 : 서비스 구현 클래스 생성

인터페이스 클래스를 implements하는 구현 클래스를 생성하고, 인터페이스 클래스에 선언된 기능들을 구현한다.

package anyframe.examples;

public class ParmesanCheese implements Cheese {
    public void slice( int slices ) {
        throw new UnsupportedOperationException( "No can do" );
    }

    public String getAroma() {
        return "strong";
    }
}

위에서 보는 것처럼 ParmesanCheese 클래스는 Cheese 클래스를 implements하고 있으며, slice() 와 getAroma()에 대한 구현 로직을 제공한다.

Step 3 : 서비스 속성 정의 파일 생성

위 소스들과 같은 경로 내에 context-example.xml 파일을 생성하고 다음과 같은 내용으로 서비스의 속성을 정의한다.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" 
       "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
   <bean id="anyframe.examples.Cheese" 
          class="anyframe.examples.ParmesanCheese">
   </bean>
</beans>

위 서비스 속성 정의 파일은 1 개의 서비스 정의를 포함하고 있으며, 앞서 정의한 Cheese 클래스의 ROLE 변수의 값을 id로 정의하고 있다. 또한 class에는 Cheese 클래스의 구현 클래스인 ParmesanCheese를 정의하고 있다.

Step 4 : 실행

main() 메소드를 가진 일반 java 클래스를 통해 앞서 정의한 서비스를 실행시켜 보도록 하자.

컨테이너 생성

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main(String args[]) throws Exception {
       String[] locations 
        = new String[] 
          { "classpath*:/anyframe/examples/context-example.xml"};
       ClassPathXmlApplicationContext context 
        = new ClassPathXmlApplicationContext(locations, false);
		context.refresh();
		...
    }
}

서비스 검색

앞서 new ClassPathXmlApplicationContext(...)를 통해 시작된 컨테이너로부터 Cheese 서비스를 찾아 getAroma() 메소드를 실행시키기 위해서는 main 메소드 내에 다음과 같은 로직을 추가한다.

Cheese cheese = (Cheese) context.getBean( Cheese.ROLE );
System.out.println( "Parmesan is " + cheese.getAroma() );

결과 확인

콘솔 창에 "Parmesan is strong"와 같은 문자열이 보이는지 확인한다.