适用于Servlet 3.0的Spring 4 Java Config for MultipartResolver

时间:2014-05-09 16:49:26

标签: java spring spring-mvc servlets spring-java-config

我采用全Java方法进行Spring MVC配置,无法弄清楚如何以编程方式将MultipartConfigElementDispatcherServlet相关联。

Spring文档声明:

  

为了使用基于Servlet 3.0的多部分解析,您需要标记   带有" multipart-config"的DispatcherServlet web.xml中的部分,或   在程序化Servlet中使用一个javax.servlet.MultipartConfigElement   登记...

http://docs.spring.io/spring/docs/4.0.4.RELEASE/spring-framework-reference/htmlsingle/#mvc-multipart

这是我的WebApplicationInitializer代码:

public class DispatcherServletInitializer implements WebApplicationInitializer {

    private static final Logger logger = LoggerFactory.getLogger(DispatcherServletInitializer.class);

    @Override
    public void onStartup(ServletContext container) {

        // Create the 'root' Spring application context
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(AppConfig.class);

        // Manage the lifecycle of the root application context
        container.addListener(new ContextLoaderListener(rootContext));

        // Create the dispatcher servlet's Spring application context
        AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
        dispatcherContext.register(WebConfig.class);

        //HOW CAN I ASSOCIATE THIS CONFIG WITH MY DISPATCHER SERVLET?
        MultipartConfigElement config = new MultipartConfigElement("C:\\Temp", 20848820, 418018841, 1048576);
        DispatcherServlet dispatcherServlet = new DispatcherServlet(dispatcherContext);

        // Register and map the dispatcher servlet
        ServletRegistration.Dynamic dispatcher = 
            container.addServlet("dispatcher", dispatcherServlet);
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/*");
    }

}

如何将MultipartConfigElement与我的DispatcherServlet相关联?我没有看到任何方法,比如setMultipartConfiguration或任何接受它的构造函数。

另请注意,我的WebConfig声明了MultipartResolver

@Bean
public StandardServletMultipartResolver multipartResolver(){
    return new StandardServletMultipartResolver();
}

但Spring文档指出:

Configuration settings such as maximum sizes or storage locations need to be applied at that Servlet registration level as Servlet 3.0 does not allow for those settings to be done from the MultipartResolver.

非常感谢任何指导。

2 个答案:

答案 0 :(得分:11)

看起来你需要这个:

ServletRegistration.Dynamic dispatcher = 
            container.addServlet("dispatcher", dispatcherServlet);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/*");

dispatcher.setMultipartConfig(new MultipartConfigElement("/tmp", 1024*1024*5, 1024*1024*5*5, 1024*1024));

答案 1 :(得分:1)

这是与AbstractAnnotationConfigDispatcherServletInitializer配置servlet方式兼容的解决方案。这比WebApplicationInitializer的侵入性小。

它使用覆盖AbstractAnnotationConfigDispatcherServletInitializer.customizeRegistration

public class MySpringWebSetup extends AbstractAnnotationConfigDispatcherServletInitializer
{
  // Your usual obligatory configuration overrides:
  @Override protected Class<?>[] getRootConfigClasses() { ... }
  @Override protected Class<?>[] getServletConfigClasses() { ... }
  @Override protected String[] getServletMappings() { ... }

  // Optional configuration:
  @Override
  protected void customizeRegistration(Dynamic registration) {
    registration.setMultipartConfig(
      // Maybe use more sophisticated configuration than this:
      new MultipartConfigElement("")
    );
  }
}

我发现它抓住了getServletMappings的堆栈跟踪,因此进入了org\springframework\web\servlet\support\AbstractDispatcherServletInitializer.java的代码:

protected void registerDispatcherServlet(ServletContext servletContext) {

    [more registration stuff was here]

    registration.setLoadOnStartup(1);
    registration.addMapping(getServletMappings());
    registration.setAsyncSupported(isAsyncSupported());

    Filter[] filters = getServletFilters();
    if (!ObjectUtils.isEmpty(filters)) {
        for (Filter filter : filters) {
            registerServletFilter(servletContext, filter);
        }
    }


    customizeRegistration(registration);
}