raycon / til

Today I Learned
MIT License
16 stars 3 forks source link

Root, Servlet WebApplicationContext #135

Open raycon opened 2 years ago

raycon commented 2 years ago

DispatcherServlet

web.xml 로 정의

<web-app>

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

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

    <!-- Servlet -->
    <servlet>
        <servlet-name>app1</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/app1-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>app1</servlet-name>
        <url-pattern>/app1/*</url-pattern>
    </servlet-mapping>

</web-app>

Java 로 정의:

public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    // Root
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] { RootConfig.class };
    }

    // Servlet
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { App1Config.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/app1/*" };
    }
}

web.xml 대신 자바 정의를 사용할 수 있는 이유

스프링 부트에서 컨텍스트가 초기화되는 방법

Root WebApplicationContext 와 Servlet WebApplicationContext 가 연결되는 지점:

raycon commented 2 years ago

Servlet Context Initialization

Context Hierarchy