org.springframework.web.SpringServletContainerInitializer
SpringServletContainerInitializer.onStartup()
WebapplicationInitializer.onStartup()
WebapplicationInitializer.onStartup() 호출하는데 WebapplicationInitializer 인터페이스를 구현한 구현체인 AppWebApplicationInitializer를 호출한다.
AppWebApplicationInitializer
는 AbstractAnnotationConfigDispatcherServletInitializer
클래스를 상속 받고 있다.
package com.bitcamp.board.config;
import javax.servlet.Filter;
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
import com.bitcamp.board.filter.LoginCheckFilter;
public class AppWebApplicationInitializer
extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] {RootConfig.class, DatabaseConfig.class, MybatisConfig.class};
}
@Override
protected String getServletName() {
return "app";
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] {AppWebConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[] {"/app/*"};
}
@Override
protected Filter[] getServletFilters() {
return new Filter[] {new CharacterEncodingFilter("UTF-8"), new LoginCheckFilter()};
}
@Override
protected void customizeRegistration(Dynamic registration) {
registration.setMultipartConfig(new MultipartConfigElement(System.getProperty("java.io.tmpdir"), // 클라이언트가 보낸 파일을 임시 저장할 때 사용할 디렉토리
1024 * 1024 * 5, // 한 파일의 최대 크기
1024 * 1024 * 10, // 첨부 파일을 포함한 전체 요청 데이터의 최대 크기
1024 * 1024 // 첨부 파일을 데이터를 일시적으로 보관할 버퍼 크기
));
}
}
AbstractDispatcherServletInitializer
에서 onStartup을 구현하고있는 것을 확인할 수 있다.public abstract class AbstractDispatcherServletInitializer extends AbstractContextLoaderInitializer {
/**
* The default servlet name. Can be customized by overriding {@link #getServletName}.
*/
public static final String DEFAULT_SERVLET_NAME = "dispatcher";
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
registerDispatcherServlet(servletContext);
}
AbstractDispatcherServletInitializer
클래스의 super.onStartup(servletContext);
호출에 의해서 조상 클래스의 onStartup()
메서드를 보면 registerContextLoaderListener(servletContext)
를 호출하는 것을 확인할 수 있다.public abstract class AbstractContextLoaderInitializer implements WebApplicationInitializer {
/** Logger available to subclasses. */
protected final Log logger = LogFactory.getLog(getClass());
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
registerContextLoaderListener(servletContext);
}
registerContextLoaderListener()
함수의 createRootApplicationContext()
를 호출한다.public abstract class AbstractContextLoaderInitializer implements WebApplicationInitializer {
protected void registerContextLoaderListener(ServletContext servletContext) {
WebApplicationContext rootAppContext = createRootApplicationContext();
if (rootAppContext != null) {
ContextLoaderListener listener = new ContextLoaderListener(rootAppContext);
listener.setContextInitializers(getRootApplicationContextInitializers());
servletContext.addListener(listener);
}
else {
logger.debug("No ContextLoaderListener registered, as " +
"createRootApplicationContext() did not return an application context");
}
}
createRootApplicationContext
는 AbstractAnnotationConfigDispatcherServletInitializer
에서 구현하고있는데 Class<?>[] configClasses = getRootConfigClasses()
를 호출하고있다.public abstract class AbstractAnnotationConfigDispatcherServletInitializer
extends AbstractDispatcherServletInitializer {
@Override
@Nullable
protected WebApplicationContext createRootApplicationContext() {
Class<?>[] configClasses = getRootConfigClasses();
if (!ObjectUtils.isEmpty(configClasses)) {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(configClasses);
return context;
}
else {
return null;
}
}
getRootConfigClasses
는 AbstractAnnotationConfigDispatcherServletInitializer
추상 클래스의 추상 메서드이며, getRootConfigClasses
를 AppWebApplicationInitializer
구현 되어있다.public abstract class AbstractAnnotationConfigDispatcherServletInitializer
extends AbstractDispatcherServletInitializer {
@Nullable
protected abstract Class<?>[] getRootConfigClasses();
getRootConfigClasses() 호출에의해서 return new Class<?>[] {RootConfig.class, DatabaseConfig.class, MybatisConfig.class}; 배열로 반환한다.
public class AppWebApplicationInitializer
extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] {RootConfig.class, DatabaseConfig.class, MybatisConfig.class};
}