최범균님의 스프링과 인터넷검색을 기반으로 제가 볼 용도의 글을 쓰는것입니다.

우선 web.xml에 dispatcherServlet 이름을 지정하여 생성하는데, 그러면 [dispatcherServlet이름]-servlet.xml 이 설정파일이 된다.

이때 servlet.xml 에 무식하게 다 적는 방법이 아닌 다른 설정을 불러와서 추가 하고 싶은경우가 있다면?

DispatcherServlet을 설정할때, contextConfingLocation 초기화 파라미터에 설정파일 목록을 지정하면된다. 
구분자는  콤마(,) 공백 , 탭, 줄바꿈(\n) , 세미콜론(;) 가 가능하다.

        <servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                <init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB--INF/main.xml /WEB-INF/bbs.xml</param-value>
</init-param> 
 
</servlet>

요런식으로 가능하게 된다.

혹은 dispatcherServlet 자체를 여러개 만들어도 된다.
 
        <servlet>
<servlet-name>front</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                <init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB--INF/front.xml</param-value>
</init-param> 
</servlet>
        <servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                <init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/rest.xml</param-value>
</init-param> 
</servlet>

그럼 front-serlvet.xml 과 rest-servlet.xml 2개의 설정파일이 생기게 되는데, 이때 같은빈을 각각의 설정파일에서 등록하고 있다면?
이를 공통화할 수 있다.
공통으로 사용될 빈 객체 정보를 담고 있는 설정파일목록을 아래처럼 service에 넣어서 처리가능하다.

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
        /WEB-INF/service.xml
    </param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
 
        <servlet>
<servlet-name>front</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
        <servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

ContextLoaderListener 가 생성하는 WebApplicationContext 는 웹어플리케이션에서 루트컨텍스트, DispatcherServlet이 생성하는 WebApplicationContext는 루트 컨텍스트를 부모로 사용하는 자식 컨텍스트가 된다. 자식은 root가 제공하는 빈을 쓸 수 있으므로 ContextLoaderListener 를 이용하여 설정하는것임

ContextLoaderListener 는 contextConfigLocation 컨텍스트 파라미터를 명시하지 않으면 /WEB-INF/applicationContext.xml 을 설정 파일로 사용한다.

<param-value> classpath:xxx.xml</param-value>
하면 클래스패스에 위치한 파일로부터 설정 정보를 읽어온다.

 
 

 



'IT > 스프링' 카테고리의 다른 글

@Controller 어노테이션... - 1 -  (0) 2012.01.10
filter....  (0) 2012.01.10
Spring 3.0 MVC 정리  (0) 2012.01.10
AspectJ in Spring?  (0) 2012.01.09
weaving??  (0) 2012.01.09

+ Recent posts