Search in sources :

Example 76 with SpringApplication

use of org.springframework.boot.SpringApplication in project spring-boot by spring-projects.

the class EnvironmentPostProcessorApplicationListener method onApplicationEnvironmentPreparedEvent.

private void onApplicationEnvironmentPreparedEvent(ApplicationEnvironmentPreparedEvent event) {
    ConfigurableEnvironment environment = event.getEnvironment();
    SpringApplication application = event.getSpringApplication();
    for (EnvironmentPostProcessor postProcessor : getEnvironmentPostProcessors(application.getResourceLoader(), event.getBootstrapContext())) {
        postProcessor.postProcessEnvironment(environment, application);
    }
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) SpringApplication(org.springframework.boot.SpringApplication)

Example 77 with SpringApplication

use of org.springframework.boot.SpringApplication 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 78 with SpringApplication

use of org.springframework.boot.SpringApplication in project spring-boot by spring-projects.

the class BasicErrorControllerDirectMockMvcTests method errorControllerWithAop.

@Test
void errorControllerWithAop() throws Exception {
    setup((ConfigurableWebApplicationContext) new SpringApplication(WithAopConfiguration.class).run("--server.port=0"));
    MvcResult response = this.mockMvc.perform(get("/error").accept(MediaType.TEXT_HTML)).andExpect(status().is5xxServerError()).andReturn();
    String content = response.getResponse().getContentAsString();
    assertThat(content).contains("status=999");
}
Also used : SpringApplication(org.springframework.boot.SpringApplication) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.jupiter.api.Test)

Example 79 with SpringApplication

use of org.springframework.boot.SpringApplication in project spring-boot by spring-projects.

the class EnvironmentPostProcessorApplicationListenerTests method onApplicationEventWhenApplicationFailedEventSwitchesLogs.

@Test
void onApplicationEventWhenApplicationFailedEventSwitchesLogs() {
    SpringApplication application = mock(SpringApplication.class);
    ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class);
    ApplicationFailedEvent event = new ApplicationFailedEvent(application, new String[0], context, new RuntimeException());
    this.listener.onApplicationEvent(event);
    then(this.deferredLogs).should().switchOverAll();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ApplicationFailedEvent(org.springframework.boot.context.event.ApplicationFailedEvent) SpringApplication(org.springframework.boot.SpringApplication) Test(org.junit.jupiter.api.Test)

Example 80 with SpringApplication

use of org.springframework.boot.SpringApplication in project xm-ms-entity by xm-online.

the class EntityApp method main.

/**
 * Main method, used to run the application.
 *
 * @param args the command line arguments
 * @throws UnknownHostException if the local host name could not be resolved into an address
 */
public static void main(String[] args) throws UnknownHostException {
    MdcUtils.putRid();
    SpringApplication app = new SpringApplication(EntityApp.class);
    DefaultProfileUtil.addDefaultProfile(app);
    Environment env = app.run(args).getEnvironment();
    String protocol = "http";
    if (env.getProperty("server.ssl.key-store") != null) {
        protocol = "https";
    }
    log.info("\n----------------------------------------------------------\n\t" + "Application '{}' is running! Access URLs:\n\t" + "Local: \t\t{}://localhost:{}\n\t" + "External: \t{}://{}:{}\n\t" + "Profile(s): \t{}\n----------------------------------------------------------", env.getProperty("spring.application.name"), protocol, env.getProperty("server.port"), protocol, InetAddress.getLocalHost().getHostAddress(), env.getProperty("server.port"), env.getActiveProfiles());
    String configServerStatus = env.getProperty("configserver.status");
    log.info("\n----------------------------------------------------------\n\t" + "Config Server: \t{}\n----------------------------------------------------------", configServerStatus == null ? "Not found or not setup for this application" : configServerStatus);
}
Also used : SpringApplication(org.springframework.boot.SpringApplication) Environment(org.springframework.core.env.Environment)

Aggregations

SpringApplication (org.springframework.boot.SpringApplication)205 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)56 Test (org.junit.Test)43 Test (org.junit.jupiter.api.Test)39 Environment (org.springframework.core.env.Environment)21 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)15 HashMap (java.util.HashMap)9 ApplicationFailedEvent (org.springframework.boot.context.event.ApplicationFailedEvent)9 ApplicationReadyEvent (org.springframework.boot.context.event.ApplicationReadyEvent)9 ApplicationStartingEvent (org.springframework.boot.context.event.ApplicationStartingEvent)9 File (java.io.File)7 SpringApplicationBuilder (org.springframework.boot.builder.SpringApplicationBuilder)7 BeforeEach (org.junit.jupiter.api.BeforeEach)6 JavaLoggingSystem (org.springframework.boot.logging.java.JavaLoggingSystem)6 ApplicationContext (org.springframework.context.ApplicationContext)6 ApplicationPreparedEvent (org.springframework.boot.context.event.ApplicationPreparedEvent)5 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)5 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)4 JoranException (ch.qos.logback.core.joran.spi.JoranException)3 IOException (java.io.IOException)3