diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/EmbeddedWebApplicationContext.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/EmbeddedWebApplicationContext.java index 1ce93e8caa0..98a73b10c73 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/EmbeddedWebApplicationContext.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/embedded/EmbeddedWebApplicationContext.java @@ -130,15 +130,13 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext if (this.embeddedServletContainer == null && getServletContext() == null) { EmbeddedServletContainerFactory containerFactory = getEmbeddedServletContainerFactory(); this.embeddedServletContainer = containerFactory - .getEmbdeddedServletContainer(getServletContextInitializers()); + .getEmbdeddedServletContainer(getSelfInitializer()); } else if (getServletContext() != null) { - for (ServletContextInitializer initializer : getServletContextInitializers()) { - try { - initializer.onStartup(getServletContext()); - } catch (ServletException e) { - throw new ApplicationContextException( - "Cannot initialize servlet context", e); - } + try { + getSelfInitializer().onStartup(getServletContext()); + } catch (ServletException e) { + throw new ApplicationContextException( + "Cannot initialize servlet context", e); } } WebApplicationContextUtils.registerWebApplicationScopes(getBeanFactory(), @@ -168,16 +166,6 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext } } - /** - * Returns all {@link ServletContextInitializer}s that should be applied. - */ - private ServletContextInitializer[] getServletContextInitializers() { - List initializers = new ArrayList(); - initializers.add(getSelfInitializer()); - initializers.addAll(getServletContextInitializerBeans()); - return initializers.toArray(new ServletContextInitializer[initializers.size()]); - } - /** * Returns the {@link ServletContextInitializer} that will be used to complete the * setup of this {@link WebApplicationContext}. @@ -188,6 +176,9 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext @Override public void onStartup(ServletContext servletContext) throws ServletException { prepareEmbeddedWebApplicationContext(servletContext); + for (ServletContextInitializer beans : getServletContextInitializerBeans()) { + beans.onStartup(servletContext); + } } }; } diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/embedded/AnnotationConfigEmbeddedWebApplicationContextTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/embedded/AnnotationConfigEmbeddedWebApplicationContextTests.java index b9790247541..4921536be32 100644 --- a/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/embedded/AnnotationConfigEmbeddedWebApplicationContextTests.java +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/embedded/AnnotationConfigEmbeddedWebApplicationContextTests.java @@ -82,13 +82,12 @@ public class AnnotationConfigEmbeddedWebApplicationContextTests { } @Test - public void createAndInitializeWithRoot() throws Exception { + public void createAndInitializeWithParent() throws Exception { AnnotationConfigEmbeddedWebApplicationContext parent = new AnnotationConfigEmbeddedWebApplicationContext( EmbeddedContainerConfiguration.class); this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context.register(ServletContextAwareConfiguration.class); this.context.setParent(parent); - this.context.setServletContext(parent.getServletContext()); this.context.refresh(); verifyContext(); assertNotNull(this.context.getBean(ServletContextAwareConfiguration.class)