Expose SpringApplicationBuilder in SpringBootServletInitializer

pull/76/merge
Dave Syer 11 years ago
parent 6cd060c4cf
commit 4655eb3a94

@ -18,6 +18,7 @@ package org.springframework.boot.samples.websocket.config;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.samples.websocket.client.GreetingService; import org.springframework.boot.samples.websocket.client.GreetingService;
import org.springframework.boot.samples.websocket.client.SimpleGreetingService; import org.springframework.boot.samples.websocket.client.SimpleGreetingService;
import org.springframework.boot.samples.websocket.echo.DefaultEchoService; import org.springframework.boot.samples.websocket.echo.DefaultEchoService;
@ -35,9 +36,9 @@ import org.springframework.web.socket.support.PerConnectionWebSocketHandler;
public class SampleWebSocketsApplication extends SpringBootServletInitializer { public class SampleWebSocketsApplication extends SpringBootServletInitializer {
@Override @Override
protected Class<?> getConfigClass() { protected void configure(SpringApplicationBuilder application) {
return SampleWebSocketsApplication.class; application.sources(SampleWebSocketsApplication.class);
} }
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(SampleWebSocketsApplication.class, args); SpringApplication.run(SampleWebSocketsApplication.class, args);

@ -27,7 +27,6 @@ import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebAppl
import org.springframework.boot.context.initializer.ParentContextApplicationContextInitializer; import org.springframework.boot.context.initializer.ParentContextApplicationContextInitializer;
import org.springframework.boot.context.initializer.ServletContextApplicationContextInitializer; import org.springframework.boot.context.initializer.ServletContextApplicationContextInitializer;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.util.ObjectUtils;
import org.springframework.web.WebApplicationInitializer; import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
@ -79,8 +78,7 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit
servletContext.setAttribute( servletContext.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, null); WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, null);
} }
SpringApplicationBuilder application = new SpringApplicationBuilder() SpringApplicationBuilder application = new SpringApplicationBuilder();
.sources(getConfigClasses());
if (parent != null) { if (parent != null) {
application.initializers(new ParentContextApplicationContextInitializer( application.initializers(new ParentContextApplicationContextInitializer(
parent)); parent));
@ -88,33 +86,20 @@ public abstract class SpringBootServletInitializer implements WebApplicationInit
application.initializers(new ServletContextApplicationContextInitializer( application.initializers(new ServletContextApplicationContextInitializer(
servletContext)); servletContext));
application.contextClass(AnnotationConfigEmbeddedWebApplicationContext.class); application.contextClass(AnnotationConfigEmbeddedWebApplicationContext.class);
configure(application);
return (WebApplicationContext) application.run(); return (WebApplicationContext) application.run();
} }
private Object[] getConfigClasses() {
Class<?>[] additionalConfigClasses = getAdditionalConfigClasses();
if (ObjectUtils.isEmpty(additionalConfigClasses)) {
return new Object[] { getConfigClass() };
}
Object[] configClasses = new Object[additionalConfigClasses.length + 1];
configClasses[0] = getConfigClass();
System.arraycopy(additionalConfigClasses, 0, configClasses, 1,
additionalConfigClasses.length);
return configClasses;
}
/**
* Returns the main configuration class to load. If you need additional configuration
* classes you can also override {@link #getAdditionalConfigClasses()}.
*/
protected abstract Class<?> getConfigClass();
/** /**
* Returns configuration classes that should be loaded in addition to the * Configure the application. Normally all you would need to do it add sources (e.g.
* {@link #getConfigClass() main configuration class}. * config classes) because other settings have sensible defaults. You might choose
* (for instance) to add default command line arguments, or set an active Spring
* profile.
*
* @param application a builder for the application context
* @see SpringApplicationBuilder
*/ */
protected Class<?>[] getAdditionalConfigClasses() { protected void configure(SpringApplicationBuilder application) {
return null;
} }
} }

Loading…
Cancel
Save