Search in sources :

Example 1 with ParentContextApplicationContextInitializer

use of org.springframework.boot.builder.ParentContextApplicationContextInitializer 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)

Aggregations

SpringApplication (org.springframework.boot.SpringApplication)1 ParentContextApplicationContextInitializer (org.springframework.boot.builder.ParentContextApplicationContextInitializer)1 SpringApplicationBuilder (org.springframework.boot.builder.SpringApplicationBuilder)1 AnnotationConfigServletWebServerApplicationContext (org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext)1 ApplicationContext (org.springframework.context.ApplicationContext)1 Configuration (org.springframework.context.annotation.Configuration)1 WebApplicationContext (org.springframework.web.context.WebApplicationContext)1