AbstractContextLoaderInitializer and AbstractDispatcherServletInitializer support ApplicationContextInitializers now
Issue: SPR-12430
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -19,6 +19,7 @@ package org.springframework.web.servlet;
|
||||
import java.io.IOException;
|
||||
import java.security.Principal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Callable;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
@@ -182,7 +183,7 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic
|
||||
private String contextConfigLocation;
|
||||
|
||||
/** Actual ApplicationContextInitializer instances to apply to the context */
|
||||
private final ArrayList<ApplicationContextInitializer<ConfigurableApplicationContext>> contextInitializers =
|
||||
private final List<ApplicationContextInitializer<ConfigurableApplicationContext>> contextInitializers =
|
||||
new ArrayList<ApplicationContextInitializer<ConfigurableApplicationContext>>();
|
||||
|
||||
/** Comma-delimited ApplicationContextInitializer class names set through init param */
|
||||
@@ -364,13 +365,15 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic
|
||||
/**
|
||||
* Specify which {@link ApplicationContextInitializer} instances should be used
|
||||
* to initialize the application context used by this {@code FrameworkServlet}.
|
||||
* @see #configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext)
|
||||
* @see #applyInitializers(ConfigurableApplicationContext)
|
||||
* @see #configureAndRefreshWebApplicationContext
|
||||
* @see #applyInitializers
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setContextInitializers(ApplicationContextInitializer<? extends ConfigurableApplicationContext>... contextInitializers) {
|
||||
for (ApplicationContextInitializer<? extends ConfigurableApplicationContext> initializer : contextInitializers) {
|
||||
this.contextInitializers.add((ApplicationContextInitializer<ConfigurableApplicationContext>) initializer);
|
||||
public void setContextInitializers(ApplicationContextInitializer<?>... initializers) {
|
||||
if (initializers != null) {
|
||||
for (ApplicationContextInitializer<?> initializer : initializers) {
|
||||
this.contextInitializers.add((ApplicationContextInitializer<ConfigurableApplicationContext>) initializer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -25,6 +25,7 @@ import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRegistration;
|
||||
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.core.Conventions;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
@@ -51,6 +52,7 @@ import org.springframework.web.servlet.DispatcherServlet;
|
||||
* @author Arjen Poutsma
|
||||
* @author Chris Beams
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Juergen Hoeller
|
||||
* @since 3.2
|
||||
*/
|
||||
public abstract class AbstractDispatcherServletInitializer extends AbstractContextLoaderInitializer {
|
||||
@@ -64,7 +66,6 @@ public abstract class AbstractDispatcherServletInitializer extends AbstractConte
|
||||
@Override
|
||||
public void onStartup(ServletContext servletContext) throws ServletException {
|
||||
super.onStartup(servletContext);
|
||||
|
||||
registerDispatcherServlet(servletContext);
|
||||
}
|
||||
|
||||
@@ -80,7 +81,7 @@ public abstract class AbstractDispatcherServletInitializer extends AbstractConte
|
||||
*/
|
||||
protected void registerDispatcherServlet(ServletContext servletContext) {
|
||||
String servletName = getServletName();
|
||||
Assert.hasLength(servletName, "getServletName() may not return empty or null");
|
||||
Assert.hasLength(servletName, "getServletName() must not return empty or null");
|
||||
|
||||
WebApplicationContext servletAppContext = createServletApplicationContext();
|
||||
Assert.notNull(servletAppContext,
|
||||
@@ -88,6 +89,8 @@ public abstract class AbstractDispatcherServletInitializer extends AbstractConte
|
||||
"context for servlet [" + servletName + "]");
|
||||
|
||||
DispatcherServlet dispatcherServlet = new DispatcherServlet(servletAppContext);
|
||||
dispatcherServlet.setContextInitializers(getServletApplicationContextInitializers());
|
||||
|
||||
ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, dispatcherServlet);
|
||||
Assert.notNull(registration,
|
||||
"Failed to register servlet with name '" + servletName + "'." +
|
||||
@@ -126,6 +129,18 @@ public abstract class AbstractDispatcherServletInitializer extends AbstractConte
|
||||
*/
|
||||
protected abstract WebApplicationContext createServletApplicationContext();
|
||||
|
||||
/**
|
||||
* Specify application context initializers to be applied to the servlet-specific
|
||||
* application context that the {@code DispatcherServlet} is being created with.
|
||||
* @since 4.2
|
||||
* @see #createServletApplicationContext()
|
||||
* @see DispatcherServlet#setContextInitializers
|
||||
* @see #getRootApplicationContextInitializers()
|
||||
*/
|
||||
protected ApplicationContextInitializer<?>[] getServletApplicationContextInitializers() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the servlet mapping(s) for the {@code DispatcherServlet} —
|
||||
* for example {@code "/"}, {@code "/app"}, etc.
|
||||
@@ -178,9 +193,9 @@ public abstract class AbstractDispatcherServletInitializer extends AbstractConte
|
||||
}
|
||||
|
||||
private EnumSet<DispatcherType> getDispatcherTypes() {
|
||||
return isAsyncSupported() ?
|
||||
EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.ASYNC) :
|
||||
EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE);
|
||||
return (isAsyncSupported() ?
|
||||
EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.ASYNC) :
|
||||
EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -57,7 +57,7 @@ import static org.junit.Assert.*;
|
||||
* @since 12.08.2003
|
||||
* @see org.springframework.web.context.support.Spr8510Tests
|
||||
*/
|
||||
public final class ContextLoaderTests {
|
||||
public class ContextLoaderTests {
|
||||
|
||||
@Test
|
||||
public void testContextLoaderListenerWithDefaultContext() {
|
||||
@@ -155,6 +155,50 @@ public final class ContextLoaderTests {
|
||||
assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContextLoaderListenerWithProgrammaticInitializers() {
|
||||
MockServletContext sc = new MockServletContext("");
|
||||
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
|
||||
"org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
|
||||
ContextLoaderListener listener = new ContextLoaderListener();
|
||||
listener.setContextInitializers(new TestContextInitializer(), new TestWebContextInitializer());
|
||||
listener.contextInitialized(new ServletContextEvent(sc));
|
||||
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
|
||||
TestBean testBean = wac.getBean(TestBean.class);
|
||||
assertThat(testBean.getName(), equalTo("testName"));
|
||||
assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContextLoaderListenerWithProgrammaticAndLocalInitializers() {
|
||||
MockServletContext sc = new MockServletContext("");
|
||||
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
|
||||
"org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
|
||||
sc.addInitParameter(ContextLoader.CONTEXT_INITIALIZER_CLASSES_PARAM, TestContextInitializer.class.getName());
|
||||
ContextLoaderListener listener = new ContextLoaderListener();
|
||||
listener.setContextInitializers(new TestWebContextInitializer());
|
||||
listener.contextInitialized(new ServletContextEvent(sc));
|
||||
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
|
||||
TestBean testBean = wac.getBean(TestBean.class);
|
||||
assertThat(testBean.getName(), equalTo("testName"));
|
||||
assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContextLoaderListenerWithProgrammaticAndGlobalInitializers() {
|
||||
MockServletContext sc = new MockServletContext("");
|
||||
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
|
||||
"org/springframework/web/context/WEB-INF/ContextLoaderTests-acc-context.xml");
|
||||
sc.addInitParameter(ContextLoader.GLOBAL_INITIALIZER_CLASSES_PARAM, TestWebContextInitializer.class.getName());
|
||||
ContextLoaderListener listener = new ContextLoaderListener();
|
||||
listener.setContextInitializers(new TestContextInitializer());
|
||||
listener.contextInitialized(new ServletContextEvent(sc));
|
||||
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
|
||||
TestBean testBean = wac.getBean(TestBean.class);
|
||||
assertThat(testBean.getName(), equalTo("testName"));
|
||||
assertThat(wac.getServletContext().getAttribute("initialized"), notNullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegisteredContextInitializerCanAccessServletContextParamsViaEnvironment() {
|
||||
MockServletContext sc = new MockServletContext("");
|
||||
@@ -169,7 +213,7 @@ public final class ContextLoaderTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testContextLoaderListenerWithUnkownContextInitializer() {
|
||||
public void testContextLoaderListenerWithUnknownContextInitializer() {
|
||||
MockServletContext sc = new MockServletContext("");
|
||||
// config file doesn't matter. just a placeholder
|
||||
sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,
|
||||
|
||||
Reference in New Issue
Block a user