Update information on WebApplicationContext hierarchy

Issue: SPR-16041
This commit is contained in:
Rossen Stoyanchev
2017-10-17 21:49:57 -04:00
parent cd634633d8
commit 32d78e60b6
7 changed files with 76 additions and 73 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,19 +22,20 @@ import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
/**
* Base class for {@link org.springframework.web.WebApplicationInitializer}
* implementations that register a
* {@link org.springframework.web.servlet.DispatcherServlet DispatcherServlet}
* configured with annotated classes, e.g. Spring's
* {@link org.springframework.context.annotation.Configuration @Configuration} classes.
* {@link org.springframework.web.WebApplicationInitializer WebApplicationInitializer}
* to register a {@code DispatcherServlet} and use Java-based Spring configuration.
*
* <p>Concrete implementations are required to implement {@link #getRootConfigClasses()}
* and {@link #getServletConfigClasses()} as well as {@link #getServletMappings()}.
* Further template and customization methods are provided by
* {@link AbstractDispatcherServletInitializer}.
* <p>Implementations are required to implement:
* <ul>
* <li>{@link #getRootConfigClasses()} -- for "root" application context (non-web
* infrastructure) configuration.
* <li>{@link #getServletConfigClasses()} -- for {@code DispatcherServlet}
* application context (Spring MVC infrastructure) configuration.
* </ul>
*
* <p>This is the preferred approach for applications that use Java-based
* Spring configuration.
* <p>If an application context hierarchy is not required, applications may
* return all configuration via {@link #getRootConfigClasses()} and return
* {@code null} from {@link #getServletConfigClasses()}.
*
* @author Arjen Poutsma
* @author Chris Beams
@@ -54,9 +55,9 @@ public abstract class AbstractAnnotationConfigDispatcherServletInitializer
protected WebApplicationContext createRootApplicationContext() {
Class<?>[] configClasses = getRootConfigClasses();
if (!ObjectUtils.isEmpty(configClasses)) {
AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext();
rootAppContext.register(configClasses);
return rootAppContext;
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(configClasses);
return context;
}
else {
return null;
@@ -70,30 +71,27 @@ public abstract class AbstractAnnotationConfigDispatcherServletInitializer
*/
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext servletAppContext = new AnnotationConfigWebApplicationContext();
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
Class<?>[] configClasses = getServletConfigClasses();
if (!ObjectUtils.isEmpty(configClasses)) {
servletAppContext.register(configClasses);
context.register(configClasses);
}
return servletAppContext;
return context;
}
/**
* Specify {@link org.springframework.context.annotation.Configuration @Configuration}
* and/or {@link org.springframework.stereotype.Component @Component} classes to be
* provided to the {@linkplain #createRootApplicationContext() root application context}.
* @return the configuration classes for the root application context, or {@code null}
* Specify {@code @Configuration} and/or {@code @Component} classes for the
* {@linkplain #createRootApplicationContext() root application context}.
* @return the configuration for the root application context, or {@code null}
* if creation and registration of a root context is not desired
*/
@Nullable
protected abstract Class<?>[] getRootConfigClasses();
/**
* Specify {@link org.springframework.context.annotation.Configuration @Configuration}
* and/or {@link org.springframework.stereotype.Component @Component} classes to be
* provided to the {@linkplain #createServletApplicationContext() dispatcher servlet
* application context}.
* @return the configuration classes for the dispatcher servlet application context or
* Specify {@code @Configuration} and/or {@code @Component} classes for the
* {@linkplain #createServletApplicationContext() Servlet application context}.
* @return the configuration for the Servlet application context, or
* {@code null} if all configuration is specified through root config classes.
*/
@Nullable

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -40,17 +40,8 @@ import org.springframework.web.servlet.FrameworkServlet;
* Base class for {@link org.springframework.web.WebApplicationInitializer}
* implementations that register a {@link DispatcherServlet} in the servlet context.
*
* <p>Concrete implementations are required to implement
* {@link #createServletApplicationContext()}, as well as {@link #getServletMappings()},
* both of which get invoked from {@link #registerDispatcherServlet(ServletContext)}.
* Further customization can be achieved by overriding
* {@link #customizeRegistration(ServletRegistration.Dynamic)}.
*
* <p>Because this class extends from {@link AbstractContextLoaderInitializer}, concrete
* implementations are also required to implement {@link #createRootApplicationContext()}
* to set up a parent "<strong>root</strong>" application context. If a root context is
* not desired, implementations can simply return {@code null} in the
* {@code createRootApplicationContext()} implementation.
* <p>Most applications should consider extending the Spring Java config, sub-class
* {@link AbstractAnnotationConfigDispatcherServletInitializer}.
*
* @author Arjen Poutsma
* @author Chris Beams