使用如下代码时:
public class MyWebApplicationInitializer implements WebApplicationInitializer {
@Override public void onStartup(ServletContext container) {
ServletRegistration.Dynamic registration = container.addServlet("example", new DispatcherServlet());
registration.setLoadOnStartup(1);
registration.addMapping("/example/*");
}
}
会发生如下错误
Spring MVC looks for a file named example-servlet.xml in the WEB-INF
实现基于java的零xml配置,需要在DispatcherServlet 里面设置
WebApplicationContext
具体方法如下
AnnotationConfigWebApplicationContext servletAppContext = new AnnotationConfigWebApplicationContext(); Class<?>[] configClasses = new Class[]{WebConfig.class}; if (!ObjectUtils.isEmpty(configClasses)) { servletAppContext.register(configClasses); }new DispatcherServlet(servletAppContext);