/** * Create a new {@code MutablePropertySources} from the given propertySources * object, preserving the original order of contained {@code PropertySource} objects. */ public MutablePropertySources(PropertySources propertySources) { this(); for (PropertySource<?> propertySource : propertySources) { addLast(propertySource); } }
/** * Create a new {@code MutablePropertySources} from the given propertySources * object, preserving the original order of contained {@code PropertySource} objects. */ public MutablePropertySources(PropertySources propertySources) { this(); for (PropertySource<?> propertySource : propertySources) { addLast(propertySource); } }
/** * Create a new {@code MockEnvironment} with a single {@link MockPropertySource}. */ public MockEnvironment() { getPropertySources().addLast(this.propertySource); }
private void processPropertySource(ConfigurableEnvironment environment, ResourceLoader resourceLoader) { try { PropertySource nacosDefaultPropertySource = buildPropertySource(resourceLoader); MutablePropertySources propertySources = environment.getPropertySources(); // append nacosDefaultPropertySource as last one in order to be overrided by higher order propertySources.addLast(nacosDefaultPropertySource); } catch (IOException e) { throw new IllegalStateException(e.getMessage(), e); } }
/** * Get Sub {@link Properties} * * @param propertySources {@link PropertySource} Iterable * @param prefix the prefix of property name * @return Map * @see Properties */ public static Map<String, Object> getSubProperties(Iterable<PropertySource<?>> propertySources, String prefix) { // Non-Extension AbstractEnvironment AbstractEnvironment environment = new AbstractEnvironment() { }; MutablePropertySources mutablePropertySources = environment.getPropertySources(); for (PropertySource<?> source : propertySources) { mutablePropertySources.addLast(source); } return getSubProperties(environment, prefix); }
/** * 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())); }
/** * Get Sub {@link Properties} * * @param propertySources {@link PropertySource} Iterable * @param prefix the prefix of property name * @return Map * @see Properties */ public static Map<String, Object> getSubProperties(Iterable<PropertySource<?>> propertySources, String prefix) { // Non-Extension AbstractEnvironment AbstractEnvironment environment = new AbstractEnvironment() { }; MutablePropertySources mutablePropertySources = environment.getPropertySources(); for (PropertySource<?> source : propertySources) { mutablePropertySources.addLast(source); } return getSubProperties(environment, prefix); }
@Override public void merge(ConfigurableEnvironment parent) { for (PropertySource<?> ps : parent.getPropertySources()) { if (!this.propertySources.contains(ps.getName())) { this.propertySources.addLast(ps); } } String[] parentActiveProfiles = parent.getActiveProfiles(); if (!ObjectUtils.isEmpty(parentActiveProfiles)) { synchronized (this.activeProfiles) { for (String profile : parentActiveProfiles) { this.activeProfiles.add(profile); } } } String[] parentDefaultProfiles = parent.getDefaultProfiles(); if (!ObjectUtils.isEmpty(parentDefaultProfiles)) { synchronized (this.defaultProfiles) { this.defaultProfiles.remove(RESERVED_DEFAULT_PROFILE_NAME); for (String profile : parentDefaultProfiles) { this.defaultProfiles.add(profile); } } } }
/** * Create a new {@code MockEnvironment} with a single {@link MockPropertySource}. */ public MockEnvironment() { getPropertySources().addLast(this.propertySource); }
propertySources.addLast(new StubPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME)); propertySources.addLast(new StubPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)); if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) { propertySources.addLast(new JndiPropertySource(JNDI_PROPERTY_SOURCE_NAME));
propertySources.addLast(propertySource);
@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 streamContainsPropertySource() { MutablePropertySources sources = new MutablePropertySources(); sources.addLast(new MockPropertySource("test")); assertThat(sources.stream(), notNullValue()); assertThat(sources.stream().count(), is(1L)); assertThat(sources.stream().anyMatch(source -> "test".equals(source.getName())), is(true)); assertThat(sources.stream().anyMatch(source -> "bogus".equals(source.getName())), is(false)); }
@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")); }
@Test public void iteratorContainsPropertySource() { MutablePropertySources sources = new MutablePropertySources(); sources.addLast(new MockPropertySource("test")); Iterator<PropertySource<?>> it = sources.iterator(); assertTrue(it.hasNext()); assertEquals("test", it.next().getName()); try { it.remove(); fail("Should have thrown UnsupportedOperationException"); } catch (UnsupportedOperationException ex) { // expected } assertFalse(it.hasNext()); }
@Test public void explicitPropertySources() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addLast(new MockPropertySource().withProperty("my.name", "foo")); PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); ppc.setPropertySources(propertySources); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo("foo")); assertEquals(ppc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next()); }
@Test public void getPropertySources_replacePropertySource() { propertySources = new MutablePropertySources(); propertyResolver = new PropertySourcesPropertyResolver(propertySources); propertySources.addLast(new MockPropertySource("local").withProperty("foo", "localValue")); propertySources.addLast(new MockPropertySource("system").withProperty("foo", "systemValue")); // 'local' was added first so has precedence assertThat(propertyResolver.getProperty("foo"), equalTo("localValue")); // replace 'local' with new property source propertySources.replace("local", new MockPropertySource("new").withProperty("foo", "newValue")); // 'system' now has precedence assertThat(propertyResolver.getProperty("foo"), equalTo("newValue")); assertThat(propertySources.size(), is(2)); }
@Test public void explicitPropertySourcesExcludesEnvironment() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addLast(new MockPropertySource()); PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); ppc.setPropertySources(propertySources); ppc.setEnvironment(new MockEnvironment().withProperty("my.name", "env")); ppc.setIgnoreUnresolvablePlaceholders(true); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}")); assertEquals(ppc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next()); }
@Test @SuppressWarnings("serial") public void explicitPropertySourcesExcludesLocalProperties() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("testBean", genericBeanDefinition(TestBean.class) .addPropertyValue("name", "${my.name}") .getBeanDefinition()); MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addLast(new MockPropertySource()); PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); ppc.setPropertySources(propertySources); ppc.setProperties(new Properties() {{ put("my.name", "local"); }}); ppc.setIgnoreUnresolvablePlaceholders(true); ppc.postProcessBeanFactory(bf); assertThat(bf.getBean(TestBean.class).getName(), equalTo("${my.name}")); }
ConfigurableEnvironment child = new StandardEnvironment(); child.setActiveProfiles("c1", "c2"); child.getPropertySources().addLast( new MockPropertySource("childMock") .withProperty("childKey", "childVal") parent.getPropertySources().addLast( new MockPropertySource("parentMock") .withProperty("parentKey", "parentVal")