Skip to content

Commit 9845d9e

Browse files
committed
Allow consolidating config in root context with Java
Issue: SPR-11357 (backported from 4.0.1)
1 parent edb6608 commit 9845d9e

File tree

2 files changed

+59
-32
lines changed

2 files changed

+59
-32
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/support/AbstractAnnotationConfigDispatcherServletInitializer.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.web.servlet.support;
1818

19-
import org.springframework.util.Assert;
2019
import org.springframework.util.ObjectUtils;
2120
import org.springframework.web.context.WebApplicationContext;
2221
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
@@ -28,8 +27,8 @@
2827
* configured with annotated classes, e.g. Spring's {@link
2928
* org.springframework.context.annotation.Configuration @Configuration} classes.
3029
*
31-
* <p>Concrete implementations are required to implement {@link #getRootConfigClasses()},
32-
* {@link #getServletConfigClasses()}, as well as {@link #getServletMappings()}. Further
30+
* <p>Concrete implementations are required to implement {@link #getRootConfigClasses()}
31+
* and {@link #getServletConfigClasses()} as well as {@link #getServletMappings()}. Further
3332
* template and customization methods are provided by {@link
3433
* AbstractDispatcherServletInitializer}.
3534
*
@@ -48,11 +47,10 @@ public abstract class AbstractAnnotationConfigDispatcherServletInitializer
4847
*/
4948
@Override
5049
protected WebApplicationContext createRootApplicationContext() {
51-
Class<?>[] rootConfigClasses = this.getRootConfigClasses();
52-
if (!ObjectUtils.isEmpty(rootConfigClasses)) {
53-
AnnotationConfigWebApplicationContext rootAppContext =
54-
new AnnotationConfigWebApplicationContext();
55-
rootAppContext.register(rootConfigClasses);
50+
Class<?>[] configClasses = getRootConfigClasses();
51+
if (!ObjectUtils.isEmpty(configClasses)) {
52+
AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext();
53+
rootAppContext.register(configClasses);
5654
return rootAppContext;
5755
}
5856
else {
@@ -64,18 +62,14 @@ protected WebApplicationContext createRootApplicationContext() {
6462
* {@inheritDoc}
6563
* <p>This implementation creates an {@link AnnotationConfigWebApplicationContext},
6664
* providing it the annotated classes returned by {@link #getServletConfigClasses()}.
67-
* @throws IllegalArgumentException if {@link #getServletConfigClasses()} returns
68-
* empty or {@code null}
6965
*/
7066
@Override
7167
protected WebApplicationContext createServletApplicationContext() {
72-
AnnotationConfigWebApplicationContext servletAppContext =
73-
new AnnotationConfigWebApplicationContext();
74-
Class<?>[] servletConfigClasses = this.getServletConfigClasses();
75-
Assert.notEmpty(servletConfigClasses,
76-
"getServletConfigClasses() did not return any configuration classes");
77-
78-
servletAppContext.register(servletConfigClasses);
68+
AnnotationConfigWebApplicationContext servletAppContext = new AnnotationConfigWebApplicationContext();
69+
Class<?>[] configClasses = getServletConfigClasses();
70+
if (!ObjectUtils.isEmpty(configClasses)) {
71+
servletAppContext.register(configClasses);
72+
}
7973
return servletAppContext;
8074
}
8175

spring-webmvc/src/test/java/org/springframework/web/servlet/support/AnnotationConfigDispatcherServletInitializerTests.java

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,33 +16,34 @@
1616

1717
package org.springframework.web.servlet.support;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertFalse;
21-
import static org.junit.Assert.assertNotNull;
22-
import static org.junit.Assert.assertTrue;
23-
2419
import java.util.Collections;
2520
import java.util.EnumSet;
21+
import java.util.EventListener;
2622
import java.util.LinkedHashMap;
2723
import java.util.Map;
28-
2924
import javax.servlet.DispatcherType;
3025
import javax.servlet.Filter;
3126
import javax.servlet.FilterRegistration.Dynamic;
3227
import javax.servlet.Servlet;
28+
import javax.servlet.ServletContextEvent;
29+
import javax.servlet.ServletContextListener;
3330
import javax.servlet.ServletException;
3431
import javax.servlet.ServletRegistration;
3532

3633
import org.junit.Before;
3734
import org.junit.Test;
35+
3836
import org.springframework.context.annotation.Bean;
3937
import org.springframework.context.annotation.Configuration;
38+
import org.springframework.mock.web.test.MockServletConfig;
4039
import org.springframework.mock.web.test.MockServletContext;
4140
import org.springframework.web.context.WebApplicationContext;
4241
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
4342
import org.springframework.web.filter.HiddenHttpMethodFilter;
4443
import org.springframework.web.servlet.DispatcherServlet;
4544

45+
import static org.junit.Assert.*;
46+
4647
/**
4748
* Test case for {@link AbstractAnnotationConfigDispatcherServletInitializer}.
4849
*
@@ -89,11 +90,11 @@ public void register() throws ServletException {
8990
assertNotNull(servlets.get(SERVLET_NAME));
9091

9192
DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME);
92-
WebApplicationContext dispatcherServletContext = servlet.getWebApplicationContext();
93-
((AnnotationConfigWebApplicationContext) dispatcherServletContext).refresh();
93+
WebApplicationContext wac = servlet.getWebApplicationContext();
94+
((AnnotationConfigWebApplicationContext) wac).refresh();
9495

95-
assertTrue(dispatcherServletContext.containsBean("bean"));
96-
assertTrue(dispatcherServletContext.getBean("bean") instanceof MyBean);
96+
assertTrue(wac.containsBean("bean"));
97+
assertTrue(wac.getBean("bean") instanceof MyBean);
9798

9899
assertEquals(1, servletRegistrations.size());
99100
assertNotNull(servletRegistrations.get(SERVLET_NAME));
@@ -135,6 +136,32 @@ protected boolean isAsyncSupported() {
135136
filterRegistration.getMappings().get(SERVLET_NAME));
136137
}
137138

139+
// SPR-11357
140+
@Test
141+
public void rootContextOnly() throws ServletException {
142+
initializer = new MyAnnotationConfigDispatcherServletInitializer() {
143+
@Override
144+
protected Class<?>[] getRootConfigClasses() {
145+
return new Class<?>[] {MyConfiguration.class};
146+
}
147+
@Override
148+
protected Class<?>[] getServletConfigClasses() {
149+
return null;
150+
}
151+
};
152+
153+
initializer.onStartup(servletContext);
154+
155+
DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME);
156+
servlet.init(new MockServletConfig(this.servletContext));
157+
158+
WebApplicationContext wac = servlet.getWebApplicationContext();
159+
((AnnotationConfigWebApplicationContext) wac).refresh();
160+
161+
assertTrue(wac.containsBean("bean"));
162+
assertTrue(wac.getBean("bean") instanceof MyBean);
163+
}
164+
138165
@Test
139166
public void noFilters() throws ServletException {
140167
initializer = new MyAnnotationConfigDispatcherServletInitializer() {
@@ -152,6 +179,12 @@ protected Filter[] getServletFilters() {
152179

153180
private class MyMockServletContext extends MockServletContext {
154181

182+
public <T extends EventListener> void addListener(T t) {
183+
if (t instanceof ServletContextListener) {
184+
((ServletContextListener) t).contextInitialized(new ServletContextEvent(this));
185+
}
186+
}
187+
155188
@Override
156189
public ServletRegistration.Dynamic addServlet(String servletName, Servlet servlet) {
157190
servlets.put(servletName, servlet);
@@ -169,6 +202,7 @@ public Dynamic addFilter(String filterName, Filter filter) {
169202
}
170203
}
171204

205+
172206
private static class MyAnnotationConfigDispatcherServletInitializer
173207
extends AbstractAnnotationConfigDispatcherServletInitializer {
174208

@@ -201,13 +235,13 @@ protected void customizeRegistration(ServletRegistration.Dynamic registration) {
201235
protected Class<?>[] getRootConfigClasses() {
202236
return null;
203237
}
204-
205238
}
206239

207-
private static class MyBean {
208240

241+
private static class MyBean {
209242
}
210243

244+
211245
@Configuration
212246
@SuppressWarnings("unused")
213247
private static class MyConfiguration {
@@ -219,7 +253,6 @@ public MyConfiguration() {
219253
public MyBean bean() {
220254
return new MyBean();
221255
}
222-
223256
}
224257

225258
}

0 commit comments

Comments
 (0)