Search in sources :

Example 26 with MutablePropertySources

use of org.springframework.core.env.MutablePropertySources in project uPortal by Jasig.

the class PortalPropertySourcesPlaceholderConfigurer method postProcessBeanFactory.

/**
     * Override the postProcessing. The default PropertySourcesPlaceholderConfigurer does not inject
     * local properties into the Environment object. It builds a local list of properties files and
     * then uses a transient Resolver to resolve the @Value annotations. That means that you are
     * unable to get to "local" properties (eg. portal.properties) after bean post-processing has
     * completed unless you are going to re-parse those file. This is similar to what
     * PropertiesManager does, but it uses all the property files configured, not just
     * portal.properties.
     *
     * <p>If we upgrade to spring 4, there are better/more efficient solutions available. I'm not
     * aware of better solutions for spring 3.x.
     *
     * @param beanFactory the bean factory
     * @throws BeansException if an error occurs while loading properties or wiring up beans
     */
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    if (propertyResolver == null) {
        try {
            MutablePropertySources sources = new MutablePropertySources();
            PropertySource<?> localPropertySource = new PropertiesPropertySource(EXTENDED_PROPERTIES_SOURCE, mergeProperties());
            sources.addLast(localPropertySource);
            propertyResolver = new PropertySourcesPropertyResolver(sources);
        } catch (IOException e) {
            throw new BeanInitializationException("Could not load properties", e);
        }
    }
    super.postProcessBeanFactory(beanFactory);
}
Also used : PropertySourcesPropertyResolver(org.springframework.core.env.PropertySourcesPropertyResolver) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) PropertiesPropertySource(org.springframework.core.env.PropertiesPropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources) IOException(java.io.IOException)

Example 27 with MutablePropertySources

use of org.springframework.core.env.MutablePropertySources in project hevelian-activemq by Hevelian.

the class ReversePropertySourcesStandardServletEnvironmentTest method customizePropertySources.

@Test
public void customizePropertySources() {
    MutablePropertySources propertySources = new MutablePropertySources();
    ReversePropertySourcesStandardServletEnvironment env = new ReversePropertySourcesStandardServletEnvironment();
    env.customizePropertySources(propertySources);
    String[] sourceNames = { StandardServletEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, StandardServletEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME, StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME };
    List<String> result = new ArrayList<>();
    propertySources.forEach(a -> result.add(a.getName()));
    Assert.assertArrayEquals(sourceNames, result.toArray());
}
Also used : ArrayList(java.util.ArrayList) MutablePropertySources(org.springframework.core.env.MutablePropertySources) Test(org.junit.Test)

Example 28 with MutablePropertySources

use of org.springframework.core.env.MutablePropertySources 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 29 with MutablePropertySources

use of org.springframework.core.env.MutablePropertySources in project engine by craftercms.

the class SiteContextFactory method getApplicationContext.

protected ConfigurableApplicationContext getApplicationContext(SiteContext siteContext, URLClassLoader classLoader, HierarchicalConfiguration config, String[] applicationContextPaths, ResourceLoader resourceLoader) {
    try {
        List<Resource> resources = new ArrayList<>();
        for (String path : applicationContextPaths) {
            Resource resource = resourceLoader.getResource(path);
            if (resource.exists()) {
                resources.add(resource);
            }
        }
        if (CollectionUtils.isNotEmpty(resources)) {
            String siteName = siteContext.getSiteName();
            logger.info("--------------------------------------------------");
            logger.info("<Loading application context for site: " + siteName + ">");
            logger.info("--------------------------------------------------");
            GenericApplicationContext appContext = new GenericApplicationContext(mainApplicationContext);
            appContext.setClassLoader(classLoader);
            if (config != null) {
                MutablePropertySources propertySources = appContext.getEnvironment().getPropertySources();
                propertySources.addFirst(new ApacheCommonsConfigPropertySource("siteConfig", config));
            }
            XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(appContext);
            reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
            for (Resource resource : resources) {
                reader.loadBeanDefinitions(resource);
            }
            appContext.refresh();
            logger.info("--------------------------------------------------");
            logger.info("</Loading application context for site: " + siteName + ">");
            logger.info("--------------------------------------------------");
            return appContext;
        } else {
            return null;
        }
    } catch (Exception e) {
        throw new SiteContextCreationException("Unable to load application context for site '" + siteContext.getSiteName() + "'", e);
    }
}
Also used : SiteContextCreationException(org.craftercms.engine.exception.SiteContextCreationException) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) ApacheCommonsConfigPropertySource(org.craftercms.engine.util.spring.ApacheCommonsConfigPropertySource) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) ArrayList(java.util.ArrayList) Resource(org.springframework.core.io.Resource) MutablePropertySources(org.springframework.core.env.MutablePropertySources) SiteContextCreationException(org.craftercms.engine.exception.SiteContextCreationException) BeansException(org.springframework.beans.BeansException) ConfigurationException(org.apache.commons.configuration.ConfigurationException)

Example 30 with MutablePropertySources

use of org.springframework.core.env.MutablePropertySources 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

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