Skip to content

Commit 018231d

Browse files
committed
Polish
1 parent 7bf3e35 commit 018231d

File tree

4 files changed

+77
-68
lines changed

4 files changed

+77
-68
lines changed

spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/web/MultipartAutoConfiguration.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,31 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.bootstrap.autoconfigure.web;
1718

1819
import javax.servlet.MultipartConfigElement;
1920

2021
import org.springframework.bootstrap.context.annotation.ConditionalOnBean;
22+
import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration;
2123
import org.springframework.context.annotation.Bean;
2224
import org.springframework.context.annotation.Configuration;
2325
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
2426

2527
/**
26-
* Autoconfiguration for multipart uploads. It detects the existence of a
27-
* {@link MultipartConfigElement} in the app context and then adds critical beans
28-
* while also autowiring it into the Jetty/Tomcat embedded containers.
28+
* {@link EnableAutoConfiguration Auto-configuration} for multi-part uploads. It detects
29+
* the existence of a {@link MultipartConfigElement} in the app context and then adds
30+
* critical beans while also autowiring it into the Jetty/Tomcat embedded containers.
2931
*
3032
* @author Greg Turnquist
31-
*
3233
*/
3334
@Configuration
3435
public class MultipartAutoConfiguration {
35-
36-
@ConditionalOnBean(MultipartConfigElement.class)
36+
3737
@Bean
38+
@ConditionalOnBean(MultipartConfigElement.class)
3839
public StandardServletMultipartResolver multipartResolver() {
39-
System.out.println("Loading up a MultipartResolver!!!");
40-
return new StandardServletMultipartResolver();
40+
return new StandardServletMultipartResolver();
4141
}
4242

4343
}

spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/EmbeddedWebApplicationContext.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.HashSet;
2424
import java.util.LinkedHashSet;
2525
import java.util.List;
26-
import java.util.Map;
2726
import java.util.Map.Entry;
2827
import java.util.Set;
2928

@@ -210,13 +209,6 @@ protected Collection<ServletContextInitializer> getServletContextInitializerBean
210209
}
211210
initializers.add(initializer);
212211
}
213-
214-
Map<String, MultipartConfigElement> multipartConfigBeans;
215-
MultipartConfigElement multipartConfigElement = null;
216-
multipartConfigBeans = getBeanFactory().getBeansOfType(MultipartConfigElement.class);
217-
for (MultipartConfigElement bean : multipartConfigBeans.values()) {
218-
multipartConfigElement = bean;
219-
}
220212

221213
List<Entry<String, Servlet>> servletBeans = getOrderedBeansOfType(Servlet.class);
222214
for (Entry<String, Servlet> servletBean : servletBeans) {
@@ -229,15 +221,11 @@ protected Collection<ServletContextInitializer> getServletContextInitializerBean
229221
if (name.equals(DISPATCHER_SERVLET_NAME)) {
230222
url = "/"; // always map the main dispatcherServlet to "/"
231223
}
232-
if (multipartConfigElement != null) {
233-
initializers.add(new ServletRegistrationBean(servlet, multipartConfigElement, url) {{
234-
setName(name);
235-
}});
236-
} else {
237-
initializers.add(new ServletRegistrationBean(servlet, url) {{
238-
setName(name);
239-
}});
240-
}
224+
ServletRegistrationBean registration = new ServletRegistrationBean(servlet,
225+
url);
226+
registration.setName(name);
227+
registration.setMultipartConfig(getMultipartConfig());
228+
initializers.add(registration);
241229
}
242230

243231
for (Entry<String, Filter> filterBean : getOrderedBeansOfType(Filter.class)) {
@@ -254,6 +242,14 @@ protected Collection<ServletContextInitializer> getServletContextInitializerBean
254242
return initializers;
255243
}
256244

245+
private MultipartConfigElement getMultipartConfig() {
246+
List<Entry<String, MultipartConfigElement>> beans = getOrderedBeansOfType(MultipartConfigElement.class);
247+
if (beans.isEmpty()) {
248+
return null;
249+
}
250+
return beans.get(0).getValue();
251+
}
252+
257253
/**
258254
* Prepare the {@link WebApplicationContext} with the given fully loaded
259255
* {@link ServletContext}. This method is usually called from

spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/ServletRegistrationBean.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public class ServletRegistrationBean extends RegistrationBean {
5656
private int loadOnStartup = 1;
5757

5858
private Set<Filter> filters = new LinkedHashSet<Filter>();
59-
60-
private MultipartConfigElement multipartConfigElement = null;
59+
60+
private MultipartConfigElement multipartConfig;
6161

6262
/**
6363
* Create a new {@link ServletRegistrationBean} instance.
@@ -67,19 +67,14 @@ public ServletRegistrationBean() {
6767

6868
/**
6969
* Create a new {@link ServletRegistrationBean} instance with the specified
70-
* {@link Servlet} and URL mapping.
70+
* {@link Servlet} and URL mappings.
7171
* @param servlet the servlet being mapped
7272
* @param urlMappings the URLs being mapped
7373
*/
7474
public ServletRegistrationBean(Servlet servlet, String... urlMappings) {
7575
setServlet(servlet);
7676
addUrlMappings(urlMappings);
7777
}
78-
79-
public ServletRegistrationBean(Servlet servlet, MultipartConfigElement multipartConfigElement, String... urlMappings) {
80-
this(servlet, urlMappings);
81-
this.multipartConfigElement = multipartConfigElement;
82-
}
8378

8479
/**
8580
* Sets the servlet to be registered.
@@ -152,6 +147,22 @@ public void addFilters(Filter... filters) {
152147
this.filters.addAll(Arrays.asList(filters));
153148
}
154149

150+
/**
151+
* Set the the {@link MultipartConfigElement multi-part configuration}.
152+
* @param multipartConfig the muti-part configuration to set or {@code null}
153+
*/
154+
public void setMultipartConfig(MultipartConfigElement multipartConfig) {
155+
this.multipartConfig = multipartConfig;
156+
}
157+
158+
/**
159+
* Returns the {@link MultipartConfigElement multi-part configuration} to be applied
160+
* or {@code null}.
161+
*/
162+
public MultipartConfigElement getMultipartConfig() {
163+
return this.multipartConfig;
164+
}
165+
155166
/**
156167
* Returns the servlet name that will be registered.
157168
*/
@@ -189,8 +200,8 @@ protected void configure(ServletRegistration.Dynamic registration) {
189200
}
190201
registration.addMapping(urlMapping);
191202
registration.setLoadOnStartup(this.loadOnStartup);
192-
if (multipartConfigElement != null) {
193-
registration.setMultipartConfig(multipartConfigElement);
203+
if (this.multipartConfig != null) {
204+
registration.setMultipartConfig(this.multipartConfig);
194205
}
195206
}
196207
}

spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/web/MultipartAutoConfigurationTests.java

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package org.springframework.bootstrap.autoconfigure.web;
1718

1819
import javax.servlet.MultipartConfigElement;
@@ -40,8 +41,8 @@
4041
import static org.junit.Assert.assertSame;
4142

4243
/**
43-
* A series of embedded unit tests, based on an empty configuration, no multipart
44-
* configuration, and a multipart configuration, with both Jetty and Tomcat.
44+
* Tests for {@link MultipartAutoConfiguration}. Tests an empty configuration, no
45+
* multipart configuration, and a multipart configuration (with both Jetty and Tomcat).
4546
*
4647
* @author Greg Turnquist
4748
* @author Dave Syer
@@ -51,7 +52,7 @@ public class MultipartAutoConfigurationTests {
5152
private AnnotationConfigEmbeddedWebApplicationContext context;
5253

5354
@Rule
54-
public ExpectedException exception = ExpectedException.none();
55+
public ExpectedException thrown = ExpectedException.none();
5556

5657
@After
5758
public void close() {
@@ -121,8 +122,39 @@ public void containerWithNoMultipartTomcatConfiguration() {
121122
verifyServletWorks();
122123
}
123124

125+
@Test
126+
public void containerWithAutomatedMultipartJettyConfiguration() {
127+
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
128+
ContainerWithEverythingJetty.class,
129+
EmbeddedServletContainerAutoConfiguration.class,
130+
MultipartAutoConfiguration.class);
131+
this.context.getBean(MultipartConfigElement.class);
132+
assertSame(this.context.getBean(DispatcherServlet.class).getMultipartResolver(),
133+
this.context.getBean(StandardServletMultipartResolver.class));
134+
verifyServletWorks();
135+
}
136+
137+
@Test
138+
public void containerWithAutomatedMultipartTomcatConfiguration() {
139+
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
140+
ContainerWithEverythingTomcat.class,
141+
EmbeddedServletContainerAutoConfiguration.class,
142+
MultipartAutoConfiguration.class);
143+
this.context.getBean(MultipartConfigElement.class);
144+
assertSame(this.context.getBean(DispatcherServlet.class).getMultipartResolver(),
145+
this.context.getBean(StandardServletMultipartResolver.class));
146+
verifyServletWorks();
147+
}
148+
149+
private void verifyServletWorks() {
150+
RestTemplate restTemplate = new RestTemplate();
151+
assertEquals(restTemplate.getForObject("http://localhost:8080/", String.class),
152+
"Hello");
153+
}
154+
124155
@Configuration
125156
public static class ContainerWithNoMultipartTomcat {
157+
126158
@Bean
127159
TomcatEmbeddedServletContainerFactory containerFactory() {
128160
return new TomcatEmbeddedServletContainerFactory();
@@ -134,18 +166,6 @@ WebController controller() {
134166
}
135167
}
136168

137-
@Test
138-
public void containerWithAutomatedMultipartJettyConfiguration() {
139-
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
140-
ContainerWithEverythingJetty.class,
141-
EmbeddedServletContainerAutoConfiguration.class,
142-
MultipartAutoConfiguration.class);
143-
this.context.getBean(MultipartConfigElement.class);
144-
assertSame(this.context.getBean(DispatcherServlet.class).getMultipartResolver(),
145-
this.context.getBean(StandardServletMultipartResolver.class));
146-
verifyServletWorks();
147-
}
148-
149169
@Configuration
150170
public static class ContainerWithEverythingJetty {
151171
@Bean
@@ -164,18 +184,6 @@ WebController webController() {
164184
}
165185
}
166186

167-
@Test
168-
public void containerWithAutomatedMultipartTomcatConfiguration() {
169-
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
170-
ContainerWithEverythingTomcat.class,
171-
EmbeddedServletContainerAutoConfiguration.class,
172-
MultipartAutoConfiguration.class);
173-
this.context.getBean(MultipartConfigElement.class);
174-
assertSame(this.context.getBean(DispatcherServlet.class).getMultipartResolver(),
175-
this.context.getBean(StandardServletMultipartResolver.class));
176-
verifyServletWorks();
177-
}
178-
179187
@Configuration
180188
@EnableWebMvc
181189
public static class ContainerWithEverythingTomcat {
@@ -204,10 +212,4 @@ String index() {
204212
}
205213
}
206214

207-
private void verifyServletWorks() {
208-
RestTemplate restTemplate = new RestTemplate();
209-
assertEquals(restTemplate.getForObject("http://localhost:8080/", String.class),
210-
"Hello");
211-
}
212-
213215
}

0 commit comments

Comments
 (0)