IT/Spring

스프링 ContextLoaderListener 의 역할

우서바 2013. 9. 11. 11:04

참조 : http://blog.naver.com/PostView.nhn?blogId=minis24&logNo=80097770192&redirect=Dlog&widgetTypeCall=true


http://devyongsik.tistory.com/146


<servlet>

    <servlet-name>aController</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>/WEB-INF/a-servlet.xml</param-value>

        </init-param>

    <load-on-startup>1</load-on-startup>

</servlet>


<servlet>

    <servlet-name>bController</servlet-name>

    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>/WEB-INF/b-servlet.xml</param-value>

        </init-param>

     <load-on-startup>1</load-on-startup>

</servlet>


위와 같은 경우 DispatcherServlet 은 각가 별도의 webapplicationcontext를 생성한다.

두 context 는 독립적이므로 각각의 설정파일에서 생성한 빈을 서로 사용할 수 없다.(공유X)


이때 동시에 필요한 의존성(공통빈) 이 있어야 하는 경우

ContextLoaderListener 을 사용하여 공통으로 사용할 빈을 설정할 수 있다.


<listener>

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

</listener>


<context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>

        /WEB-INF/applicationContext.xml

        /WEB-INF/applicationContext_dao.xml

    </param-value>

</context-param>


ContextLoaderListener 와 DispatcherServlet 은 각각 webapplicationcontext 를 생성하는데

ContextLoaderListener 가 생성한 컨텍스트가 root 컨텍스트가 되고 DispatcherServlet  생성한 인스턴스는

root 컨텍스트를 부모로 하는 자식 컨텍스트가 된다.


자식 컨텍스트들은 root 컨텍스트의 설정 빈을 사용 할 수 있다.

그러기에 ContextLoaderListener 을 이용 공통빈 설정 가능.