Search in sources :

Example 46 with ConfigurableEnvironment

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

the class OnEnabledResourceChainCondition method getMatchOutcome.

@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
    ConfigurableEnvironment environment = (ConfigurableEnvironment) context.getEnvironment();
    boolean fixed = getEnabledProperty(environment, "strategy.fixed.", false);
    boolean content = getEnabledProperty(environment, "strategy.content.", false);
    Boolean chain = getEnabledProperty(environment, "", null);
    Boolean match = ResourceProperties.Chain.getEnabled(fixed, content, chain);
    ConditionMessage.Builder message = ConditionMessage.forCondition(ConditionalOnEnabledResourceChain.class);
    if (match == null) {
        if (ClassUtils.isPresent(WEBJAR_ASSET_LOCATOR, getClass().getClassLoader())) {
            return ConditionOutcome.match(message.found("class").items(WEBJAR_ASSET_LOCATOR));
        }
        return ConditionOutcome.noMatch(message.didNotFind("class").items(WEBJAR_ASSET_LOCATOR));
    }
    if (match) {
        return ConditionOutcome.match(message.because("enabled"));
    }
    return ConditionOutcome.noMatch(message.because("disabled"));
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) ConditionMessage(org.springframework.boot.autoconfigure.condition.ConditionMessage)

Example 47 with ConfigurableEnvironment

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

the class NestedBeansElementTests method getBean_withActiveProfile.

@Test
public void getBean_withActiveProfile() {
    ConfigurableEnvironment env = new StandardEnvironment();
    env.setActiveProfiles("dev");
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(bf);
    reader.setEnvironment(env);
    reader.loadBeanDefinitions(XML);
    // should not throw NSBDE
    bf.getBean("devOnlyBean");
    Object foo = bf.getBean("foo");
    assertThat(foo, instanceOf(Integer.class));
    bf.getBean("devOnlyBean");
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Test(org.junit.Test)

Example 48 with ConfigurableEnvironment

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

the class ProfileXmlBeanDefinitionTests method testDefaultAndNonDefaultProfile.

@Test
public void testDefaultAndNonDefaultProfile() {
    assertThat(beanFactoryFor(DEFAULT_ELIGIBLE_XML, NONE_ACTIVE), containsTargetBean());
    assertThat(beanFactoryFor(DEFAULT_ELIGIBLE_XML, "other"), not(containsTargetBean()));
    {
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
        ConfigurableEnvironment env = new StandardEnvironment();
        env.setActiveProfiles(DEV_ACTIVE);
        env.setDefaultProfiles("default");
        reader.setEnvironment(env);
        reader.loadBeanDefinitions(new ClassPathResource(DEFAULT_AND_DEV_ELIGIBLE_XML, getClass()));
        assertThat(beanFactory, containsTargetBean());
    }
    {
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
        ConfigurableEnvironment env = new StandardEnvironment();
        // env.setActiveProfiles(DEV_ACTIVE);
        env.setDefaultProfiles("default");
        reader.setEnvironment(env);
        reader.loadBeanDefinitions(new ClassPathResource(DEFAULT_AND_DEV_ELIGIBLE_XML, getClass()));
        assertThat(beanFactory, containsTargetBean());
    }
    {
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);
        ConfigurableEnvironment env = new StandardEnvironment();
        // env.setActiveProfiles(DEV_ACTIVE);
        //env.setDefaultProfiles("default");
        reader.setEnvironment(env);
        reader.loadBeanDefinitions(new ClassPathResource(DEFAULT_AND_DEV_ELIGIBLE_XML, getClass()));
        assertThat(beanFactory, containsTargetBean());
    }
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Test(org.junit.Test)

Example 49 with ConfigurableEnvironment

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

the class DevToolsHomePropertiesPostProcessorTests method loadsHomeProperties.

@Test
public void loadsHomeProperties() throws Exception {
    Properties properties = new Properties();
    properties.put("abc", "def");
    OutputStream out = new FileOutputStream(new File(this.home, ".spring-boot-devtools.properties"));
    properties.store(out, null);
    out.close();
    ConfigurableEnvironment environment = new MockEnvironment();
    MockDevToolHomePropertiesPostProcessor postProcessor = new MockDevToolHomePropertiesPostProcessor();
    postProcessor.postProcessEnvironment(environment, null);
    assertThat(environment.getProperty("abc")).isEqualTo("def");
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) MockEnvironment(org.springframework.mock.env.MockEnvironment) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) Properties(java.util.Properties) File(java.io.File) Test(org.junit.Test)

Example 50 with ConfigurableEnvironment

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

the class ConfigFileApplicationListenerTests method customDefaultProfileAndActiveFromFile.

@Test
public void customDefaultProfileAndActiveFromFile() throws Exception {
    // gh-5998
    SpringApplication application = new SpringApplication(Config.class);
    application.setWebApplicationType(WebApplicationType.NONE);
    this.context = application.run("--spring.config.name=customprofile", "--spring.profiles.default=customdefault");
    ConfigurableEnvironment environment = this.context.getEnvironment();
    assertThat(environment.containsProperty("customprofile")).isTrue();
    assertThat(environment.containsProperty("customprofile-specific")).isTrue();
    assertThat(environment.containsProperty("customprofile-customdefault")).isTrue();
    assertThat(environment.acceptsProfiles("customdefault")).isTrue();
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) SpringApplication(org.springframework.boot.SpringApplication) 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