/** * Creates new {@link RedisSentinelConfiguration}. */ public RedisSentinelConfiguration() { this(new MapPropertySource("RedisSentinelConfiguration", Collections.emptyMap())); }
/** * Create a new {@link MapConfigurationPropertySource} instance with entries copies * from the specified map. * @param map the source map */ public MapConfigurationPropertySource(Map<?, ?> map) { this.source = new LinkedHashMap<>(); this.delegate = new SpringIterableConfigurationPropertySource( new MapPropertySource("source", this.source), DefaultPropertyMapper.INSTANCE); putAll(map); }
@Override public void postProcessEnvironment(ConfigurableEnvironment env, SpringApplication application) { env.getPropertySources().addFirst(new MapPropertySource("gateway-properties", Collections.singletonMap("spring.webflux.hiddenmethod.filter.enabled", "false"))); } }
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);
private ConfigurableEnvironment getEnvironment(String profile) { ConfigurableEnvironment environment = new StandardEnvironment(); Map<String, Object> map = new HashMap<>(); map.put("spring.profiles.active", profile); map.put("spring.main.web-application-type", "none"); environment.getPropertySources().addFirst(new MapPropertySource("profiles", map)); return environment; }
private void addPropertySourceIfNeeded(Function<String, Map<String, String>> contentToMapFunction, String content, String name, CompositePropertySource composite) { Map<String, Object> map = new HashMap<>(); map.putAll(contentToMapFunction.apply(content)); if(map.isEmpty()) { LOG.warn("Property source: " + name + "will be ignored because no properties could be found"); } else { composite.addFirstPropertySource(new MapPropertySource(name, map)); } } }
@Test public void getProperty_withExplicitNullValue() { // java.util.Properties does not allow null values (because Hashtable does not) Map<String, Object> nullableProperties = new HashMap<>(); propertySources.addLast(new MapPropertySource("nullableProperties", nullableProperties)); nullableProperties.put("foo", null); assertThat(propertyResolver.getProperty("foo"), nullValue()); }
@Test public void addFirst() { PropertySource<?> p1 = new MapPropertySource("p1", Collections.emptyMap()); PropertySource<?> p2 = new MapPropertySource("p2", Collections.emptyMap()); PropertySource<?> p3 = new MapPropertySource("p3", Collections.emptyMap()); CompositePropertySource composite = new CompositePropertySource("c"); composite.addPropertySource(p2); composite.addPropertySource(p3); composite.addPropertySource(p1); composite.addFirstPropertySource(p1); String s = composite.toString(); int i1 = s.indexOf("name='p1'"); int i2 = s.indexOf("name='p2'"); int i3 = s.indexOf("name='p3'"); assertTrue("Bad order: " + s, ((i1 < i2) && (i2 < i3))); }
@Test @SuppressWarnings("serial") public void equals() { Map<String, Object> map1 = new HashMap<String, Object>() {{ put("a", "b"); }}; Map<String, Object> map2 = new HashMap<String, Object>() {{ put("c", "d"); }}; Properties props1 = new Properties() {{ setProperty("a", "b"); }}; Properties props2 = new Properties() {{ setProperty("c", "d"); }}; MapPropertySource mps = new MapPropertySource("mps", map1); assertThat(mps, equalTo(mps)); assertThat(new MapPropertySource("x", map1).equals(new MapPropertySource("x", map1)), is(true)); assertThat(new MapPropertySource("x", map1).equals(new MapPropertySource("x", map2)), is(true)); assertThat(new MapPropertySource("x", map1).equals(new PropertiesPropertySource("x", props1)), is(true)); assertThat(new MapPropertySource("x", map1).equals(new PropertiesPropertySource("x", props2)), is(true)); assertThat(new MapPropertySource("x", map1).equals(new Object()), is(false)); assertThat(new MapPropertySource("x", map1).equals("x"), is(false)); assertThat(new MapPropertySource("x", map1).equals(new MapPropertySource("y", map1)), is(false)); assertThat(new MapPropertySource("x", map1).equals(new MapPropertySource("y", map2)), is(false)); assertThat(new MapPropertySource("x", map1).equals(new PropertiesPropertySource("y", props1)), is(false)); assertThat(new MapPropertySource("x", map1).equals(new PropertiesPropertySource("y", props2)), is(false)); }
/** * Customize the set of property sources with those appropriate for any standard * Java environment: * <ul> * <li>{@value #SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME} * <li>{@value #SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME} * </ul> * <p>Properties present in {@value #SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME} will * take precedence over those in {@value #SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME}. * @see AbstractEnvironment#customizePropertySources(MutablePropertySources) * @see #getSystemProperties() * @see #getSystemEnvironment() */ @Override protected void customizePropertySources(MutablePropertySources propertySources) { propertySources.addLast(new MapPropertySource(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, getSystemProperties())); propertySources.addLast(new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment())); }
@Test @SuppressWarnings("serial") public void collectionsOperations() { Map<String, Object> map1 = new HashMap<String, Object>() {{ put("a", "b"); }}; Map<String, Object> map2 = new HashMap<String, Object>() {{ put("c", "d"); }}; PropertySource<?> ps1 = new MapPropertySource("ps1", map1); ps1.getSource(); List<PropertySource<?>> propertySources = new ArrayList<>(); assertThat(propertySources.add(ps1), equalTo(true)); assertThat(propertySources.contains(ps1), is(true)); assertThat(propertySources.contains(PropertySource.named("ps1")), is(true)); PropertySource<?> ps1replacement = new MapPropertySource("ps1", map2); // notice - different map assertThat(propertySources.add(ps1replacement), is(true)); // true because linkedlist allows duplicates assertThat(propertySources.size(), is(2)); assertThat(propertySources.remove(PropertySource.named("ps1")), is(true)); assertThat(propertySources.size(), is(1)); assertThat(propertySources.remove(PropertySource.named("ps1")), is(true)); assertThat(propertySources.size(), is(0)); PropertySource<?> ps2 = new MapPropertySource("ps2", map2); propertySources.add(ps1); propertySources.add(ps2); assertThat(propertySources.indexOf(PropertySource.named("ps1")), is(0)); assertThat(propertySources.indexOf(PropertySource.named("ps2")), is(1)); propertySources.clear(); }
@Test public void getProperty_doesNotCache_replaceExistingKeyPostConstruction() { String key = "foo"; String value1 = "bar"; String value2 = "biz"; HashMap<String, Object> map = new HashMap<>(); map.put(key, value1); // before construction MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addFirst(new MapPropertySource("testProperties", map)); PropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources); assertThat(propertyResolver.getProperty(key), equalTo(value1)); map.put(key, value2); // after construction and first resolution assertThat(propertyResolver.getProperty(key), equalTo(value2)); }
@Test public void getProperty_doesNotCache_addNewKeyPostConstruction() { HashMap<String, Object> map = new HashMap<>(); MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addFirst(new MapPropertySource("testProperties", map)); PropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources); assertThat(propertyResolver.getProperty("foo"), equalTo(null)); map.put("foo", "42"); assertThat(propertyResolver.getProperty("foo"), equalTo("42")); }
@Test public void orderingDoesntReplaceExisting() throws Exception { // SPR-12198: mySource should 'win' as it was registered manually AnnotationConfigApplicationContext ctxWithoutName = new AnnotationConfigApplicationContext(); MapPropertySource mySource = new MapPropertySource("mine", Collections.singletonMap("testbean.name", "myTestBean")); ctxWithoutName.getEnvironment().getPropertySources().addLast(mySource); ctxWithoutName.register(ConfigWithFourResourceLocations.class); ctxWithoutName.refresh(); assertThat(ctxWithoutName.getEnvironment().getProperty("testbean.name"), equalTo("myTestBean")); }
private PropertyResolver getVersionResolver(Class<?> sourceClass) { MutablePropertySources propertySources = new MutablePropertySources(); propertySources .addLast(new MapPropertySource("version", getVersionsMap(sourceClass))); return new PropertySourcesPropertyResolver(propertySources); }
@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(); }
@Override protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) { int availableServerSocket = SocketUtils.findAvailableTcpPort(5678); final Map<String, Object> sockets = new HashMap<String, Object>(); sockets.put("availableServerSocket", availableServerSocket); if (LOGGER.isInfoEnabled()) { LOGGER.info("Available Server Socket: " + availableServerSocket); } final MapPropertySource propertySource = new MapPropertySource("sockets", sockets); context.getEnvironment().getPropertySources().addLast(propertySource); super.loadBeanDefinitions(context, mergedConfig); }
/** * Customize the set of property sources with those appropriate for any standard * Java environment: * <ul> * <li>{@value #SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME} * <li>{@value #SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME} * </ul> * <p>Properties present in {@value #SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME} will * take precedence over those in {@value #SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME}. * @see AbstractEnvironment#customizePropertySources(MutablePropertySources) * @see #getSystemProperties() * @see #getSystemEnvironment() */ @Override protected void customizePropertySources(MutablePropertySources propertySources) { propertySources.addLast(new MapPropertySource(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, getSystemProperties())); propertySources.addLast(new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment())); }
@Override public org.springframework.core.env.PropertySource<?> locate(Environment environment) { CompositePropertySource composite = new CompositePropertySource("configService"); for (PropertySource source : repository.findOne(name, profiles, label) .getPropertySources()) { @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) source.getSource(); composite.addPropertySource(new MapPropertySource(source.getName(), map)); } return composite; }
@Test public void environmentAccess() throws Exception { Map<String, Object> map = new HashMap<>(); map.put("key.foo", "value.foo"); GenericApplicationContext wac = (GenericApplicationContext) context.getRequest().getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE); wac.getEnvironment().getPropertySources().addFirst(new MapPropertySource("mapSource", map)); wac.getDefaultListableBeanFactory().registerSingleton("bean2", context.getRequest().getAttribute("bean")); tag.setExpression("@environment['key.foo']"); int action = tag.doStartTag(); assertEquals(Tag.EVAL_BODY_INCLUDE, action); action = tag.doEndTag(); assertEquals(Tag.EVAL_PAGE, action); assertEquals("value.foo", ((MockHttpServletResponse) context.getResponse()).getContentAsString()); }