Search in sources :

Example 11 with MapPropertySource

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");
}
Also used : MapPropertySource(org.springframework.core.env.MapPropertySource) Test(org.junit.Test)

Example 12 with MapPropertySource

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));
        }
    }
}
Also used : CompositePropertySource(org.springframework.core.env.CompositePropertySource) SimpleCommandLinePropertySource(org.springframework.core.env.SimpleCommandLinePropertySource) MapPropertySource(org.springframework.core.env.MapPropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources)

Example 13 with MapPropertySource

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();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) MapPropertySource(org.springframework.core.env.MapPropertySource) Test(org.junit.Test)

Example 14 with MapPropertySource

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));
    }
}
Also used : MapPropertySource(org.springframework.core.env.MapPropertySource) LinkedHashMap(java.util.LinkedHashMap)

Example 15 with MapPropertySource

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();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) HashMap(java.util.HashMap) MapPropertySource(org.springframework.core.env.MapPropertySource) Test(org.junit.Test)

Aggregations

MapPropertySource (org.springframework.core.env.MapPropertySource)72 Test (org.junit.Test)52 HashMap (java.util.HashMap)35 StandardEnvironment (org.springframework.core.env.StandardEnvironment)26 URI (java.net.URI)24 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)23 MessageSerDe (com.kixeye.chassis.transport.serde.MessageSerDe)21 ProtobufMessageSerDe (com.kixeye.chassis.transport.serde.converter.ProtobufMessageSerDe)15 RestTemplate (org.springframework.web.client.RestTemplate)15 ServiceError (com.kixeye.chassis.transport.dto.ServiceError)14 JsonJacksonMessageSerDe (com.kixeye.chassis.transport.serde.converter.JsonJacksonMessageSerDe)14 XmlMessageSerDe (com.kixeye.chassis.transport.serde.converter.XmlMessageSerDe)14 YamlJacksonMessageSerDe (com.kixeye.chassis.transport.serde.converter.YamlJacksonMessageSerDe)14 SerDeHttpMessageConverter (com.kixeye.chassis.transport.http.SerDeHttpMessageConverter)13 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)13 MutablePropertySources (org.springframework.core.env.MutablePropertySources)10 QueuingWebSocketListener (com.kixeye.chassis.transport.websocket.QueuingWebSocketListener)9 WebSocketMessageRegistry (com.kixeye.chassis.transport.websocket.WebSocketMessageRegistry)9 ArrayList (java.util.ArrayList)9 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)9