본문 바로가기
프로그래밍/스프링프레임워크

스프링프레임웍의 시동

by pentode 2018. 4. 19.

스프링프레임웍이 제 기능을 수행하기 위해서는 필요한 설정, 빈들이 WAS 가 시작될 때 모두 생성되고, 준비가 되어야 합니다. 이렇게 준비하는 과정을 스프링프레임웍의 시동이라고 할 수 있겠습니다.

 

WAS 가 시작될 때 필요한 설정들이 시작되는 곳이 배포 설명자(Deploy Descriptor)web.xml 파일입니다. 그러므로 스프링 프레임웍의 실행도 여기에서 시작합니다. 스프링프레임웍의 설정은 xml 파일로 작성되었었습니다. 지금은 순수한 자바 클래스에 아노테이션을 사용해서 설정을 할 수 있습니다. 그리고 그 두 가지를 섞어서 사용하는 것도 가능합니다.

 

이것들을 조합해서 사용할 수 있는 네 가지의 정도의 시동 방법을 알아보겠습니다. 전자정부표준프레임웍 템플릿의 설정으로 예를 들어 보겠습니다.

 

 

1. ContextLoaderListener와 xml 설정 파일을 통한 시동

 

처음부터 사용되어 오던 가장 기본적인 시동 방법입니다. 아직도 많이 사용되며 전자정부표준프레임웍 템플릿에서도 사용되고 있습니다.

 

서블릿에서 컨텍스트 로드 리스너는 컨텍스트가 실행될때 뭔가를 실행할 수 있는 방법입니다. 이 리스너를 구현해서 스프링 프레임웍을 로딩하는데 사용하고 있습니다. 설정파일들은 <context-param> 을 통해서 제공합니다.

 

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath*:egovframework/spring/com/context-*.xml</param-value>
</context-param>

<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

 

 

2. 순수 자바 클래스와 아노테이션을 이용한 시동

 

스프링프레임웍을 순수 자바 클래스 만으로 시작하는 것을 설명하기 위해서는 서블릿 컨텍스의 시작에 대한 사전 지식이 필요합니다. 좀 길어질 수 있지만 이것부터 예기해 보겠습니다.

 

Servlet 3.0 부터는 web.xml 없이 서블릿 컨텍스트를 초기화할 수 있는 방법이 나왔습니다. web.xml 파일과 같이 사용할 수 도 있습니다.

 

web.xml파일 없이 서블릿 컨텍스트를 초기화하기 위해서는 javax.servlet.ServletContainerInitializer 인터페이스를 구현하는 클래스를 만들고, 이 클래스의 이름을 /META-INF/services/javax.servlet.ServletContainerInitializer 라는 텍스트 파일에 적어줍니다. xml 파일은 없어졌지만 시작위치를 지정하는 텍스트 파일 하나는 남아 있습니다.

 

이 구현 클래스에 @HandlesTypes 아노테이션으로 클래스를 지정해주면 WAS 가 시작될 때 지정된 클래스를 찾아서 onStartup 메소드의 인자로 서블릿 컨텍스트 객체와 지정된 클래스를 넣어줍니다. 개발자는 이것을 이용해서 설정을 하게 되는 것입니다. 아래 코드는 스프링프레익웍에서 사용되고 있는것 입니다.

 

@HandlesTypes(WebApplicationInitializer.class)
public SpringServletContainerInitializer implements ServletContainerInitializer {
	@Override
	public void onStartup(Set<class<?>> webAppInitializerClasses, ServletContext servletContext)
    	throws ServletException {
		...
	}
}

 

스프링프레임웍도 이것을 사용해서 시작을 할 수 있는 방법이 제공됩니다. spring-web-4.1.2.RELEASE.jar 파일내의 META-INF 폴더를 보면 위에서 설명한 방법을 사용하고 있는것을 알 수 있습니다.

 

 

ServletContainerInitializer 인터페이스를 구현하고 있는 SpringServletContainerInitializer 를 사용하고 있습니다. 이 클래스 내에서 @HandlesTypes(WebApplicationInitializer.class)를 지정하고 있습니다.

 

그러므로, 스프링프레임웍을 xml 없이 실행하기 위해서는 org.springframework.web.WebApplicationInitializer를 구현하는 클래스를 만들어주면 되는 것입니다.

 

작성예를 보도록 하겠습니다. WebApplicationInitializer인터페이스를 구현한 SpringStarter 라는 클래스를 만들었습니다. WAS 가 시작될 때 이 클래스가 실행됩니다. 실제 설정에 관련된 정보는 RootContextConfiguration 클래스에 작성합니다. 클래스 이름은 중요하지 않고  @Configuration 아노테이션을 사용한다는 것이 중요합니다. 그외에 xml 설정에서 사용하는 기능을 구현한 다양한 아노테이션들을 사용하여 설정을 하게 됩니다.

 

package com.tistory.pentode.init;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

public class SpringStarter implements WebApplicationInitializer {
	public void onStartup(ServletContext servletContext) throws ServletException {
		AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
		rootContext.register(com.tistory.pentode.init.RootContextConfiguration.class);
		servletContext.addListener(new ContextLoaderListener(rootContext));
	}
}



package com.tistory.pentode.init;

import org.springframework.context.annotation.Configuration;

@Configuration
public class RootContextConfiguration {
}

 

 

 

3. ContextLoaderListener와 설정 클래스를 이용한 시동

 

WebApplicationInitializer를 사용하지 않고  web.xml 을 사용해서 설정 클래스를 로드하는 방법도 있습니다. 다음처럼  contextConfigLocation 과 contextClass를 사용하는 방법입니다. 설정 자체는 순수 자바를 사용하여 수행됩니다.

 

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>com.tistory.pentode.init.RootContextConfiguration</param-value>
</context-param>

<context-param>
	<param-name>contextClass</param-name>
	<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>

<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

 

 

4. WebApplicationInitializer를 사용하여 시작하고 설정은 xml을 사용

 

시작을 WebApplicationInitializer를 사용하고 실제 설정은 xml 파일을 사용할 수 도 있습습니다.

 

package com.tistory.pentode.init;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

public class SpringStarter implements WebApplicationInitializer {
	public void onStartup(ServletContext servletContext) throws ServletException {
		AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
		rootContext.register(com.tistory.pentode.init.RootContextConfiguration.class);
		servletContext.addListener(new ContextLoaderListener(rootContext));
	}
}



package com.tistory.pentode.init;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportResource;

@Configuration
@Import({DatabaseConfig.class, ComponentConfig.class})
@ImportResource("classpath*:egovframework/spring/com/context-*.xml")
public class RootContextConfiguration {
}

 

@importResource 아노테이션을 사용하여 xml 설정 파일을 지정할 수 있습니다. 또한 @Import 아노테이션을 사용해서 다른 설정 클래스를 임포트하는 방식으로 사용할수도 있습니다. 설정이 복잡해지면 여러개의 설정 클래스로 나누어 사용하면 되겠습니다.

 

지금까지 스프링프레임웍을 시동하는 여러가지 방법에 대해서 알아보았습니다. 스프링프레임웍이 인기를 끌게된 원인이 이전에 사용하던 EJB가 설정이 너무 복잡하고 어려워서 개발자들 사이에서 사용하지 않게 되고, 이 때 간편하게 설정할 수 있는 스프링프레임웍이 인기를 끌게 되었다고 들었습니다.

 

현재 JEE 프레임웍의 대세가 스프링프레임웍이긴한데 점점 덩치가 커지고 설정이 복잡해지고 있는것 같습니다.

반응형