1. 程式人生 > >基於註解的SpringMVC自定義DispatcherServlet配置

基於註解的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 }