Search in sources :

Example 1 with PropertySource

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

the class PropertySourcesPlaceholderConfigurerTests method withNonEnumerablePropertySource.

@Test
public void withNonEnumerablePropertySource() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class).addPropertyValue("name", "${foo}").getBeanDefinition());
    PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
    PropertySource<?> ps = new PropertySource<Object>("simplePropertySource", new Object()) {

        @Override
        public Object getProperty(String key) {
            return "bar";
        }
    };
    MockEnvironment env = new MockEnvironment();
    env.getPropertySources().addFirst(ps);
    ppc.setEnvironment(env);
    ppc.postProcessBeanFactory(bf);
    assertThat(bf.getBean(TestBean.class).getName(), equalTo("bar"));
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) MockEnvironment(org.springframework.mock.env.MockEnvironment) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) PropertySource(org.springframework.core.env.PropertySource) MockPropertySource(org.springframework.mock.env.MockPropertySource) Test(org.junit.Test)

Example 2 with PropertySource

use of org.springframework.core.env.PropertySource in project libresonic by Libresonic.

the class LoggingFileOverrideListener method onApplicationEvent.

@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
    PropertySource ps = new MapPropertySource("LogFileLocationPS", Collections.singletonMap(LogFile.FILE_PROPERTY, getLogFile().getAbsolutePath()));
    event.getEnvironment().getPropertySources().addLast(ps);
}
Also used : MapPropertySource(org.springframework.core.env.MapPropertySource) PropertySource(org.springframework.core.env.PropertySource) MapPropertySource(org.springframework.core.env.MapPropertySource)

Example 3 with PropertySource

use of org.springframework.core.env.PropertySource in project java-chassis by ServiceComb.

the class ConfigurableEnvironmentConfiguration method getPropertySources.

private Map<String, PropertySource<?>> getPropertySources() {
    Map<String, PropertySource<?>> map = new LinkedHashMap<>();
    MutablePropertySources sources = (this.environment != null ? this.environment.getPropertySources() : new StandardEnvironment().getPropertySources());
    for (PropertySource<?> source : sources) {
        extract("", map, source);
    }
    return map;
}
Also used : MutablePropertySources(org.springframework.core.env.MutablePropertySources) LinkedHashMap(java.util.LinkedHashMap) CompositePropertySource(org.springframework.core.env.CompositePropertySource) PropertySource(org.springframework.core.env.PropertySource) EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) StandardEnvironment(org.springframework.core.env.StandardEnvironment)

Example 4 with PropertySource

use of org.springframework.core.env.PropertySource in project bitsquare by bitsquare.

the class BitsquareEnvironmentTests method testPropertySourcePrecedence.

@Test
public void testPropertySourcePrecedence() {
    PropertySource commandlineProps = new MockPropertySource(BITSQUARE_COMMANDLINE_PROPERTY_SOURCE_NAME).withProperty("key.x", "x.commandline");
    PropertySource filesystemProps = new MockPropertySource(BITSQUARE_APP_DIR_PROPERTY_SOURCE_NAME).withProperty("key.x", "x.env").withProperty("key.y", "y.env");
    ConfigurableEnvironment env = new BitsquareEnvironment(commandlineProps) {

        @Override
        PropertySource<?> appDirProperties() {
            return filesystemProps;
        }
    };
    MutablePropertySources propertySources = env.getPropertySources();
    assertThat(propertySources.precedenceOf(named(BITSQUARE_COMMANDLINE_PROPERTY_SOURCE_NAME)), equalTo(0));
    assertThat(propertySources.precedenceOf(named(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(1));
    assertThat(propertySources.precedenceOf(named(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(2));
    assertThat(propertySources.precedenceOf(named(BITSQUARE_APP_DIR_PROPERTY_SOURCE_NAME)), equalTo(3));
    assertThat(propertySources.precedenceOf(named(BITSQUARE_HOME_DIR_PROPERTY_SOURCE_NAME)), equalTo(4));
    assertThat(propertySources.precedenceOf(named(BITSQUARE_CLASSPATH_PROPERTY_SOURCE_NAME)), equalTo(5));
    assertThat(propertySources.precedenceOf(named(BITSQUARE_DEFAULT_PROPERTY_SOURCE_NAME)), equalTo(6));
    assertThat(propertySources.size(), equalTo(7));
    // commandline value wins due to precedence
    assertThat(env.getProperty("key.x"), equalTo("x.commandline"));
    // env value wins because it's the only one available
    assertThat(env.getProperty("key.y"), equalTo("y.env"));
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) BitsquareEnvironment(io.bitsquare.app.BitsquareEnvironment) MutablePropertySources(org.springframework.core.env.MutablePropertySources) MockPropertySource(org.springframework.mock.env.MockPropertySource) PropertySource(org.springframework.core.env.PropertySource) MockPropertySource(org.springframework.mock.env.MockPropertySource) Test(org.junit.Test)

Example 5 with PropertySource

use of org.springframework.core.env.PropertySource in project grails-core by grails.

the class EnvironmentAwarePropertySource method initialize.

private void initialize() {
    if (propertyNames == null) {
        propertyNames = new ArrayList<>();
        Environment env = Environment.getCurrent();
        String key = "environments." + env.getName();
        for (PropertySource propertySource : source) {
            if ((propertySource != this) && // plugin default configuration is not allowed to be environment aware (GRAILS-12123)
            !propertySource.getName().contains("plugin") && propertySource instanceof EnumerablePropertySource) {
                EnumerablePropertySource enumerablePropertySource = (EnumerablePropertySource) propertySource;
                for (String propertyName : enumerablePropertySource.getPropertyNames()) {
                    if (propertyName.startsWith(key) && propertyName.length() > key.length()) {
                        propertyNames.add(propertyName.substring(key.length() + 1));
                    }
                }
            }
        }
    }
}
Also used : EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource) Environment(grails.util.Environment) PropertySource(org.springframework.core.env.PropertySource) EnumerablePropertySource(org.springframework.core.env.EnumerablePropertySource)

Aggregations

PropertySource (org.springframework.core.env.PropertySource)10 EnumerablePropertySource (org.springframework.core.env.EnumerablePropertySource)5 LinkedHashMap (java.util.LinkedHashMap)3 CompositePropertySource (org.springframework.core.env.CompositePropertySource)3 MutablePropertySources (org.springframework.core.env.MutablePropertySources)3 Environment (grails.util.Environment)2 Test (org.junit.Test)2 MockPropertySource (org.springframework.mock.env.MockPropertySource)2 BitsquareEnvironment (io.bitsquare.app.BitsquareEnvironment)1 JOptCommandLinePropertySource (io.bitsquare.util.spring.JOptCommandLinePropertySource)1 ImmutableConfiguration (org.apache.commons.configuration2.ImmutableConfiguration)1 ApacheCommonsConfigurationService (org.libresonic.player.service.ApacheCommonsConfigurationService)1 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)1 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)1 MapPropertySource (org.springframework.core.env.MapPropertySource)1 PropertiesPropertySource (org.springframework.core.env.PropertiesPropertySource)1 PropertyResolver (org.springframework.core.env.PropertyResolver)1 PropertySourcesPropertyResolver (org.springframework.core.env.PropertySourcesPropertyResolver)1 StandardEnvironment (org.springframework.core.env.StandardEnvironment)1 Resource (org.springframework.core.io.Resource)1