Search in sources :

Example 31 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 32 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 33 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)

Example 34 with ConfigurableEnvironment

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

the class RelaxedPropertyResolver method ignoringUnresolvableNestedPlaceholders.

/**
	 * Return a property resolver for the environment, preferring one that ignores
	 * unresolvable nested placeholders.
	 * @param environment the source environment
	 * @param prefix the prefix
	 * @return a property resolver for the environment
	 * @since 1.4.3
	 */
public static RelaxedPropertyResolver ignoringUnresolvableNestedPlaceholders(Environment environment, String prefix) {
    Assert.notNull(environment, "Environment must not be null");
    PropertyResolver resolver = environment;
    if (environment instanceof ConfigurableEnvironment) {
        resolver = new PropertySourcesPropertyResolver(((ConfigurableEnvironment) environment).getPropertySources());
        ((PropertySourcesPropertyResolver) resolver).setIgnoreUnresolvableNestedPlaceholders(true);
    }
    return new RelaxedPropertyResolver(resolver, prefix);
}
Also used : PropertySourcesPropertyResolver(org.springframework.core.env.PropertySourcesPropertyResolver) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) PropertyResolver(org.springframework.core.env.PropertyResolver) PropertySourcesPropertyResolver(org.springframework.core.env.PropertySourcesPropertyResolver)

Example 35 with ConfigurableEnvironment

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

the class DelegatingApplicationContextInitializer method initialize.

@Override
public void initialize(ConfigurableApplicationContext context) {
    ConfigurableEnvironment environment = context.getEnvironment();
    List<Class<?>> initializerClasses = getInitializerClasses(environment);
    if (!initializerClasses.isEmpty()) {
        applyInitializerClasses(context, initializerClasses);
    }
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment)

Aggregations

ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)55 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)7 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