@Test public void testBindWithDashPrefix() throws Exception { // gh-4045 this.targetName = "foo-bar"; MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addLast(new SystemEnvironmentPropertySource("systemEnvironment", Collections.<String, Object>singletonMap("FOO_BAR_NAME", "blah"))); propertySources.addLast(new RandomValuePropertySource()); setupFactory(); this.factory.setPropertySources(propertySources); this.factory.afterPropertiesSet(); Foo foo = this.factory.getObject(); assertThat(foo.name).isEqualTo("blah"); }
@Test public void testBindWithDelimitedPrefixUsingMatchingDelimiter() throws Exception { this.targetName = "env_foo"; this.ignoreUnknownFields = false; MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addLast(new SystemEnvironmentPropertySource("systemEnvironment", Collections.<String, Object>singletonMap("ENV_FOO_NAME", "blah"))); propertySources.addLast(new RandomValuePropertySource("random")); setupFactory(); this.factory.setPropertySources(propertySources); this.factory.afterPropertiesSet(); Foo foo = this.factory.getObject(); assertThat(foo.name).isEqualTo("blah"); }
@Test public void testBindWithDelimitedPrefixUsingDifferentDelimiter() throws Exception { this.targetName = "env.foo"; MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addLast(new SystemEnvironmentPropertySource("systemEnvironment", Collections.<String, Object>singletonMap("ENV_FOO_NAME", "blah"))); propertySources.addLast(new RandomValuePropertySource("random")); this.ignoreUnknownFields = false; setupFactory(); this.factory.setPropertySources(propertySources); this.factory.afterPropertiesSet(); Foo foo = this.factory.getObject(); assertThat(foo.name).isEqualTo("blah"); }
@Test public void testBindWithDashPrefix() throws Exception { // gh-4045 this.targetName = "foo-bar"; MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addLast(new SystemEnvironmentPropertySource("systemEnvironment", Collections.<String, Object>singletonMap("FOO_BAR_NAME", "blah"))); propertySources.addLast(new RandomValuePropertySource("random")); setupFactory(); this.factory.setPropertySources(propertySources); this.factory.afterPropertiesSet(); Foo foo = this.factory.getObject(); assertEquals("blah", foo.name); }
@Override public void initialize(ConfigurableApplicationContext applicationContext) { ConfigurableEnvironment environment = applicationContext.getEnvironment(); MutablePropertySources propertySources = environment.getPropertySources(); Set<String> found = new LinkedHashSet<>(); Map<String, Object> map = decrypt(propertySources); if (!map.isEmpty()) { // We have some decrypted properties found.addAll(map.keySet()); insert(applicationContext, new SystemEnvironmentPropertySource( DECRYPTED_PROPERTY_SOURCE_NAME, map)); } PropertySource<?> bootstrap = propertySources .get(BootstrapApplicationListener.BOOTSTRAP_PROPERTY_SOURCE_NAME); if (bootstrap != null) { map = decrypt(bootstrap); if (!map.isEmpty()) { found.addAll(map.keySet()); insert(applicationContext, new SystemEnvironmentPropertySource( DECRYPTED_BOOTSTRAP_PROPERTY_SOURCE_NAME, map)); } } if (!found.isEmpty()) { ApplicationContext parent = applicationContext.getParent(); if (parent != null) { // The parent is actually the bootstrap context, and it is fully // initialized, so we can fire an EnvironmentChangeEvent there to rebind // @ConfigurationProperties, in case they were encrypted. parent.publishEvent(new EnvironmentChangeEvent(parent, found)); } } }
@SuppressWarnings("unchecked") private static Map<String, Object> getOrAdd(MutablePropertySources sources, String name) { if (sources.contains(name)) { return (Map<String, Object>) sources.get(name).getSource(); } Map<String, Object> map = new HashMap<>(); sources.addFirst(new SystemEnvironmentPropertySource(name, map)); return map; }