Hello World! 자세히 들여다 보기
Submitted by soyon.lim on 화, 05/20/2008 - 14:17
Hello World! 어플리케이션은 HelloWorld 서비스로만 구성되었으며, HelloWorld 서비스는 서비스 인터페이스 클래스, 해당 인터페이스를 implements한 구현 클래스 그리고 속성 정의 파일로 구성되어 있다. 다음과 같이 구분하여 HelloWorld 서비스의 주요 구성 요소에 대해 살펴보도록 하겠다.
서비스 인터페이스 클래스
다음은 HelloWorld 서비스에 대한 인터페이스 클래스의 내용이다.
package anyframe.examples.helloworld;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public interface IHelloWorldService {
String ROLE = IHelloWorldService.class.getName();
Log LOGGER = LogFactory.getLog(IHelloWorldService.class);
/**
* Return greetings message.
*
*/
public String greet() throws Exception;
}
서비스 구현 클래스
다음은 HelloWorld 서비스에 대한 구현 클래스의 내용이다.
package anyframe.examples.helloworld;
public class HelloWorldServiceImpl
implements IHelloWorldService {
String greetings;
public String greet() throws Exception {
IHelloWorldService.LOGGER.debug("start greet()");
return "Hello" + getGreetings();
}
public String getGreetings() {
return greetings;
}
public void setGreetings(String greetings) {
IHelloWorldService.LOGGER.debug("setGreetings()");
this.greetings = greetings;
}
}
서비스 속성 정의 파일
다음은 HelloWorld 서비스에 대한 속성 정의 파일의 일부 내용이다.
<bean name="anyframe.examples.helloworld.IHelloWorldService"
class="anyframe.examples.helloworld.HelloWorldServiceImpl">
<property name="greetings">
<value>World!</value>
</property>
</bean>
테스트 클래스
다음은 HelloWorld 서비스를 테스트하기 위한 테스트 코드의 내용이다.
public class HelloWorldClient{
private static Log logger
= LogFactory.getLog(HelloWorldClient.class);
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context;
logger.debug("get DefaultLoader()");
logger.debug("get ApplicationContext()");
String[] locations = getConfigLocations();
context = new ClassPathXmlApplicationContext
(locations, false);
context.refresh();
logger.debug("HelloWorldService lookup");
IHelloWorldService iHelloWorldService =
(IHelloWorldService) context.getBean
(IHelloWorldService.ROLE);
logger.debug("System.out.println");
System.out.println ("HelloWorldService : "
+ iHelloWorldService.greet());
logger.debug("loader.dispose()");
context.close();
}
protected static String[] getConfigLocations() {
return new String[]
{ "classpath*:/context-*.xml"};
}
}

