use of org.springframework.boot.builder.SpringApplicationBuilder in project spring-boot by spring-projects.
the class SpringBootServletInitializer method createRootApplicationContext.
protected WebApplicationContext createRootApplicationContext(ServletContext servletContext) {
SpringApplicationBuilder builder = createSpringApplicationBuilder();
builder.main(getClass());
ApplicationContext parent = getExistingRootWebApplicationContext(servletContext);
if (parent != null) {
this.logger.info("Root context already created (using as parent).");
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, null);
builder.initializers(new ParentContextApplicationContextInitializer(parent));
}
builder.initializers(new ServletContextApplicationContextInitializer(servletContext));
builder.contextFactory((webApplicationType) -> new AnnotationConfigServletWebServerApplicationContext());
builder = configure(builder);
builder.listeners(new WebEnvironmentPropertySourceInitializer(servletContext));
SpringApplication application = builder.build();
if (application.getAllSources().isEmpty() && MergedAnnotations.from(getClass(), SearchStrategy.TYPE_HIERARCHY).isPresent(Configuration.class)) {
application.addPrimarySources(Collections.singleton(getClass()));
}
Assert.state(!application.getAllSources().isEmpty(), "No SpringApplication sources have been defined. Either override the " + "configure method or add an @Configuration annotation");
// Ensure error pages are registered
if (this.registerErrorPageFilter) {
application.addPrimarySources(Collections.singleton(ErrorPageFilterConfiguration.class));
}
application.setRegisterShutdownHook(false);
return run(application);
}
use of org.springframework.boot.builder.SpringApplicationBuilder in project spring-boot by spring-projects.
the class SpringBootServletInitializerTests method whenServletContextIsDestroyedThenJdbcDriversAreDeregistered.
@Test
void whenServletContextIsDestroyedThenJdbcDriversAreDeregistered() throws ServletException {
ServletContext servletContext = mock(ServletContext.class);
given(servletContext.getInitParameterNames()).willReturn(new Vector<String>().elements());
given(servletContext.getAttributeNames()).willReturn(new Vector<String>().elements());
AtomicBoolean driversDeregistered = new AtomicBoolean();
new SpringBootServletInitializer() {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Config.class);
}
@Override
protected void deregisterJdbcDrivers(ServletContext servletContext) {
driversDeregistered.set(true);
}
}.onStartup(servletContext);
ArgumentCaptor<ServletContextListener> captor = ArgumentCaptor.forClass(ServletContextListener.class);
then(servletContext).should().addListener(captor.capture());
captor.getValue().contextDestroyed(new ServletContextEvent(servletContext));
assertThat(driversDeregistered).isTrue();
}
use of org.springframework.boot.builder.SpringApplicationBuilder in project spring-boot by spring-projects.
the class ResourceHandlingApplication method main.
public static void main(String[] args) {
try {
Class.forName("org.springframework.web.servlet.DispatcherServlet");
System.err.println("Spring MVC must not be present, otherwise its static resource handling " + "will be used rather than the embedded containers'");
System.exit(1);
} catch (Throwable ex) {
new SpringApplicationBuilder(ResourceHandlingApplication.class).properties("server.port:0").listeners(new WebServerPortFileWriter(args[0])).run(args);
}
}
use of org.springframework.boot.builder.SpringApplicationBuilder in project spring-boot by spring-projects.
the class FailureAnalyzersIntegrationTests method analysisIsPerformed.
@Test
void analysisIsPerformed(CapturedOutput output) {
assertThatExceptionOfType(Exception.class).isThrownBy(() -> new SpringApplicationBuilder(TestConfiguration.class).web(WebApplicationType.NONE).run());
assertThat(output).contains("APPLICATION FAILED TO START");
}
use of org.springframework.boot.builder.SpringApplicationBuilder in project spring-boot by spring-projects.
the class SampleWebSocketsApplicationTests method echoEndpoint.
@Test
void echoEndpoint() {
ConfigurableApplicationContext context = new SpringApplicationBuilder(ClientConfiguration.class, PropertyPlaceholderAutoConfiguration.class).properties("websocket.uri:ws://localhost:" + this.port + "/echo/websocket").run("--spring.main.web-application-type=none");
long count = context.getBean(ClientConfiguration.class).latch.getCount();
AtomicReference<String> messagePayloadReference = context.getBean(ClientConfiguration.class).messagePayload;
context.close();
assertThat(count).isEqualTo(0);
assertThat(messagePayloadReference.get()).isEqualTo("Did you say \"Hello world!\"?");
}
Aggregations