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);
}
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());
}
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"));
}
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);
}
}
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));
}
Aggregations