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.contextClass(AnnotationConfigEmbeddedWebApplicationContext.class); builder = configure(builder); SpringApplication application = builder.build(); if (application.getSources().isEmpty() && AnnotationUtils .findAnnotation(getClass(), Configuration.class) != null) { application.getSources().add(getClass()); } Assert.state(!application.getSources().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.getSources().add(ErrorPageFilter.class); } return run(application); }
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.contextClass(AnnotationConfigEmbeddedWebApplicationContext.class); builder = configure(builder); SpringApplication application = builder.build(); if (application.getSources().isEmpty() && AnnotationUtils .findAnnotation(getClass(), Configuration.class) != null) { application.getSources().add(getClass()); } Assert.state(application.getSources().size() > 0, "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.getSources().add(ErrorPageFilter.class); } return run(application); }
@Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { ConfigurableEnvironment environment = event.getEnvironment(); if (!environment.getProperty("spring.cloud.bootstrap.enabled", Boolean.class, true)) { return; } // don't listen to events in a bootstrap context if (environment.getPropertySources().contains(BOOTSTRAP_PROPERTY_SOURCE_NAME)) { return; } ConfigurableApplicationContext context = null; String configName = environment .resolvePlaceholders("${spring.cloud.bootstrap.name:bootstrap}"); for (ApplicationContextInitializer<?> initializer : event.getSpringApplication() .getInitializers()) { if (initializer instanceof ParentContextApplicationContextInitializer) { context = findBootstrapContext( (ParentContextApplicationContextInitializer) initializer, configName); } } if (context == null) { context = bootstrapServiceContext(environment, event.getSpringApplication(), configName); } apply(context, event.getSpringApplication(), environment); }
private ConfigurableApplicationContext findBootstrapContext( ParentContextApplicationContextInitializer initializer, String configName) { Field field = ReflectionUtils .findField(ParentContextApplicationContextInitializer.class, "parent"); ReflectionUtils.makeAccessible(field); ConfigurableApplicationContext parent = safeCast( ConfigurableApplicationContext.class, ReflectionUtils.getField(field, initializer)); if (parent != null && !configName.equals(parent.getId())) { parent = safeCast(ConfigurableApplicationContext.class, parent.getParent()); } return parent; }
@Override public void initialize(ConfigurableApplicationContext context) { while (context.getParent() != null && context.getParent() != context) { context = (ConfigurableApplicationContext) context.getParent(); } reorderSources(context.getEnvironment()); new ParentContextApplicationContextInitializer(this.parent) .initialize(context); }
/** * This is the initializing method when running ParaServer as WAR, * deployed to a servlet container. * @param sc the ServletContext instance * @param sources the application classes that will be scanned * @return the application context */ protected static WebApplicationContext runAsWAR(ServletContext sc, Object... sources) { ApplicationContext parent = null; Object object = sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); if (object instanceof ApplicationContext) { logger.info("Root context already created (using as parent)."); parent = (ApplicationContext) object; sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, null); } SpringApplicationBuilder application = new SpringApplicationBuilder(sources); if (parent != null) { application.initializers(new ParentContextApplicationContextInitializer(parent)); } application.initializers(new ServletContextApplicationContextInitializer(sc)); application.contextClass(AnnotationConfigEmbeddedWebApplicationContext.class); // entry point (WAR) application.profiles(Config.ENVIRONMENT); application.web(true); application.bannerMode(Banner.Mode.OFF); Para.initialize(getCoreModules()); // Ensure error pages are registered application.sources(ErrorFilter.class); WebApplicationContext rootAppContext = (WebApplicationContext) application.run(); if (rootAppContext != null) { sc.addListener(new ContextLoaderListener(rootAppContext) { @Override public void contextInitialized(ServletContextEvent event) { // no-op because the application context is already initialized } }); sc.getSessionCookieConfig().setName("sess"); sc.getSessionCookieConfig().setMaxAge(1); sc.getSessionCookieConfig().setHttpOnly(true); sc.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed", "false"); } return rootAppContext; }