Java configuration registers and initializes the DispatcherServlet
public class MyWebApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletCxt) {
// Load Spring web application configuration
AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();
ac.register(AppConfig.class);
ac.refresh();
// Create and register the DispatcherServlet
DispatcherServlet servlet = new DispatcherServlet(ac);
ServletRegistration.Dynamic registration = servletCxt.addServlet("app", servlet);
registration.setLoadOnStartup(1);
registration.addMapping("/app/*");
}
}
SpringServletContainerInitializer 这是servlet规范,所有的web都会保存图一的格式,会启动调用startup方法,加上注释
HandlesTypes(WebApplicationInitializer.class) 会遍历实现这个接口的所有的类,依次执行startup方法。