Fix deployable WAR (Servlets have to be registered)

Includes smart guess for prepending servlet context initializer
in SpringApplicationBuilder.
pull/76/merge
Dave Syer 11 years ago
parent ad7503b0bb
commit 6cd060c4cf

@ -296,7 +296,7 @@ public class SpringApplication {
}
private void callNonEnvironmentAwareSpringApplicationInitializers(String[] args) {
for (ApplicationContextInitializer<?> initializer : this.initializers) {
for (ApplicationContextInitializer<?> initializer : getInitializers()) {
if (initializer instanceof SpringApplicationInitializer
&& !(initializer instanceof EnvironmentAware)) {
((SpringApplicationInitializer) initializer).initialize(this, args);
@ -334,7 +334,7 @@ public class SpringApplication {
private void callEnvironmentAwareSpringApplicationInitializers(String[] args,
ConfigurableEnvironment environment) {
for (ApplicationContextInitializer<?> initializer : this.initializers) {
for (ApplicationContextInitializer<?> initializer : getInitializers()) {
if (initializer instanceof SpringApplicationInitializer
&& initializer instanceof EnvironmentAware) {
((EnvironmentAware) initializer).setEnvironment(environment);
@ -360,7 +360,7 @@ public class SpringApplication {
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void applyInitializers(ConfigurableApplicationContext context) {
for (ApplicationContextInitializer initializer : this.initializers) {
for (ApplicationContextInitializer initializer : getInitializers()) {
Class<?> requiredType = GenericTypeResolver.resolveTypeArgument(
initializer.getClass(), ApplicationContextInitializer.class);
Assert.isInstanceOf(requiredType, context, "Unable to call initializer.");
@ -634,7 +634,9 @@ public class SpringApplication {
/**
* Sets the {@link ApplicationContextInitializer} that will be applied to the Spring
* {@link ApplicationContext}. Any existing initializers will be replaced.
* {@link ApplicationContext}. Any existing initializers will be replaced. The default
* initializers (from <code>META-INF/spring.factories</code> are always appended at
* runtime).
* @param initializers the initializers to set
*/
public void setInitializers(
@ -648,7 +650,10 @@ public class SpringApplication {
* @return the initializers
*/
public List<ApplicationContextInitializer<?>> getInitializers() {
return this.initializers;
List<ApplicationContextInitializer<?>> initializers = new ArrayList<ApplicationContextInitializer<?>>(
this.initializers);
initializers.addAll(getSpringFactoriesApplicationContextInitializers());
return initializers;
}
/**

@ -23,6 +23,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.initializer.ParentContextApplicationContextInitializer;
import org.springframework.boot.context.initializer.ServletContextApplicationContextInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
@ -395,7 +396,14 @@ public class SpringApplicationBuilder {
*/
public SpringApplicationBuilder initializers(
ApplicationContextInitializer<?>... initializers) {
addInitializers(false, initializers);
for (ApplicationContextInitializer<?> initializer : initializers) {
boolean prepend = false;
if (initializer instanceof ParentContextApplicationContextInitializer
|| initializer instanceof ServletContextApplicationContextInitializer) {
prepend = true;
}
addInitializers(prepend, initializer);
}
return this;
}

@ -147,11 +147,20 @@ public class EmbeddedWebApplicationContext extends GenericWebApplicationContext
}
private synchronized void createEmbeddedServletContainer() {
if (this.embeddedServletContainer == null) {
if (this.embeddedServletContainer == null && getServletContext() == null) {
EmbeddedServletContainerFactory containerFactory = getEmbeddedServletContainerFactory();
this.embeddedServletContainer = containerFactory
.getEmbeddedServletContainer(getSelfInitializer());
}
else if (getServletContext() != null) {
try {
getSelfInitializer().onStartup(getServletContext());
}
catch (ServletException ex) {
throw new ApplicationContextException(
"Cannot initialize servlet context", ex);
}
}
WebApplicationContextUtils.registerWebApplicationScopes(getBeanFactory(),
getServletContext());
WebApplicationContextUtils.registerEnvironmentBeans(getBeanFactory(),

@ -81,9 +81,12 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit
}
SpringApplicationBuilder application = new SpringApplicationBuilder()
.sources(getConfigClasses());
application.initializers(
new ParentContextApplicationContextInitializer(parent),
new ServletContextApplicationContextInitializer(servletContext));
if (parent != null) {
application.initializers(new ParentContextApplicationContextInitializer(
parent));
}
application.initializers(new ServletContextApplicationContextInitializer(
servletContext));
application.contextClass(AnnotationConfigEmbeddedWebApplicationContext.class);
return (WebApplicationContext) application.run();
}

Loading…
Cancel
Save