use of org.springframework.core.env.MapPropertySource in project spring-boot by spring-projects.
the class ConfigFileApplicationListenerTests method defaultPropertyAsFallbackDuringFileParsing.
@Test
public void defaultPropertyAsFallbackDuringFileParsing() throws Exception {
this.environment.getPropertySources().addLast(new MapPropertySource("defaultProperties", Collections.singletonMap("spring.config.name", (Object) "testproperties")));
this.initializer.postProcessEnvironment(this.environment, this.application);
String property = this.environment.getProperty("the.property");
assertThat(property).isEqualTo("frompropertiesfile");
}
use of org.springframework.core.env.MapPropertySource in project spring-boot by spring-projects.
the class SpringApplication method configurePropertySources.
/**
* Add, remove or re-order any {@link PropertySource}s in this application's
* environment.
* @param environment this application's environment
* @param args arguments passed to the {@code run} method
* @see #configureEnvironment(ConfigurableEnvironment, String[])
*/
protected void configurePropertySources(ConfigurableEnvironment environment, String[] args) {
MutablePropertySources sources = environment.getPropertySources();
if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) {
sources.addLast(new MapPropertySource("defaultProperties", this.defaultProperties));
}
if (this.addCommandLineProperties && args.length > 0) {
String name = CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME;
if (sources.contains(name)) {
PropertySource<?> source = sources.get(name);
CompositePropertySource composite = new CompositePropertySource(name);
composite.addPropertySource(new SimpleCommandLinePropertySource(name + "-" + args.hashCode(), args));
composite.addPropertySource(source);
sources.replace(name, composite);
} else {
sources.addFirst(new SimpleCommandLinePropertySource(args));
}
}
}
use of org.springframework.core.env.MapPropertySource in project spring-framework by spring-projects.
the class ImportResourceTests method importWithPlaceholder.
@Test
public void importWithPlaceholder() throws Exception {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
PropertySource<?> propertySource = new MapPropertySource("test", Collections.<String, Object>singletonMap("test", "springframework"));
ctx.getEnvironment().getPropertySources().addFirst(propertySource);
ctx.register(ImportXmlConfig.class);
ctx.refresh();
assertTrue("did not contain xml-declared bean", ctx.containsBean("xmlDeclaredBean"));
ctx.close();
}
use of org.springframework.core.env.MapPropertySource in project spring-framework by spring-projects.
the class TestPropertySourceUtils method addInlinedPropertiesToEnvironment.
/**
* Add the given <em>inlined properties</em> (in the form of <em>key-value</em>
* pairs) to the supplied {@link ConfigurableEnvironment environment}.
* <p>All key-value pairs will be added to the {@code Environment} as a
* single {@link MapPropertySource} with the highest precedence.
* <p>For details on the parsing of <em>inlined properties</em>, consult the
* Javadoc for {@link #convertInlinedPropertiesToMap}.
* @param environment the environment to update; never {@code null}
* @param inlinedProperties the inlined properties to add to the environment;
* potentially empty but never {@code null}
* @since 4.1.5
* @see MapPropertySource
* @see #INLINED_PROPERTIES_PROPERTY_SOURCE_NAME
* @see TestPropertySource#properties
* @see #convertInlinedPropertiesToMap
*/
public static void addInlinedPropertiesToEnvironment(ConfigurableEnvironment environment, String... inlinedProperties) {
Assert.notNull(environment, "'environment' must not be null");
Assert.notNull(inlinedProperties, "'inlinedProperties' must not be null");
if (!ObjectUtils.isEmpty(inlinedProperties)) {
if (logger.isDebugEnabled()) {
logger.debug("Adding inlined properties to environment: " + ObjectUtils.nullSafeToString(inlinedProperties));
}
MapPropertySource ps = (MapPropertySource) environment.getPropertySources().get(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME);
if (ps == null) {
ps = new MapPropertySource(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME, new LinkedHashMap<>());
environment.getPropertySources().addFirst(ps);
}
ps.getSource().putAll(convertInlinedPropertiesToMap(inlinedProperties));
}
}
use of org.springframework.core.env.MapPropertySource in project spring-boot by spring-projects.
the class AbstractEndpointTests method isOptIn.
@Test
public void isOptIn() throws Exception {
this.context = new AnnotationConfigApplicationContext();
Map<String, Object> source = new HashMap<>();
source.put("endpoints.enabled", false);
source.put(this.property + ".enabled", true);
PropertySource<?> propertySource = new MapPropertySource("test", source);
this.context.getEnvironment().getPropertySources().addFirst(propertySource);
this.context.register(this.configClass);
this.context.refresh();
assertThat(getEndpointBean().isEnabled()).isTrue();
}
Aggregations