- public classMywebAppInitializerimplements WebApplicationInitializer{
- @Override
- public voidonStartup(ServletContext servletContext)throws ServletException {
- //相当于在web.xml在配置spring启动用的ContextLoaderListenerAnnotationConfigWebApplicationContext rootContext=new AnnotationConfigWebApplicationContext();
- rootContext.register(AppConfig.class);
- servletContext.addListener(new ContextLoaderListener(rootContext));
- //相当于在web.xml在配置DispatcherServletAnnotationConfigWebApplicationContext webContext=new AnnotationConfigWebApplicationContext();
- webContext.register(WebConfig.class);
- Dynamic registration = servletContext.addServlet("dispatcher",new DispatcherServlet(webContext));
- registration.setLoadOnStartup(1);
- registration.addMapping("/");
- }
- }
来源: