Search in sources :

Example 26 with ConfigurableEnvironment

use of org.springframework.core.env.ConfigurableEnvironment in project spring-framework by spring-projects.

the class FrameworkServlet method configureAndRefreshWebApplicationContext.

protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac) {
    if (ObjectUtils.identityToString(wac).equals(wac.getId())) {
        // -> assign a more useful id based on available information
        if (this.contextId != null) {
            wac.setId(this.contextId);
        } else {
            // Generate default id...
            wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(getServletContext().getContextPath()) + '/' + getServletName());
        }
    }
    wac.setServletContext(getServletContext());
    wac.setServletConfig(getServletConfig());
    wac.setNamespace(getNamespace());
    wac.addApplicationListener(new SourceFilteringListener(wac, new ContextRefreshListener()));
    // 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(getServletContext(), getServletConfig());
    }
    postProcessWebApplicationContext(wac);
    applyInitializers(wac);
    wac.refresh();
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) ConfigurableWebEnvironment(org.springframework.web.context.ConfigurableWebEnvironment) SourceFilteringListener(org.springframework.context.event.SourceFilteringListener)

Example 27 with ConfigurableEnvironment

use of org.springframework.core.env.ConfigurableEnvironment in project spring-boot by spring-projects.

the class RemoteUrlPropertyExtractor method onApplicationEvent.

@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
    ConfigurableEnvironment environment = event.getEnvironment();
    String url = cleanRemoteUrl(environment.getProperty(NON_OPTION_ARGS));
    Assert.state(StringUtils.hasLength(url), "No remote URL specified");
    Assert.state(url.indexOf(",") == -1, "Multiple URLs specified");
    try {
        new URI(url);
    } catch (URISyntaxException ex) {
        throw new IllegalStateException("Malformed URL '" + url + "'");
    }
    Map<String, Object> source = Collections.singletonMap("remoteUrl", (Object) url);
    PropertySource<?> propertySource = new MapPropertySource("remoteUrl", source);
    environment.getPropertySources().addLast(propertySource);
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) MapPropertySource(org.springframework.core.env.MapPropertySource) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 28 with ConfigurableEnvironment

use of org.springframework.core.env.ConfigurableEnvironment in project spring-boot by spring-projects.

the class SpringApplication method run.

/**
	 * Run the Spring application, creating and refreshing a new
	 * {@link ApplicationContext}.
	 * @param args the application arguments (usually passed from a Java main method)
	 * @return a running {@link ApplicationContext}
	 */
public ConfigurableApplicationContext run(String... args) {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    ConfigurableApplicationContext context = null;
    Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
    configureHeadlessProperty();
    SpringApplicationRunListeners listeners = getRunListeners(args);
    listeners.starting();
    try {
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
        ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
        configureIgnoreBeanInfo(environment);
        bindToSpringApplication(environment);
        Banner printedBanner = printBanner(environment);
        context = createApplicationContext();
        exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[] { ConfigurableApplicationContext.class }, context);
        prepareContext(context, environment, listeners, applicationArguments, printedBanner);
        refreshContext(context);
        afterRefresh(context, applicationArguments);
        listeners.finished(context, null);
        stopWatch.stop();
        if (this.logStartupInfo) {
            new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
        }
        return context;
    } catch (Throwable ex) {
        handleRunFailure(context, listeners, exceptionReporters, ex);
        throw new IllegalStateException(ex);
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ArrayList(java.util.ArrayList) StopWatch(org.springframework.util.StopWatch) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment)

Example 29 with ConfigurableEnvironment

use of org.springframework.core.env.ConfigurableEnvironment in project spring-boot by spring-projects.

the class SpringApplication method prepareEnvironment.

private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments) {
    // Create and configure the environment
    ConfigurableEnvironment environment = getOrCreateEnvironment();
    configureEnvironment(environment, applicationArguments.getSourceArgs());
    listeners.environmentPrepared(environment);
    if (isWebEnvironment(environment) && this.webApplicationType == WebApplicationType.NONE) {
        environment = convertToStandardEnvironment(environment);
    }
    return environment;
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment)

Example 30 with ConfigurableEnvironment

use of org.springframework.core.env.ConfigurableEnvironment in project spring-boot by spring-projects.

the class RelaxedPropertyResolver method getSubProperties.

/**
	 * Return a Map of all values from all underlying properties that start with the
	 * specified key. NOTE: this method can only be used if the underlying resolver is a
	 * {@link ConfigurableEnvironment}.
	 * @param keyPrefix the key prefix used to filter results
	 * @return a map of all sub properties starting with the specified key prefix.
	 * @see PropertySourceUtils#getSubProperties
	 */
public Map<String, Object> getSubProperties(String keyPrefix) {
    Assert.isInstanceOf(ConfigurableEnvironment.class, this.resolver, "SubProperties not available.");
    ConfigurableEnvironment env = (ConfigurableEnvironment) this.resolver;
    return PropertySourceUtils.getSubProperties(env.getPropertySources(), this.prefix, keyPrefix);
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment)

Aggregations

ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)54 Test (org.junit.Test)27 StandardEnvironment (org.springframework.core.env.StandardEnvironment)18 MutablePropertySources (org.springframework.core.env.MutablePropertySources)10 SpringApplication (org.springframework.boot.SpringApplication)7 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)6 MockEnvironment (org.springframework.mock.env.MockEnvironment)6 CompositePropertySource (org.springframework.core.env.CompositePropertySource)4 MapPropertySource (org.springframework.core.env.MapPropertySource)4 Environment (org.springframework.core.env.Environment)3 ProfileAnnotatedComponent (example.profilescan.ProfileAnnotatedComponent)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PrintStream (java.io.PrintStream)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 AnnotatedGenericBeanDefinition (org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition)2 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)2 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 CommandLinePropertySource (org.springframework.core.env.CommandLinePropertySource)2 PropertySource (org.springframework.core.env.PropertySource)2