정의

WebApplicationInitializer란?

servlet 3.0 버전 이후, web.xml 없이 servlet context 초기화 작업을 도와주는 API

Gradle 설정

dependencies {  
    implementation 'org.springframework:spring-web:5.3.22'  
    implementation 'javax.servlet:javax.servlet-api:4.0.1'
}

Example

  • WebApplicationInitializer 구현 Class 선언
  • onStartup 메서드 구현
public class ApplicationInitializer implements WebApplicationInitializer {  
    @Override  
    public void onStartup(ServletContext servletContext) throws ServletException {  
        AnnotationConfigWebApplicationContext apiContext = new AnnotationConfigWebApplicationContext();  
        apiContext.register(APIConfiguration.class);  
  
        DispatcherServlet apiDispatcherServlet = new DispatcherServlet(apiContext);  
  
        ServletRegistration.Dynamic apiDispatcher = servletContext.addServlet("apiDispatcher", apiDispatcherServlet);  
        apiDispatcher.addMapping("/*");  
    }  
}

연결문서

댓글남기기