Search in sources :

Example 41 with ConfigurableEnvironment

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

the class SpringApplicationTests method commandLinePropertySource.

@Test
public void commandLinePropertySource() throws Exception {
    SpringApplication application = new SpringApplication(ExampleConfig.class);
    application.setWebApplicationType(WebApplicationType.NONE);
    ConfigurableEnvironment environment = new StandardEnvironment();
    application.setEnvironment(environment);
    this.context = application.run("--foo=bar");
    assertThat(environment).has(matchingPropertySource(CommandLinePropertySource.class, "commandLineArgs"));
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) CommandLinePropertySource(org.springframework.core.env.CommandLinePropertySource) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Test(org.junit.Test)

Example 42 with ConfigurableEnvironment

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

the class SpringApplicationTests method propertiesFileEnhancesEnvironment.

@Test
public void propertiesFileEnhancesEnvironment() throws Exception {
    SpringApplication application = new SpringApplication(ExampleConfig.class);
    application.setWebApplicationType(WebApplicationType.NONE);
    ConfigurableEnvironment environment = new StandardEnvironment();
    application.setEnvironment(environment);
    this.context = application.run();
    assertThat(environment.getProperty("foo")).isEqualTo("bucket");
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Test(org.junit.Test)

Example 43 with ConfigurableEnvironment

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

the class ImageBannerTests method printBanner.

private String printBanner(String path, String... properties) {
    ImageBanner banner = new ImageBanner(new ClassPathResource(path, getClass()));
    ConfigurableEnvironment environment = new MockEnvironment();
    TestPropertySourceUtils.addInlinedPropertiesToEnvironment(environment, properties);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    banner.printBanner(environment, getClass(), new PrintStream(out));
    return out.toString();
}
Also used : PrintStream(java.io.PrintStream) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) MockEnvironment(org.springframework.mock.env.MockEnvironment) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ClassPathResource(org.springframework.core.io.ClassPathResource)

Example 44 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 45 with ConfigurableEnvironment

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

the class StandardServletEnvironmentTests method propertySourceOrder.

@Test
public void propertySourceOrder() throws Exception {
    SimpleNamingContextBuilder.emptyActivatedContextBuilder();
    ConfigurableEnvironment env = new StandardServletEnvironment();
    MutablePropertySources sources = env.getPropertySources();
    assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), equalTo(0));
    assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), equalTo(1));
    assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME)), equalTo(2));
    assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(3));
    assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(4));
    assertThat(sources.size(), is(5));
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) MutablePropertySources(org.springframework.core.env.MutablePropertySources) Test(org.junit.Test)

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