Add MultipartConfig to DispatcherServlet by default

RC4 added a ServletRegistrationBaen for teh DispatcherServlet and we
didn't register a MultipartConfigElement if one was present.

Fixes gh-427
pull/450/head
Dave Syer 11 years ago
parent ffe91ca6bf
commit a0ba8c90a6

@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.web;
import java.util.Arrays;
import java.util.List;
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletRegistration;
import org.springframework.beans.factory.annotation.Autowired;
@ -69,6 +70,9 @@ public class DispatcherServletAutoConfiguration {
@Autowired
private ServerProperties server;
@Autowired(required = false)
private MultipartConfigElement multipartConfig;
@Bean(name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public DispatcherServlet dispatcherServlet() {
return new DispatcherServlet();
@ -76,8 +80,12 @@ public class DispatcherServletAutoConfiguration {
@Bean
public ServletRegistrationBean dispatcherServletRegistration() {
return new ServletRegistrationBean(dispatcherServlet(),
this.server.getServletPath());
ServletRegistrationBean registration = new ServletRegistrationBean(
dispatcherServlet(), this.server.getServletPath());
if (this.multipartConfig != null) {
registration.setMultipartConfig(this.multipartConfig);
}
return registration;
}
}

@ -16,15 +16,21 @@
package org.springframework.boot.autoconfigure.web;
import javax.servlet.MultipartConfigElement;
import org.junit.Test;
import org.springframework.boot.context.embedded.MultiPartConfigFactory;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
/**
* Tests for {@link DispatcherServletAutoConfiguration}.
@ -60,6 +66,33 @@ public class DispatcherServletAutoConfigurationTests {
ServletRegistrationBean registration = this.context
.getBean(ServletRegistrationBean.class);
assertEquals("[/spring]", registration.getUrlMappings().toString());
assertNull(registration.getMultipartConfig());
}
@Test
public void multipartConfig() throws Exception {
this.context = new AnnotationConfigWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(MultipartConfiguration.class,
ServerPropertiesAutoConfiguration.class,
DispatcherServletAutoConfiguration.class);
this.context.refresh();
ServletRegistrationBean registration = this.context
.getBean(ServletRegistrationBean.class);
assertNotNull(registration.getMultipartConfig());
}
@Configuration
protected static class MultipartConfiguration {
@Bean
public MultipartConfigElement multipartConfig() {
MultiPartConfigFactory factory = new MultiPartConfigFactory();
factory.setMaxFileSize("128KB");
factory.setMaxRequestSize("128KB");
return factory.createMultipartConfig();
}
}
}

Loading…
Cancel
Save