Skip to content

Commit 32d78e6

Browse files
committed
Update information on WebApplicationContext hierarchy
Issue: SPR-16041
1 parent cd63463 commit 32d78e6

File tree

7 files changed

+76
-73
lines changed

7 files changed

+76
-73
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/support/AbstractAnnotationConfigDispatcherHandlerInitializer.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -20,13 +20,11 @@
2020
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2121
import org.springframework.http.server.reactive.ServletHttpHandlerAdapter;
2222
import org.springframework.util.ObjectUtils;
23-
import org.springframework.web.reactive.DispatcherHandler;
2423

2524
/**
26-
* Base class for {@link org.springframework.web.WebApplicationInitializer}
27-
* implementations that register a {@link DispatcherHandler} configured with annotated
28-
* {@link org.springframework.context.annotation.Configuration @Configuration} classes in the
29-
* servlet context, wrapping it in a {@link ServletHttpHandlerAdapter}.
25+
* {@link org.springframework.web.WebApplicationInitializer WebApplicationInitializer}
26+
* to register a {@code DispatcherHandler}, wrapping it in a
27+
* {@link ServletHttpHandlerAdapter}, and use Java-based Spring configuration.
3028
*
3129
* <p>Concrete implementations are required to implement {@link #getConfigClasses()}.
3230
* Further template and customization methods are provided by
@@ -38,26 +36,26 @@
3836
public abstract class AbstractAnnotationConfigDispatcherHandlerInitializer
3937
extends AbstractDispatcherHandlerInitializer {
4038

39+
4140
/**
4241
* {@inheritDoc}
4342
* <p>This implementation creates an {@link AnnotationConfigApplicationContext},
4443
* providing it the annotated classes returned by {@link #getConfigClasses()}.
4544
*/
4645
@Override
4746
protected ApplicationContext createApplicationContext() {
48-
AnnotationConfigApplicationContext servletAppContext = new AnnotationConfigApplicationContext();
47+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
4948
Class<?>[] configClasses = getConfigClasses();
5049
if (!ObjectUtils.isEmpty(configClasses)) {
51-
servletAppContext.register(configClasses);
50+
context.register(configClasses);
5251
}
53-
return servletAppContext;
52+
return context;
5453
}
5554

5655
/**
57-
* Specify {@link org.springframework.context.annotation.Configuration @Configuration}
58-
* and/or {@link org.springframework.stereotype.Component @Component} classes to be
59-
* provided to the {@linkplain #createApplicationContext() application context}.
60-
* @return the configuration classes for the dispatcher servlet application context
56+
* Specify {@code @Configuration} and/or {@code @Component} classes for
57+
* the {@linkplain #createApplicationContext() application context}.
58+
* @return the configuration for the application context
6159
*/
6260
protected abstract Class<?>[] getConfigClasses();
6361

spring-webflux/src/main/java/org/springframework/web/reactive/support/AbstractDispatcherHandlerInitializer.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -34,16 +34,15 @@
3434

3535
/**
3636
* Base class for {@link org.springframework.web.WebApplicationInitializer}
37-
* implementations that register a {@link DispatcherHandler} in the servlet context, wrapping it in
38-
* a {@link ServletHttpHandlerAdapter}.
37+
* implementations that register a {@link DispatcherHandler} in the servlet
38+
* context, wrapping it in a {@link ServletHttpHandlerAdapter}.
3939
*
40-
* <p>Concrete implementations are required to implement
41-
* {@link #createApplicationContext()}, which gets invoked from
42-
* {@link #registerDispatcherHandler(ServletContext)}. Further customization can be achieved by
43-
* overriding {@link #customizeRegistration(ServletRegistration.Dynamic)}.
40+
* <p>Most applications should consider extending the Spring Java config, sub-class
41+
* {@link AbstractAnnotationConfigDispatcherHandlerInitializer}.
4442
*
4543
* @author Arjen Poutsma
4644
* @since 5.0
45+
* @see AbstractServletHttpHandlerAdapterInitializer
4746
*/
4847
public abstract class AbstractDispatcherHandlerInitializer implements WebApplicationInitializer {
4948

spring-webflux/src/main/java/org/springframework/web/reactive/support/AbstractServletHttpHandlerAdapterInitializer.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,15 @@
2727

2828
/**
2929
* Base class for {@link org.springframework.web.WebApplicationInitializer}
30-
* implementations that register a {@link ServletHttpHandlerAdapter} in the servlet context.
30+
* implementations that register a {@link ServletHttpHandlerAdapter} in the
31+
* servlet context.
3132
*
32-
* <p>Concrete implementations are required to implement
33-
* {@link #createHttpHandler()}, as well as {@link #getServletMappings()},
34-
* both of which get invoked from {@link #registerHandlerAdapter(ServletContext)}.
35-
* Further customization can be achieved by overriding
36-
* {@link #customizeRegistration(ServletRegistration.Dynamic)}.
33+
* <p>See {@link AbstractDispatcherHandlerInitializer} if registering a
34+
* {@link org.springframework.web.reactive.DispatcherHandler DispatcherHandler}.
3735
*
3836
* @author Arjen Poutsma
3937
* @since 5.0
38+
* @see AbstractDispatcherHandlerInitializer
4039
*/
4140
public abstract class AbstractServletHttpHandlerAdapterInitializer implements WebApplicationInitializer {
4241

@@ -67,12 +66,12 @@ protected void registerHandlerAdapter(ServletContext servletContext) {
6766
Assert.hasLength(servletName, "getServletName() must not return empty or null");
6867

6968
HttpHandler httpHandler = createHttpHandler();
70-
Assert.notNull(httpHandler,
71-
"createHttpHandler() did not return a HttpHandler for servlet [" + servletName + "]");
69+
Assert.notNull(httpHandler, "createHttpHandler() did not return a HttpHandler" +
70+
"for servlet [" + servletName + "]");
7271

7372
ServletHttpHandlerAdapter servlet = createServlet(httpHandler);
74-
Assert.notNull(servlet,
75-
"createHttpHandler() did not return a ServletHttpHandlerAdapter for servlet [" + servletName + "]");
73+
Assert.notNull(servlet, "createHttpHandler() did not return a ServletHttpHandlerAdapter " +
74+
"for servlet [" + servletName + "]");
7675

7776
ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, servlet);
7877
Assert.notNull(registration,
@@ -102,8 +101,8 @@ protected String getServletName() {
102101

103102
/**
104103
* Create a {@link ServletHttpHandlerAdapter} with the specified .
105-
* <p>Default implementation returns a {@code ServletHttpHandlerAdapter} with the provided
106-
* {@code httpHandler}.
104+
* <p>Default implementation returns a {@code ServletHttpHandlerAdapter}
105+
* with the provided {@code httpHandler}.
107106
*/
108107
protected ServletHttpHandlerAdapter createServlet(HttpHandler httpHandler) {
109108
return new ServletHttpHandlerAdapter(httpHandler);

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

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -22,19 +22,20 @@
2222
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
2323

2424
/**
25-
* Base class for {@link org.springframework.web.WebApplicationInitializer}
26-
* implementations that register a
27-
* {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet}
28-
* configured with annotated classes, e.g. Spring's
29-
* {@link org.springframework.context.annotation.Configuration @Configuration} classes.
25+
* {@link org.springframework.web.WebApplicationInitializer WebApplicationInitializer}
26+
* to register a {@code DispatcherServlet} and use Java-based Spring configuration.
3027
*
31-
* <p>Concrete implementations are required to implement {@link #getRootConfigClasses()}
32-
* and {@link #getServletConfigClasses()} as well as {@link #getServletMappings()}.
33-
* Further template and customization methods are provided by
34-
* {@link AbstractDispatcherServletInitializer}.
28+
* <p>Implementations are required to implement:
29+
* <ul>
30+
* <li>{@link #getRootConfigClasses()} -- for "root" application context (non-web
31+
* infrastructure) configuration.
32+
* <li>{@link #getServletConfigClasses()} -- for {@code DispatcherServlet}
33+
* application context (Spring MVC infrastructure) configuration.
34+
* </ul>
3535
*
36-
* <p>This is the preferred approach for applications that use Java-based
37-
* Spring configuration.
36+
* <p>If an application context hierarchy is not required, applications may
37+
* return all configuration via {@link #getRootConfigClasses()} and return
38+
* {@code null} from {@link #getServletConfigClasses()}.
3839
*
3940
* @author Arjen Poutsma
4041
* @author Chris Beams
@@ -54,9 +55,9 @@ public abstract class AbstractAnnotationConfigDispatcherServletInitializer
5455
protected WebApplicationContext createRootApplicationContext() {
5556
Class<?>[] configClasses = getRootConfigClasses();
5657
if (!ObjectUtils.isEmpty(configClasses)) {
57-
AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext();
58-
rootAppContext.register(configClasses);
59-
return rootAppContext;
58+
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
59+
context.register(configClasses);
60+
return context;
6061
}
6162
else {
6263
return null;
@@ -70,30 +71,27 @@ protected WebApplicationContext createRootApplicationContext() {
7071
*/
7172
@Override
7273
protected WebApplicationContext createServletApplicationContext() {
73-
AnnotationConfigWebApplicationContext servletAppContext = new AnnotationConfigWebApplicationContext();
74+
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
7475
Class<?>[] configClasses = getServletConfigClasses();
7576
if (!ObjectUtils.isEmpty(configClasses)) {
76-
servletAppContext.register(configClasses);
77+
context.register(configClasses);
7778
}
78-
return servletAppContext;
79+
return context;
7980
}
8081

8182
/**
82-
* Specify {@link org.springframework.context.annotation.Configuration @Configuration}
83-
* and/or {@link org.springframework.stereotype.Component @Component} classes to be
84-
* provided to the {@linkplain #createRootApplicationContext() root application context}.
85-
* @return the configuration classes for the root application context, or {@code null}
83+
* Specify {@code @Configuration} and/or {@code @Component} classes for the
84+
* {@linkplain #createRootApplicationContext() root application context}.
85+
* @return the configuration for the root application context, or {@code null}
8686
* if creation and registration of a root context is not desired
8787
*/
8888
@Nullable
8989
protected abstract Class<?>[] getRootConfigClasses();
9090

9191
/**
92-
* Specify {@link org.springframework.context.annotation.Configuration @Configuration}
93-
* and/or {@link org.springframework.stereotype.Component @Component} classes to be
94-
* provided to the {@linkplain #createServletApplicationContext() dispatcher servlet
95-
* application context}.
96-
* @return the configuration classes for the dispatcher servlet application context or
92+
* Specify {@code @Configuration} and/or {@code @Component} classes for the
93+
* {@linkplain #createServletApplicationContext() Servlet application context}.
94+
* @return the configuration for the Servlet application context, or
9795
* {@code null} if all configuration is specified through root config classes.
9896
*/
9997
@Nullable

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

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -40,17 +40,8 @@
4040
* Base class for {@link org.springframework.web.WebApplicationInitializer}
4141
* implementations that register a {@link DispatcherServlet} in the servlet context.
4242
*
43-
* <p>Concrete implementations are required to implement
44-
* {@link #createServletApplicationContext()}, as well as {@link #getServletMappings()},
45-
* both of which get invoked from {@link #registerDispatcherServlet(ServletContext)}.
46-
* Further customization can be achieved by overriding
47-
* {@link #customizeRegistration(ServletRegistration.Dynamic)}.
48-
*
49-
* <p>Because this class extends from {@link AbstractContextLoaderInitializer}, concrete
50-
* implementations are also required to implement {@link #createRootApplicationContext()}
51-
* to set up a parent "<strong>root</strong>" application context. If a root context is
52-
* not desired, implementations can simply return {@code null} in the
53-
* {@code createRootApplicationContext()} implementation.
43+
* <p>Most applications should consider extending the Spring Java config, sub-class
44+
* {@link AbstractAnnotationConfigDispatcherServletInitializer}.
5445
*
5546
* @author Arjen Poutsma
5647
* @author Chris Beams

src/docs/asciidoc/web/webflux.adoc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,13 @@ server.addConnector(connector);
346346
server.start();
347347
----
348348

349-
You can also deploy as a WAR to any Servlet 3.1 container by wrapping the handler with
350-
`ServletHttpHandlerAdapter` as a `Servlet`.
349+
[NOTE]
350+
====
351+
To deploy as a WAR to a Servlet 3.1+ container, wrap `HttpHandler` with
352+
`ServletHttpHandlerAdapter` and register that as a `Servlet`. Use
353+
{api-spring-framework}/web/reactive/support/AbstractServletHttpHandlerAdapterInitializer.html[AbstractServletHttpHandlerAdapterInitializer]
354+
to automate the required Servlet container configuration.
355+
====
351356

352357

353358

src/docs/asciidoc/web/webmvc.adoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ Below is example configuration with a `WebApplicationContext` hierarchy:
161161
}
162162
----
163163

164+
[TIP]
165+
====
166+
If an application context hierarchy is not required, applications may return all
167+
configuration via `getRootConfigClasses()` and `null` from `getServletConfigClasses()`.
168+
====
169+
164170
And the `web.xml` equivalent:
165171

166172
[source,xml,indent=0]
@@ -195,6 +201,13 @@ And the `web.xml` equivalent:
195201
</web-app>
196202
----
197203

204+
[TIP]
205+
====
206+
If an application context hierarchy is not required, applications may configure a
207+
"root" context only and leave the `contextConfigLocation` Servlet parameter empty.
208+
====
209+
210+
198211

199212

200213
[[mvc-servlet-special-bean-types]]

0 commit comments

Comments
 (0)