List of usage examples for org.springframework.web.context ConfigurableWebApplicationContext getEnvironment
Environment getEnvironment();
From source file:de.dennishoersch.web.springframework.profile.SetProfileContextListener.java
private void addProfiles(ConfigurableWebApplicationContext applicationContext, Iterable<Profile> profiles) { for (Profile profile : profiles) { if (profile.isActive(applicationContext)) { logger.debug(String.format("Profile '%s' is active.", profile.name())); applicationContext.getEnvironment().addActiveProfile(profile.name()); profile.init();/*from w w w . j a va 2 s. co m*/ } } }
From source file:com.wisemapping.webmvc.ApplicationContextInitializer.java
public void initialize(@NotNull ConfigurableWebApplicationContext ctx) { try {/* w w w. j a v a 2s. c o m*/ final Resource resource = new ServletContextResource(ctx.getServletContext(), "/WEB-INF/app.properties"); final ResourcePropertySource resourcePropertySource = new ResourcePropertySource(resource); ctx.getEnvironment().getPropertySources().addFirst(resourcePropertySource); } catch (IOException e) { throw new IllegalStateException(e); } }
From source file:org.cloudfoundry.identity.uaa.config.YamlServletProfileInitializer.java
@Override public void initialize(ConfigurableWebApplicationContext applicationContext) { Resource resource = null;//from www. j a v a2 s .c o m ServletContext servletContext = applicationContext.getServletContext(); WebApplicationContextUtils.initServletPropertySources( applicationContext.getEnvironment().getPropertySources(), servletContext, applicationContext.getServletConfig()); ServletConfig servletConfig = applicationContext.getServletConfig(); String locations = servletConfig == null ? null : servletConfig.getInitParameter(PROFILE_CONFIG_FILE_LOCATIONS); resource = getResource(servletContext, applicationContext, locations); if (resource == null) { servletContext.log("No YAML environment properties from servlet. Defaulting to servlet context."); locations = servletContext.getInitParameter(PROFILE_CONFIG_FILE_LOCATIONS); resource = getResource(servletContext, applicationContext, locations); } try { servletContext.log("Loading YAML environment properties from location: " + resource); YamlMapFactoryBean factory = new YamlMapFactoryBean(); factory.setResolutionMethod(ResolutionMethod.OVERRIDE_AND_IGNORE); List<Resource> resources = new ArrayList<Resource>(); String defaultLocation = servletConfig == null ? null : servletConfig.getInitParameter(PROFILE_CONFIG_FILE_DEFAULT); if (defaultLocation != null) { Resource defaultResource = new ClassPathResource(defaultLocation); if (defaultResource.exists()) { resources.add(defaultResource); } } resources.add(resource); factory.setResources(resources.toArray(new Resource[resources.size()])); Map<String, Object> map = factory.getObject(); String yamlStr = (new Yaml()).dump(map); map.put(rawYamlKey, yamlStr); NestedMapPropertySource properties = new NestedMapPropertySource("servletConfigYaml", map); applicationContext.getEnvironment().getPropertySources().addLast(properties); applySpringProfiles(applicationContext.getEnvironment(), servletContext); applyLog4jConfiguration(applicationContext.getEnvironment(), servletContext); } catch (Exception e) { servletContext.log("Error loading YAML environment properties from location: " + resource, e); } }
From source file:org.cloudfoundry.identity.uaa.config.YamlServletProfileInitializer.java
private Resource getResource(ServletContext servletContext, ConfigurableWebApplicationContext applicationContext, String locations) { Resource resource = null;/*from w w w .j av a 2 s . c o m*/ String[] configFileLocations = locations == null ? DEFAULT_PROFILE_CONFIG_FILE_LOCATIONS : StringUtils.commaDelimitedListToStringArray(locations); for (String location : configFileLocations) { location = applicationContext.getEnvironment().resolvePlaceholders(location); servletContext.log("Testing for YAML resources at: " + location); resource = applicationContext.getResource(location); if (resource != null && resource.exists()) { break; } } return resource; }
From source file:com.kixeye.chassis.transport.http.HttpTransportConfiguration.java
@Bean(name = HTTP_TRANSPORT_CHILD_CONTEXT_BEAN_NAME) public SpringContextWrapper transportWebMvcContext(ConfigurableWebApplicationContext parentContext, ServletContextHandler servletContextHandler) { AnnotationConfigWebApplicationContext transportWebMvcContext = new AnnotationConfigWebApplicationContext(); transportWebMvcContext.setDisplayName("httpTransport-webMvcContext"); transportWebMvcContext.setServletContext(servletContextHandler.getServletContext()); transportWebMvcContext.setParent(parentContext); transportWebMvcContext.setEnvironment(parentContext.getEnvironment()); transportWebMvcContext.register(SpringMvcConfiguration.class); transportWebMvcContext.register(PropertySourcesPlaceholderConfigurer.class); transportWebMvcContext.refresh();//from w w w . j a v a2 s . c o m return new SpringContextWrapper(transportWebMvcContext); }
From source file:com.dhcc.framework.web.context.DhccContextLoader.java
/** * Customize the {@link ConfigurableWebApplicationContext} created by this * ContextLoader after config locations have been supplied to the context * but before the context is <em>refreshed</em>. * <p>The default implementation {@linkplain #determineContextInitializerClasses(ServletContext) * determines} what (if any) context initializer classes have been specified through * {@linkplain #CONTEXT_INITIALIZER_CLASSES_PARAM context init parameters} and * {@linkplain ApplicationContextInitializer#initialize invokes each} with the * given web application context.//w ww . java2s .c o m * <p>Any {@code ApplicationContextInitializers} implementing * {@link org.springframework.core.Ordered Ordered} or marked with @{@link * org.springframework.core.annotation.Order Order} will be sorted appropriately. * @param servletContext the current servlet context * @param applicationContext the newly created application context * @see #createWebApplicationContext(ServletContext, ApplicationContext) * @see #CONTEXT_INITIALIZER_CLASSES_PARAM * @see ApplicationContextInitializer#initialize(ConfigurableApplicationContext) */ protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext applicationContext) { List<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>> initializerClasses = determineContextInitializerClasses( servletContext); if (initializerClasses.size() == 0) { // no ApplicationContextInitializers have been declared -> nothing to do return; } Class<?> contextClass = applicationContext.getClass(); ArrayList<ApplicationContextInitializer<ConfigurableApplicationContext>> initializerInstances = new ArrayList<ApplicationContextInitializer<ConfigurableApplicationContext>>(); for (Class<ApplicationContextInitializer<ConfigurableApplicationContext>> initializerClass : initializerClasses) { Class<?> initializerContextClass = GenericTypeResolver.resolveTypeArgument(initializerClass, ApplicationContextInitializer.class); if (initializerContextClass != null) { Assert.isAssignable(initializerContextClass, contextClass, String.format( "Could not add context initializer [%s] as its generic parameter [%s] " + "is not assignable from the type of application context used by this " + "context loader [%s]: ", initializerClass.getName(), initializerContextClass.getName(), contextClass.getName())); } initializerInstances.add(BeanUtils.instantiateClass(initializerClass)); } ConfigurableEnvironment env = applicationContext.getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(servletContext, null); } Collections.sort(initializerInstances, new AnnotationAwareOrderComparator()); for (ApplicationContextInitializer<ConfigurableApplicationContext> initializer : initializerInstances) { initializer.initialize(applicationContext); } }
From source file:no.kantega.publishing.spring.OpenAksessContextLoaderListener.java
@Override protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext wac) { Configuration.setApplicationDirectory(dataDirectory); // Make dataDir available as Servlet Context attribute servletContext.setAttribute(APPLICATION_DIRECTORY, dataDirectory); // Set up @Autowired support ApplicationContextUtils.addAutowiredSupport(wac); final Configuration configuration = new Configuration(properties); if (configuration.getBoolean("caching.enabled", true)) { wac.getEnvironment().setActiveProfiles("useCaching"); }/* w w w. java2 s . com*/ Aksess.setContextPath(servletContext.getContextPath()); // Set and load configuration on these classes since they are not DI-based (hackish..) Aksess.setConfiguration(configuration); Aksess.loadConfiguration(); // Add ${appDir} property for the Spring Context ApplicationContextUtils.addAppDirPropertySupport(wac); dbConnectionFactory.setServletContext(servletContext); dbConnectionFactory.setConfiguration(configuration); dbConnectionFactory.loadConfiguration(); // Add the Configuration and the ConfigurationLoader as Spring beans addConfigurationAndLoaderAsSingletonsInContext(wac, configuration, configurationLoader); // Replace ${} properties in Spring with config properties addConfigurationPropertyReplacer(wac, properties); RootContext.setInstance(wac); }
From source file:org.springframework.web.context.ContextLoader.java
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) {//from w w w . j av a2 s. c o m if (ObjectUtils.identityToString(wac).equals(wac.getId())) { // The application context id is still set to its original default value // -> assign a more useful id based on available information String idParam = sc.getInitParameter(CONTEXT_ID_PARAM); if (idParam != null) { wac.setId(idParam); } else { // Generate default id... wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(sc.getContextPath())); } } wac.setServletContext(sc); String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM); if (configLocationParam != null) { wac.setConfigLocation(configLocationParam); } // The wac environment's #initPropertySources will be called in any case when the context // is refreshed; do it eagerly here to ensure servlet property sources are in place for // use in any post-processing or initialization that occurs below prior to #refresh ConfigurableEnvironment env = wac.getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(sc, null); } customizeContext(sc, wac); wac.refresh(); }