반응형

Spring ApplicationContext Container


(원문 위치 : http://www.tutorialspoint.com/spring/spring_applicationcontext_container.htm )

Application Context는 Spring의 더 발전된 container이다. BeanFactory와 유사하게 bean definition load, bean을 묶고(wire), 요청에 따라 bean을 분배할 수 있다. 게다가 이것은 더 enterprise-specific 기능을 추가한다.

가장 일반적인 ApplicationContext 구현은 아래와 같다.
  • FileSystemXmlApplicationContext이 container는 xml file에서 bean definition을 load한다. 여기서 생성자에 xml bean 설정파일의 full path를 제공해야 한다.

  • ClassPathXmlApplicationContext: 이 container는 xml file에서 bean definition을 load한다. 여기서는 xml file의 full path를 제공하지 않아도 되지만 이 container가 CLASSPATH에서 bean 설정 xml file을 찾기 때문에 적절한 CLASSPATH의 설정이 필요하다.

  • WebXmlApplicationContext: 이 container는 web application에서부터 모든 bean의 정의(definition)으로 xml file을 load한다.

[003] HelloWorld Example로  ClassPathXmlApplicationContext container 예제를 이미 Web기반 Spring app;ication에 대한 강좌에서 XmlWebApplicationContext를 다룰 것이다. 따라서 여기에서는 FileSystemXmlApplicationContext의 예제를 보자.

[003] HelloWorld Example의 방식으로 프로젝트를 생성한다.
    - package : com.tutorialspoint
    - project : 003.SpringExample

    -> 지금까지의 예제와 MainApp.java를 제외하면 동일하다. FileSystemXmlApplicationContext() API에 xml file의 full path를 생성자로 주어 factory bean 객체를 생성하고 그 이후 부분은 동일하다.

HelloWorld.java:

package com.tutorialspoint;

public class HelloWorld {
   private String message;

   public void setMessage(String message){
      this.message  = message;
   }

   public void getMessage(){
      System.out.println("Your Message : " + message);
   }
}

MainApp.java:

package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class MainApp {
   public static void main(String[] args) {

      ApplicationContext context = new FileSystemXmlApplicationContext
            ("C:/Users/ZARA/workspace/HelloSpring/src/Beans.xml");

      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
      obj.getMessage();
   }
}

Beans.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">

   <bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
       <property name="message" value="Hello World!"/>
   </bean>

</beans>

실행 결과는 아래와 같다.

Your Message : Hello World!


반응형

+ Recent posts