org.springframework.core.env.MutablePropertySources#get ( )源码实例Demo

下面列出了org.springframework.core.env.MutablePropertySources#get ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@Test
public void orderingIsCorrect() {
	refresher.refresh();
	MutablePropertySources propertySources = environment.getPropertySources();
	PropertySource<?> test1 = propertySources
			.get("bootstrapProperties-testContextRefresherOrdering1");
	PropertySource<?> test2 = propertySources
			.get("bootstrapProperties-testContextRefresherOrdering2");
	PropertySource<?> test3 = propertySources
			.get("bootstrapProperties-testContextRefresherOrdering3");
	int index1 = propertySources.precedenceOf(test1);
	int index2 = propertySources.precedenceOf(test2);
	int index3 = propertySources.precedenceOf(test3);
	assertThat(index1).as("source1 index not less then source2").isLessThan(index2);
	assertThat(index2).as("source2 index not less then source3").isLessThan(index3);
}
 
/**
 * Attach a {@link ConfigurationPropertySource} support to the specified
 * {@link Environment}. Adapts each {@link PropertySource} managed by the environment
 * to a {@link ConfigurationPropertySource} and allows classic
 * {@link PropertySourcesPropertyResolver} calls to resolve using
 * {@link ConfigurationPropertyName configuration property names}.
 * <p>
 * The attached resolver will dynamically track any additions or removals from the
 * underlying {@link Environment} property sources.
 *
 * @param environment the source environment (must be an instance of
 *                    {@link ConfigurableEnvironment})
 * @see #get(Environment)
 */
public static void attach(Environment environment) {
    Assert.isInstanceOf(ConfigurableEnvironment.class, environment);
    MutablePropertySources sources = ((ConfigurableEnvironment) environment)
            .getPropertySources();
    PropertySource<?> attached = sources.get(ATTACHED_PROPERTY_SOURCE_NAME);
    if (attached != null && attached.getSource() != sources) {
        sources.remove(ATTACHED_PROPERTY_SOURCE_NAME);
        attached = null;
    }
    if (attached == null) {
        sources.addFirst(new ConfigurationPropertySourcesPropertySource(
                ATTACHED_PROPERTY_SOURCE_NAME,
                new SpringConfigurationPropertySources(sources)));
    }
}
 
private void mergeDefaultProperties(MutablePropertySources environment,
		MutablePropertySources bootstrap) {
	String name = DEFAULT_PROPERTIES;
	if (bootstrap.contains(name)) {
		PropertySource<?> source = bootstrap.get(name);
		if (!environment.contains(name)) {
			environment.addLast(source);
		}
		else {
			PropertySource<?> target = environment.get(name);
			if (target instanceof MapPropertySource && target != source
					&& source instanceof MapPropertySource) {
				Map<String, Object> targetMap = ((MapPropertySource) target)
						.getSource();
				Map<String, Object> map = ((MapPropertySource) source).getSource();
				for (String key : map.keySet()) {
					if (!target.containsProperty(key)) {
						targetMap.put(key, map.get(key));
					}
				}
			}
		}
	}
	mergeAdditionalPropertySources(environment, bootstrap);
}
 
private void mergeAdditionalPropertySources(MutablePropertySources environment,
		MutablePropertySources bootstrap) {
	PropertySource<?> defaultProperties = environment.get(DEFAULT_PROPERTIES);
	ExtendedDefaultPropertySource result = defaultProperties instanceof ExtendedDefaultPropertySource
			? (ExtendedDefaultPropertySource) defaultProperties
			: new ExtendedDefaultPropertySource(DEFAULT_PROPERTIES,
					defaultProperties);
	for (PropertySource<?> source : bootstrap) {
		if (!environment.contains(source.getName())) {
			result.add(source);
		}
	}
	for (String name : result.getPropertySourceNames()) {
		bootstrap.remove(name);
	}
	addOrReplace(environment, result);
	addOrReplace(bootstrap, result);
}
 
private void addPropertySource(PropertySource<?> propertySource) {
	String name = propertySource.getName();
	MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources();

	if (this.propertySourceNames.contains(name)) {
		// We've already added a version, we need to extend it
		PropertySource<?> existing = propertySources.get(name);
		if (existing != null) {
			PropertySource<?> newSource = (propertySource instanceof ResourcePropertySource ?
					((ResourcePropertySource) propertySource).withResourceName() : propertySource);
			if (existing instanceof CompositePropertySource) {
				((CompositePropertySource) existing).addFirstPropertySource(newSource);
			}
			else {
				if (existing instanceof ResourcePropertySource) {
					existing = ((ResourcePropertySource) existing).withResourceName();
				}
				CompositePropertySource composite = new CompositePropertySource(name);
				composite.addPropertySource(newSource);
				composite.addPropertySource(existing);
				propertySources.replace(name, composite);
			}
			return;
		}
	}

	if (this.propertySourceNames.isEmpty()) {
		propertySources.addLast(propertySource);
	}
	else {
		String firstProcessed = this.propertySourceNames.get(this.propertySourceNames.size() - 1);
		propertySources.addBefore(firstProcessed, propertySource);
	}
	this.propertySourceNames.add(name);
}
 
@Test
public void propertySourcesFound() throws Exception {
    String foo = this.environment.getProperty("foo");
    assertThat(foo).as("foo was wrong").isEqualTo("bar-app-dev");

    String myBaz = this.environment.getProperty("my.baz");
    assertThat(myBaz).as("my.baz was wrong").isEqualTo("bar-app-dev");

    MutablePropertySources propertySources = this.environment.getPropertySources();
    PropertySource<?> bootstrapProperties = propertySources
            .get("bootstrapProperties");
    assertThat(bootstrapProperties).as("bootstrapProperties was null").isNotNull();
    assertThat(bootstrapProperties).as("bootstrapProperties was wrong type")
            .isInstanceOf(CompositePropertySource.class);

    Collection<PropertySource<?>> consulSources = ((CompositePropertySource) bootstrapProperties)
            .getPropertySources();
    assertThat(consulSources).as("consulSources was wrong size").hasSize(1);

    PropertySource<?> consulSource = consulSources.iterator().next();
    assertThat(consulSource).as("consulSource was wrong type")
            .isInstanceOf(CompositePropertySource.class);
    Collection<PropertySource<?>> fileSources = ((CompositePropertySource) consulSource)
            .getPropertySources();
    assertThat(fileSources).as("fileSources was wrong size").hasSize(4);

    assertFileSourceNames(fileSources, APP_NAME_DEV_PROPS, APP_NAME_PROPS,
            APPLICATION_DEV_YML, APPLICATION_YML);
}
 
@Override
public void contextLoaded(ConfigurableApplicationContext context) {
    // 从 ConfigurableApplicationContext 获取 ConfigurableEnvironment
    ConfigurableEnvironment environment = context.getEnvironment();
    MutablePropertySources propertySources = environment.getPropertySources();
    // 通过名称获取名为 "systemProperties" 的 PropertySource(实现使用常量)
    PropertySource propertySource = propertySources.get(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME);
    // 将 "systemProperties" 的 PropertySource 添加在 "contextPrepared" 之前(由 contextPrepared 方法添加),提高优先级
    propertySources.addBefore("contextPrepared", propertySource);
}
 
源代码8 项目: ambari-logsearch   文件: LogFeederProps.java
@PostConstruct
public void init() {
  properties = new Properties();
  MutablePropertySources propSrcs = ((AbstractEnvironment) env).getPropertySources();
  ResourcePropertySource propertySource = (ResourcePropertySource) propSrcs.get("class path resource [" +
    LogFeederConstants.LOGFEEDER_PROPERTIES_FILE + "]");
  if (propertySource != null) {
    Stream.of(propertySource)
      .map(MapPropertySource::getPropertyNames)
      .flatMap(Arrays::<String>stream)
      .forEach(propName -> properties.setProperty(propName, env.getProperty(propName)));
  } else {
    throw new IllegalArgumentException("Cannot find logfeeder.properties on the classpath");
  }
}
 
@PostConstruct
public void initialize() {
	MutablePropertySources propertySources = environment.getPropertySources();
	PropertySource propertySource = propertySources
			.get(RandomValuePropertySource.RANDOM_PROPERTY_SOURCE_NAME);
	if (propertySource != null) {
		propertySources.addLast(new CachedRandomPropertySource(propertySource));
	}
}
 
源代码10 项目: lams   文件: ConfigurationClassParser.java
private void addPropertySource(PropertySource<?> propertySource) {
	String name = propertySource.getName();
	MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources();
	if (propertySources.contains(name) && this.propertySourceNames.contains(name)) {
		// We've already added a version, we need to extend it
		PropertySource<?> existing = propertySources.get(name);
		PropertySource<?> newSource = (propertySource instanceof ResourcePropertySource ?
				((ResourcePropertySource) propertySource).withResourceName() : propertySource);
		if (existing instanceof CompositePropertySource) {
			((CompositePropertySource) existing).addFirstPropertySource(newSource);
		}
		else {
			if (existing instanceof ResourcePropertySource) {
				existing = ((ResourcePropertySource) existing).withResourceName();
			}
			CompositePropertySource composite = new CompositePropertySource(name);
			composite.addPropertySource(newSource);
			composite.addPropertySource(existing);
			propertySources.replace(name, composite);
		}
	}
	else {
		if (this.propertySourceNames.isEmpty()) {
			propertySources.addLast(propertySource);
		}
		else {
			String firstProcessed = this.propertySourceNames.get(this.propertySourceNames.size() - 1);
			propertySources.addBefore(firstProcessed, propertySource);
		}
	}
	this.propertySourceNames.add(name);
}
 
@Test
public void orderingIsCorrect() {
	MutablePropertySources propertySources = environment.getPropertySources();
	PropertySource<?> test1 = propertySources
			.get("bootstrapProperties-testBootstrap1");
	PropertySource<?> test2 = propertySources
			.get("bootstrapProperties-testBootstrap2");
	PropertySource<?> test3 = propertySources
			.get("bootstrapProperties-testBootstrap3");
	int index1 = propertySources.precedenceOf(test1);
	int index2 = propertySources.precedenceOf(test2);
	int index3 = propertySources.precedenceOf(test3);
	assertThat(index1).as("source1 index not less then source2").isLessThan(index2);
	assertThat(index2).as("source2 index not less then source3").isLessThan(index3);
}
 
源代码12 项目: apollo   文件: PropertySourcesProcessor.java
private void ensureBootstrapPropertyPrecedence(ConfigurableEnvironment environment) {
  MutablePropertySources propertySources = environment.getPropertySources();

  PropertySource<?> bootstrapPropertySource = propertySources
      .get(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME);

  // not exists or already in the first place
  if (bootstrapPropertySource == null || propertySources.precedenceOf(bootstrapPropertySource) == 0) {
    return;
  }

  propertySources.remove(PropertySourcesConstants.APOLLO_BOOTSTRAP_PROPERTY_SOURCE_NAME);
  propertySources.addFirst(bootstrapPropertySource);
}
 
private void addPropertySource(ResourcePropertySource propertySource) {
	String name = propertySource.getName();
	MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources();
	if (propertySources.contains(name) && this.propertySourceNames.contains(name)) {
		// We've already added a version, we need to extend it
		PropertySource<?> existing = propertySources.get(name);
		if (existing instanceof CompositePropertySource) {
			((CompositePropertySource) existing).addFirstPropertySource(propertySource.withResourceName());
		}
		else {
			if (existing instanceof ResourcePropertySource) {
				existing = ((ResourcePropertySource) existing).withResourceName();
			}
			CompositePropertySource composite = new CompositePropertySource(name);
			composite.addPropertySource(propertySource.withResourceName());
			composite.addPropertySource(existing);
			propertySources.replace(name, composite);
		}
	}
	else {
		if (this.propertySourceNames.isEmpty()) {
			propertySources.addLast(propertySource);
		}
		else {
			String firstProcessed = this.propertySourceNames.get(this.propertySourceNames.size() - 1);
			propertySources.addBefore(firstProcessed, propertySource);
		}
	}
	this.propertySourceNames.add(name);
}
 
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
	ConfigurableEnvironment environment = applicationContext.getEnvironment();
	MutablePropertySources propertySources = environment.getPropertySources();

	String[] defaultKeys = {"password", "secret", "key", "token", ".*credentials.*", "vcap_services"};
	Set<String> propertiesToSanitize = Stream.of(defaultKeys)
											 .collect(Collectors.toSet());

	PropertySource<?> bootstrapProperties = propertySources.get(BOOTSTRAP_PROPERTY_SOURCE_NAME);
	Set<PropertySource<?>> bootstrapNestedPropertySources = new HashSet<>();
	Set<PropertySource<?>> configServiceNestedPropertySources = new HashSet<>();

	if (bootstrapProperties != null && bootstrapProperties instanceof CompositePropertySource) {
		bootstrapNestedPropertySources.addAll(((CompositePropertySource) bootstrapProperties).getPropertySources());
	}
	for (PropertySource<?> nestedProperty : bootstrapNestedPropertySources) {
		if (nestedProperty.getName().equals(CONFIG_SERVICE_PROPERTY_SOURCE_NAME)) {
			configServiceNestedPropertySources.addAll(((CompositePropertySource) nestedProperty).getPropertySources());
		}
	}

	Stream<String> vaultKeyNameStream =
			configServiceNestedPropertySources.stream()
											  .filter(ps -> ps instanceof EnumerablePropertySource)
											  .filter(ps -> ps.getName().startsWith(VAULT_PROPERTY_PATTERN) || ps.getName().startsWith(CREDHUB_PROPERTY_PATTERN))
											  .map(ps -> ((EnumerablePropertySource) ps).getPropertyNames())
											  .flatMap(Arrays::<String>stream);

	propertiesToSanitize.addAll(vaultKeyNameStream.collect(Collectors.toSet()));

	PropertiesPropertySource envKeysToSanitize =
			new PropertiesPropertySource(
					SANITIZE_ENV_KEY, mergeClientProperties(propertySources, propertiesToSanitize));

	environment.getPropertySources().addFirst(envKeysToSanitize);
	applicationContext.setEnvironment(environment);

}
 
源代码15 项目: lams   文件: WebApplicationContextUtils.java
/**
 * Replace {@code Servlet}-based {@link StubPropertySource stub property sources} with
 * actual instances populated with the given {@code servletContext} and
 * {@code servletConfig} objects.
 * <p>This method is idempotent with respect to the fact it may be called any number
 * of times but will perform replacement of stub property sources with their
 * corresponding actual property sources once and only once.
 * @param propertySources the {@link MutablePropertySources} to initialize (must not
 * be {@code null})
 * @param servletContext the current {@link ServletContext} (ignored if {@code null}
 * or if the {@link StandardServletEnvironment#SERVLET_CONTEXT_PROPERTY_SOURCE_NAME
 * servlet context property source} has already been initialized)
 * @param servletConfig the current {@link ServletConfig} (ignored if {@code null}
 * or if the {@link StandardServletEnvironment#SERVLET_CONFIG_PROPERTY_SOURCE_NAME
 * servlet config property source} has already been initialized)
 * @see org.springframework.core.env.PropertySource.StubPropertySource
 * @see org.springframework.core.env.ConfigurableEnvironment#getPropertySources()
 */
public static void initServletPropertySources(
		MutablePropertySources propertySources, ServletContext servletContext, ServletConfig servletConfig) {

	Assert.notNull(propertySources, "'propertySources' must not be null");
	if (servletContext != null && propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME) &&
			propertySources.get(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME) instanceof StubPropertySource) {
		propertySources.replace(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME,
				new ServletContextPropertySource(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME, servletContext));
	}
	if (servletConfig != null && propertySources.contains(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME) &&
			propertySources.get(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME) instanceof StubPropertySource) {
		propertySources.replace(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME,
				new ServletConfigPropertySource(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME, servletConfig));
	}
}
 
@Test
public void configuresSecurityContext() {

	ConfigurableEnvironment mockEnvironment = mock(ConfigurableEnvironment.class);

	Properties vcapProperties = new Properties();

	vcapProperties.setProperty("vcap.application.name", "TestApp");
	vcapProperties.setProperty("vcap.application.uris", "test-app.apps.cloud.skullbox.com");
	vcapProperties.setProperty("vcap.services.test-pcc.credentials.locators", "boombox[10334],skullbox[10334]");
	vcapProperties.setProperty("vcap.services.test-pcc.credentials.urls.gfsh", "https://cloud.skullbox.com:8080/gemfire/v1");
	vcapProperties.setProperty("vcap.services.test-pcc.credentials.urls.pulse", "https://cloud.skullbox.com:8080/pulse");
	vcapProperties.setProperty("vcap.services.test-pcc.credentials.users[0].username", "Abuser");
	vcapProperties.setProperty("vcap.services.test-pcc.credentials.users[0].password", "[email protected]");
	vcapProperties.setProperty("vcap.services.test-pcc.credentials.users[0].roles", "cluster_developer");
	vcapProperties.setProperty("vcap.services.test-pcc.credentials.users[1].username", "Master");
	vcapProperties.setProperty("vcap.services.test-pcc.credentials.users[1].password", "[email protected]$$w0rd");
	vcapProperties.setProperty("vcap.services.test-pcc.credentials.users[1].roles", "cluster_operator");
	vcapProperties.setProperty("vcap.services.test-pcc.name", "test-pcc");
	vcapProperties.setProperty("vcap.services.test-pcc.tags", "gemfire,cloudcache,test,geode,pivotal");

	PropertySource vcapPropertySource = new PropertiesPropertySource("vcap", vcapProperties);

	MutablePropertySources propertySources = new MutablePropertySources();

	propertySources.addFirst(vcapPropertySource);

	when(mockEnvironment.getPropertySources()).thenReturn(propertySources);

	AutoConfiguredCloudSecurityEnvironmentPostProcessor environmentPostProcessor =
		spy(new AutoConfiguredCloudSecurityEnvironmentPostProcessor());

	environmentPostProcessor.configureSecurityContext(mockEnvironment);

	verify(mockEnvironment, times(2)).getPropertySources();

	assertThat(propertySources.contains("boot.data.gemfire.cloudcache")).isTrue();

	PropertySource propertySource = propertySources.get("boot.data.gemfire.cloudcache");

	assertThat(propertySource).isNotNull();
	assertThat(propertySource.getName()).isEqualTo("boot.data.gemfire.cloudcache");
	assertThat(propertySource.getProperty("spring.data.gemfire.security.username")).isEqualTo("Master");
	assertThat(propertySource.getProperty("spring.data.gemfire.security.password")).isEqualTo("[email protected]$$w0rd");
	assertThat(propertySource.containsProperty("spring.data.gemfire.security.ssl.use-default-context")).isFalse();
	assertThat(propertySource.getProperty("spring.data.gemfire.pool.locators"))
		.isEqualTo("boombox[10334],skullbox[10334]");
	assertThat(propertySource.getProperty("spring.data.gemfire.management.use-http")).isEqualTo("true");
	assertThat(propertySource.getProperty("spring.data.gemfire.management.require-https")).isEqualTo("true");
	assertThat(propertySource.getProperty("spring.data.gemfire.management.http.host")).isEqualTo("cloud.skullbox.com");
	assertThat(propertySource.getProperty("spring.data.gemfire.management.http.port")).isEqualTo("8080");
}
 
@Test
public void configuresSecurityContextWithLocatorsOnly() {

	ConfigurableEnvironment mockEnvironment = mock(ConfigurableEnvironment.class);

	when(mockEnvironment.containsProperty("spring.data.gemfire.security.username")).thenReturn(true);
	when(mockEnvironment.containsProperty("spring.data.gemfire.security.password")).thenReturn(true);

	Properties vcapProperties = new Properties();

	vcapProperties.setProperty("vcap.application.name", "TestApp");
	vcapProperties.setProperty("vcap.application.uris", "test-app.apps.cloud.skullbox.com");
	vcapProperties.setProperty("vcap.services.test-pcc.credentials.locators", "boombox[10334],skullbox[10334]");
	vcapProperties.setProperty("vcap.services.test-pcc.credentials.urls.pulse", "https://cloud.skullbox.com:8080/pulse");
	vcapProperties.setProperty("vcap.services.test-pcc.credentials.users[0].username", "Abuser");
	vcapProperties.setProperty("vcap.services.test-pcc.credentials.users[0].password", "[email protected]");
	vcapProperties.setProperty("vcap.services.test-pcc.credentials.users[0].roles", "cluster_developer");
	vcapProperties.setProperty("vcap.services.test-pcc.credentials.users[1].username", "Master");
	vcapProperties.setProperty("vcap.services.test-pcc.credentials.users[1].password", "[email protected]$$w0rd");
	vcapProperties.setProperty("vcap.services.test-pcc.credentials.users[1].roles", "cluster_operator");
	vcapProperties.setProperty("vcap.services.test-pcc.tags", "gemfire,cloudcache,test");

	PropertySource vcapPropertySource = new PropertiesPropertySource("vcap", vcapProperties);

	MutablePropertySources propertySources = new MutablePropertySources();

	propertySources.addFirst(vcapPropertySource);

	when(mockEnvironment.getPropertySources()).thenReturn(propertySources);

	AutoConfiguredCloudSecurityEnvironmentPostProcessor environmentPostProcessor =
		spy(new AutoConfiguredCloudSecurityEnvironmentPostProcessor());

	environmentPostProcessor.configureSecurityContext(mockEnvironment);

	verify(mockEnvironment, times(2)).getPropertySources();

	assertThat(propertySources.contains("boot.data.gemfire.cloudcache")).isTrue();

	PropertySource propertySource = propertySources.get("boot.data.gemfire.cloudcache");

	assertThat(propertySource).isNotNull();
	assertThat(propertySource.getName()).isEqualTo("boot.data.gemfire.cloudcache");
	assertThat(propertySource.containsProperty("spring.data.gemfire.security.username")).isFalse();
	assertThat(propertySource.containsProperty("spring.data.gemfire.security.password")).isFalse();
	assertThat(propertySource.containsProperty("spring.data.gemfire.security.ssl.use-default-context")).isFalse();
	assertThat(propertySource.getProperty("spring.data.gemfire.pool.locators"))
		.isEqualTo("boombox[10334],skullbox[10334]");
	assertThat(propertySource.containsProperty("spring.data.gemfire.management.use-http")).isFalse();
	assertThat(propertySource.containsProperty("spring.data.gemfire.management.require-https")).isFalse();
	assertThat(propertySource.containsProperty("spring.data.gemfire.management.http.host")).isFalse();
	assertThat(propertySource.containsProperty("spring.data.gemfire.management.http.port")).isFalse();
}
 
/**
 * Replace {@code Servlet}-based {@link StubPropertySource stub property sources} with
 * actual instances populated with the given {@code servletContext} and
 * {@code servletConfig} objects.
 * <p>This method is idempotent with respect to the fact it may be called any number
 * of times but will perform replacement of stub property sources with their
 * corresponding actual property sources once and only once.
 * @param propertySources the {@link MutablePropertySources} to initialize (must not
 * be {@code null})
 * @param servletContext the current {@link ServletContext} (ignored if {@code null}
 * or if the {@link StandardServletEnvironment#SERVLET_CONTEXT_PROPERTY_SOURCE_NAME
 * servlet context property source} has already been initialized)
 * @param servletConfig the current {@link ServletConfig} (ignored if {@code null}
 * or if the {@link StandardServletEnvironment#SERVLET_CONFIG_PROPERTY_SOURCE_NAME
 * servlet config property source} has already been initialized)
 * @see org.springframework.core.env.PropertySource.StubPropertySource
 * @see org.springframework.core.env.ConfigurableEnvironment#getPropertySources()
 */
public static void initServletPropertySources(
		MutablePropertySources propertySources, ServletContext servletContext, ServletConfig servletConfig) {

	Assert.notNull(propertySources, "propertySources must not be null");
	if (servletContext != null && propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME) &&
			propertySources.get(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME) instanceof StubPropertySource) {
		propertySources.replace(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME,
				new ServletContextPropertySource(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME, servletContext));
	}
	if (servletConfig != null && propertySources.contains(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME) &&
			propertySources.get(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME) instanceof StubPropertySource) {
		propertySources.replace(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME,
				new ServletConfigPropertySource(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME, servletConfig));
	}
}
 
/**
 * Replace {@code Servlet}-based {@link StubPropertySource stub property sources} with
 * actual instances populated with the given {@code servletContext} and
 * {@code servletConfig} objects.
 * <p>This method is idempotent with respect to the fact it may be called any number
 * of times but will perform replacement of stub property sources with their
 * corresponding actual property sources once and only once.
 * @param sources the {@link MutablePropertySources} to initialize (must not
 * be {@code null})
 * @param servletContext the current {@link ServletContext} (ignored if {@code null}
 * or if the {@link StandardServletEnvironment#SERVLET_CONTEXT_PROPERTY_SOURCE_NAME
 * servlet context property source} has already been initialized)
 * @param servletConfig the current {@link ServletConfig} (ignored if {@code null}
 * or if the {@link StandardServletEnvironment#SERVLET_CONFIG_PROPERTY_SOURCE_NAME
 * servlet config property source} has already been initialized)
 * @see org.springframework.core.env.PropertySource.StubPropertySource
 * @see org.springframework.core.env.ConfigurableEnvironment#getPropertySources()
 */
public static void initServletPropertySources(MutablePropertySources sources,
		@Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) {

	Assert.notNull(sources, "'propertySources' must not be null");
	String name = StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME;
	if (servletContext != null && sources.contains(name) && sources.get(name) instanceof StubPropertySource) {
		sources.replace(name, new ServletContextPropertySource(name, servletContext));
	}
	name = StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME;
	if (servletConfig != null && sources.contains(name) && sources.get(name) instanceof StubPropertySource) {
		sources.replace(name, new ServletConfigPropertySource(name, servletConfig));
	}
}
 
/**
 * Replace {@code Servlet}-based {@link StubPropertySource stub property sources} with
 * actual instances populated with the given {@code servletContext} and
 * {@code servletConfig} objects.
 * <p>This method is idempotent with respect to the fact it may be called any number
 * of times but will perform replacement of stub property sources with their
 * corresponding actual property sources once and only once.
 * @param sources the {@link MutablePropertySources} to initialize (must not
 * be {@code null})
 * @param servletContext the current {@link ServletContext} (ignored if {@code null}
 * or if the {@link StandardServletEnvironment#SERVLET_CONTEXT_PROPERTY_SOURCE_NAME
 * servlet context property source} has already been initialized)
 * @param servletConfig the current {@link ServletConfig} (ignored if {@code null}
 * or if the {@link StandardServletEnvironment#SERVLET_CONFIG_PROPERTY_SOURCE_NAME
 * servlet config property source} has already been initialized)
 * @see org.springframework.core.env.PropertySource.StubPropertySource
 * @see org.springframework.core.env.ConfigurableEnvironment#getPropertySources()
 */
public static void initServletPropertySources(MutablePropertySources sources,
		@Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) {

	Assert.notNull(sources, "'propertySources' must not be null");
	String name = StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME;
	if (servletContext != null && sources.contains(name) && sources.get(name) instanceof StubPropertySource) {
		sources.replace(name, new ServletContextPropertySource(name, servletContext));
	}
	name = StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME;
	if (servletConfig != null && sources.contains(name) && sources.get(name) instanceof StubPropertySource) {
		sources.replace(name, new ServletConfigPropertySource(name, servletConfig));
	}
}