Spring

[Spring] WebApplicationInitializer

KEMON 2020. 6. 14. 21:32
728x90

공식 API 문서를 보면 알 수 있듯이

Servlet 3.0 환경의 인터페이스로 기존의 web.xml로 설정하는 것을 프로그래밍으로 설정할 수 있도록 한다.

 

기존에는 web.xml로 Servlet을 설정 했다면 onStartup 메소드를 오버라이드 해주면서 스프링 설정을 해준다.

 public class MyWebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) {
      // Create the 'root' Spring application context
      AnnotationConfigWebApplicationContext rootContext =
        new AnnotationConfigWebApplicationContext();
      rootContext.register(AppConfig.class);

      // Manage the lifecycle of the root application context
      container.addListener(new ContextLoaderListener(rootContext));

      // Create the dispatcher servlet's Spring application context
      AnnotationConfigWebApplicationContext dispatcherContext =
        new AnnotationConfigWebApplicationContext();
      dispatcherContext.register(DispatcherConfig.class);

      // Register and map the dispatcher servlet
      ServletRegistration.Dynamic dispatcher =
        container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
      dispatcher.setLoadOnStartup(1);
      dispatcher.addMapping("/");
    }

 }

출처 : https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/WebApplicationInitializer.html

 

WebApplicationInitializer (Spring Framework 5.2.7.RELEASE API)

Interface to be implemented in Servlet 3.0+ environments in order to configure the ServletContext programmatically -- as opposed to (or possibly in conjunction with) the traditional web.xml-based approach. Implementations of this SPI will be detected autom

docs.spring.io

 

728x90