基于注解的SpringMVC自定义DispatcherServlet配置
通过重载AbstractAnnotationConfigDispatcherServletInitializer实现类的customizeRegistration()方法来自定义DispatcherServlet配置
在AbstractAnnotationConfigDispatcherServletInitializer将DispatcherServlet注册到Servlet容器之后,就会调用customizeRegistration()方法,并将Servlet注册后得到的ServletRegistration.Dynamic传递进来,通过重载customizeRegistration()方法,我们可以对DispatcherServlet进行额外的配置。
1 public class SpringWebAppInit extends AbstractAnnotationConfigDispatcherServletInitializer { 2 //Spring容器 3 @Override 4 protected Class<?>[] getRootConfigClasses() { 5 return new Class[]{RootConfig.class}; 6 } 7 8 //SpringMVC容器 9 @Override 10 protected Class<?>[] getServletConfigClasses() { 11 return new Class[]{WebConfig.class}; 12 } 13 14 //DispatcherServlet映射关系 15 @Override 16 protected String[] getServletMappings() { 17 return new String[]{"/"}; 18 } 19 20 21 @Override 22 protected void customizeRegistration(ServletRegistration.Dynamic registration) { 23 super.customizeRegistration(registration); 24 registration.setMultipartConfig(new MultipartConfigElement("/tmp/coreqi/uploads"));//配置对multipart的支持 25 registration.setLoadOnStartup(1);//设置load-on-startup优先级 26 } 27 }