Search in sources :

Example 61 with SpringApplicationBuilder

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);
}
Also used : AnnotationConfigServletWebServerApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) Configuration(org.springframework.context.annotation.Configuration) SpringApplication(org.springframework.boot.SpringApplication) AnnotationConfigServletWebServerApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) ParentContextApplicationContextInitializer(org.springframework.boot.builder.ParentContextApplicationContextInitializer)

Example 62 with SpringApplicationBuilder

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();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServletContextListener(jakarta.servlet.ServletContextListener) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) MockServletContext(org.springframework.mock.web.MockServletContext) ServletContext(jakarta.servlet.ServletContext) Vector(java.util.Vector) ServletContextEvent(jakarta.servlet.ServletContextEvent) Test(org.junit.jupiter.api.Test)

Example 63 with SpringApplicationBuilder

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);
    }
}
Also used : WebServerPortFileWriter(org.springframework.boot.web.context.WebServerPortFileWriter) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder)

Example 64 with SpringApplicationBuilder

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");
}
Also used : SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) PortInUseException(org.springframework.boot.web.server.PortInUseException) Test(org.junit.jupiter.api.Test)

Example 65 with SpringApplicationBuilder

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!\"?");
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

SpringApplicationBuilder (org.springframework.boot.builder.SpringApplicationBuilder)319 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)170 Test (org.junit.Test)156 OutputDestination (org.springframework.cloud.stream.binder.test.OutputDestination)105 InputDestination (org.springframework.cloud.stream.binder.test.InputDestination)100 Test (org.junit.jupiter.api.Test)82 ApplicationContext (org.springframework.context.ApplicationContext)55 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)26 Environment (org.springframework.cloud.config.environment.Environment)19 Before (org.junit.Before)8 SpringApplication (org.springframework.boot.SpringApplication)8 Map (java.util.Map)7 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)7 ConsulClient (com.ecwid.consul.v1.ConsulClient)6 Message (org.springframework.messaging.Message)6 MessageHeaders (org.springframework.messaging.MessageHeaders)6 GenericMessage (org.springframework.messaging.support.GenericMessage)6 ArrayList (java.util.ArrayList)5 LinkedHashMap (java.util.LinkedHashMap)5 CountDownLatch (java.util.concurrent.CountDownLatch)5