Obtain ServletContextInitializer beans later

Update EmbeddedWebApplicationContext to obtain ServletContextInitializer
beans after self initialization. Allows @Configuration beans to be
ServletContextAware.
This commit is contained in:
Phillip Webb 2013-06-12 15:41:25 -07:00
parent 4923717524
commit 6a2f36a68a
2 changed files with 10 additions and 20 deletions

View File

@ -130,15 +130,13 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
if (this.embeddedServletContainer == null && getServletContext() == null) { if (this.embeddedServletContainer == null && getServletContext() == null) {
EmbeddedServletContainerFactory containerFactory = getEmbeddedServletContainerFactory(); EmbeddedServletContainerFactory containerFactory = getEmbeddedServletContainerFactory();
this.embeddedServletContainer = containerFactory this.embeddedServletContainer = containerFactory
.getEmbdeddedServletContainer(getServletContextInitializers()); .getEmbdeddedServletContainer(getSelfInitializer());
} else if (getServletContext() != null) { } else if (getServletContext() != null) {
for (ServletContextInitializer initializer : getServletContextInitializers()) { try {
try { getSelfInitializer().onStartup(getServletContext());
initializer.onStartup(getServletContext()); } catch (ServletException e) {
} catch (ServletException e) { throw new ApplicationContextException(
throw new ApplicationContextException( "Cannot initialize servlet context", e);
"Cannot initialize servlet context", e);
}
} }
} }
WebApplicationContextUtils.registerWebApplicationScopes(getBeanFactory(), 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<ServletContextInitializer> initializers = new ArrayList<ServletContextInitializer>();
initializers.add(getSelfInitializer());
initializers.addAll(getServletContextInitializerBeans());
return initializers.toArray(new ServletContextInitializer[initializers.size()]);
}
/** /**
* Returns the {@link ServletContextInitializer} that will be used to complete the * Returns the {@link ServletContextInitializer} that will be used to complete the
* setup of this {@link WebApplicationContext}. * setup of this {@link WebApplicationContext}.
@ -188,6 +176,9 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
@Override @Override
public void onStartup(ServletContext servletContext) throws ServletException { public void onStartup(ServletContext servletContext) throws ServletException {
prepareEmbeddedWebApplicationContext(servletContext); prepareEmbeddedWebApplicationContext(servletContext);
for (ServletContextInitializer beans : getServletContextInitializerBeans()) {
beans.onStartup(servletContext);
}
} }
}; };
} }

View File

@ -82,13 +82,12 @@ public class AnnotationConfigEmbeddedWebApplicationContextTests {
} }
@Test @Test
public void createAndInitializeWithRoot() throws Exception { public void createAndInitializeWithParent() throws Exception {
AnnotationConfigEmbeddedWebApplicationContext parent = new AnnotationConfigEmbeddedWebApplicationContext( AnnotationConfigEmbeddedWebApplicationContext parent = new AnnotationConfigEmbeddedWebApplicationContext(
EmbeddedContainerConfiguration.class); EmbeddedContainerConfiguration.class);
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context = new AnnotationConfigEmbeddedWebApplicationContext();
this.context.register(ServletContextAwareConfiguration.class); this.context.register(ServletContextAwareConfiguration.class);
this.context.setParent(parent); this.context.setParent(parent);
this.context.setServletContext(parent.getServletContext());
this.context.refresh(); this.context.refresh();
verifyContext(); verifyContext();
assertNotNull(this.context.getBean(ServletContextAwareConfiguration.class) assertNotNull(this.context.getBean(ServletContextAwareConfiguration.class)