Search in sources :

Example 46 with MutablePropertySources

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

the class PropertySourcesPlaceholderConfigurer method postProcessBeanFactory.

/**
	 * {@inheritDoc}
	 * <p>Processing occurs by replacing ${...} placeholders in bean definitions by resolving each
	 * against this configurer's set of {@link PropertySources}, which includes:
	 * <ul>
	 * <li>all {@linkplain org.springframework.core.env.ConfigurableEnvironment#getPropertySources
	 * environment property sources}, if an {@code Environment} {@linkplain #setEnvironment is present}
	 * <li>{@linkplain #mergeProperties merged local properties}, if {@linkplain #setLocation any}
	 * {@linkplain #setLocations have} {@linkplain #setProperties been}
	 * {@linkplain #setPropertiesArray specified}
	 * <li>any property sources set by calling {@link #setPropertySources}
	 * </ul>
	 * <p>If {@link #setPropertySources} is called, <strong>environment and local properties will be
	 * ignored</strong>. This method is designed to give the user fine-grained control over property
	 * sources, and once set, the configurer makes no assumptions about adding additional sources.
	 */
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    if (this.propertySources == null) {
        this.propertySources = new MutablePropertySources();
        if (this.environment != null) {
            this.propertySources.addLast(new PropertySource<Environment>(ENVIRONMENT_PROPERTIES_PROPERTY_SOURCE_NAME, this.environment) {

                @Override
                public String getProperty(String key) {
                    return this.source.getProperty(key);
                }
            });
        }
        try {
            PropertySource<?> localPropertySource = new PropertiesPropertySource(LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME, mergeProperties());
            if (this.localOverride) {
                this.propertySources.addFirst(localPropertySource);
            } else {
                this.propertySources.addLast(localPropertySource);
            }
        } catch (IOException ex) {
            throw new BeanInitializationException("Could not load properties", ex);
        }
    }
    processProperties(beanFactory, new PropertySourcesPropertyResolver(this.propertySources));
    this.appliedPropertySources = this.propertySources;
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) PropertySourcesPropertyResolver(org.springframework.core.env.PropertySourcesPropertyResolver) PropertiesPropertySource(org.springframework.core.env.PropertiesPropertySource) Environment(org.springframework.core.env.Environment) MutablePropertySources(org.springframework.core.env.MutablePropertySources) IOException(java.io.IOException)

Example 47 with MutablePropertySources

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

the class PropertySourcesPlaceholderConfigurerTests method explicitPropertySourcesExcludesLocalProperties.

@Test
@SuppressWarnings("serial")
public void explicitPropertySourcesExcludesLocalProperties() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class).addPropertyValue("name", "${my.name}").getBeanDefinition());
    MutablePropertySources propertySources = new MutablePropertySources();
    propertySources.addLast(new MockPropertySource());
    PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer();
    ppc.setPropertySources(propertySources);
    ppc.setProperties(new Properties() {

        {
            put("my.name", "local");
        }
    });
    ppc.setIgnoreUnresolvablePlaceholders(true);
    ppc.postProcessBeanFactory(bf);
    assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}"));
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) MutablePropertySources(org.springframework.core.env.MutablePropertySources) MockPropertySource(org.springframework.mock.env.MockPropertySource) Properties(java.util.Properties) Test(org.junit.Test)

Example 48 with MutablePropertySources

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

the class ConfigurationClassParser method addPropertySource.

private void addPropertySource(PropertySource<?> propertySource) {
    String name = propertySource.getName();
    MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources();
    if (propertySources.contains(name) && this.propertySourceNames.contains(name)) {
        // We've already added a version, we need to extend it
        PropertySource<?> existing = propertySources.get(name);
        PropertySource<?> newSource = (propertySource instanceof ResourcePropertySource ? ((ResourcePropertySource) propertySource).withResourceName() : propertySource);
        if (existing instanceof CompositePropertySource) {
            ((CompositePropertySource) existing).addFirstPropertySource(newSource);
        } else {
            if (existing instanceof ResourcePropertySource) {
                existing = ((ResourcePropertySource) existing).withResourceName();
            }
            CompositePropertySource composite = new CompositePropertySource(name);
            composite.addPropertySource(newSource);
            composite.addPropertySource(existing);
            propertySources.replace(name, composite);
        }
    } else {
        if (this.propertySourceNames.isEmpty()) {
            propertySources.addLast(propertySource);
        } else {
            String firstProcessed = this.propertySourceNames.get(this.propertySourceNames.size() - 1);
            propertySources.addBefore(firstProcessed, propertySource);
        }
    }
    this.propertySourceNames.add(name);
}
Also used : ResourcePropertySource(org.springframework.core.io.support.ResourcePropertySource) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) CompositePropertySource(org.springframework.core.env.CompositePropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources)

Example 49 with MutablePropertySources

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

the class PropertySourceAnnotationTests method withExplicitName.

@Test
public void withExplicitName() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(ConfigWithExplicitName.class);
    ctx.refresh();
    assertTrue("property source p1 was not added", ctx.getEnvironment().getPropertySources().contains("p1"));
    assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean"));
    // assert that the property source was added last to the set of sources
    String name;
    MutablePropertySources sources = ctx.getEnvironment().getPropertySources();
    Iterator<org.springframework.core.env.PropertySource<?>> iterator = sources.iterator();
    do {
        name = iterator.next().getName();
    } while (iterator.hasNext());
    assertThat(name, is("p1"));
}
Also used : MutablePropertySources(org.springframework.core.env.MutablePropertySources) MapPropertySource(org.springframework.core.env.MapPropertySource) Test(org.junit.Test)

Example 50 with MutablePropertySources

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

the class TestPropertySourceUtilsTests method addInlinedPropertiesToEnvironmentWithEmptyProperty.

@Test
@SuppressWarnings("rawtypes")
public void addInlinedPropertiesToEnvironmentWithEmptyProperty() {
    ConfigurableEnvironment environment = new MockEnvironment();
    MutablePropertySources propertySources = environment.getPropertySources();
    propertySources.remove(MockPropertySource.MOCK_PROPERTIES_PROPERTY_SOURCE_NAME);
    assertEquals(0, propertySources.size());
    addInlinedPropertiesToEnvironment(environment, asArray("  "));
    assertEquals(1, propertySources.size());
    assertEquals(0, ((Map) propertySources.iterator().next().getSource()).size());
}
Also used : ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) MockEnvironment(org.springframework.mock.env.MockEnvironment) MutablePropertySources(org.springframework.core.env.MutablePropertySources) Test(org.junit.Test)

Aggregations

MutablePropertySources (org.springframework.core.env.MutablePropertySources)50 Test (org.junit.Test)19 MapPropertySource (org.springframework.core.env.MapPropertySource)11 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)10 PropertiesPropertySource (org.springframework.core.env.PropertiesPropertySource)8 MockPropertySource (org.springframework.mock.env.MockPropertySource)8 Properties (java.util.Properties)6 PropertySourcesPropertyResolver (org.springframework.core.env.PropertySourcesPropertyResolver)6 CompositePropertySource (org.springframework.core.env.CompositePropertySource)5 ByteArrayResource (org.springframework.core.io.ByteArrayResource)4 LinkedHashMap (java.util.LinkedHashMap)3 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)3 RandomValuePropertySource (org.springframework.boot.env.RandomValuePropertySource)3 PropertySource (org.springframework.core.env.PropertySource)3 StandardEnvironment (org.springframework.core.env.StandardEnvironment)3 SystemEnvironmentPropertySource (org.springframework.core.env.SystemEnvironmentPropertySource)3 MockEnvironment (org.springframework.mock.env.MockEnvironment)3 TestBean (org.springframework.tests.sample.beans.TestBean)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2