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

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

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 registerPort(RunningStubs runStubs) {
	MutablePropertySources propertySources = this.environment.getPropertySources();
	if (!propertySources.contains(STUBRUNNER_PREFIX)) {
		propertySources
				.addFirst(new MapPropertySource(STUBRUNNER_PREFIX, new HashMap<>()));
	}
	Map<String, Object> source = ((MapPropertySource) propertySources
			.get(STUBRUNNER_PREFIX)).getSource();
	for (Map.Entry<StubConfiguration, Integer> entry : runStubs.validNamesAndPorts()
			.entrySet()) {
		source.put(STUBRUNNER_PREFIX + "." + entry.getKey().getArtifactId() + ".port",
				entry.getValue());
		// there are projects where artifact id is the same, what differs is the group
		// id
		source.put(STUBRUNNER_PREFIX + "." + entry.getKey().getGroupId() + "."
				+ entry.getKey().getArtifactId() + ".port", entry.getValue());
	}
}
 
@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;
}
 
private void addOrReplace(MutablePropertySources environment,
		PropertySource<?> result) {
	if (environment.contains(result.getName())) {
		environment.replace(result.getName(), result);
	}
	else {
		environment.addLast(result);
	}
}
 
源代码6 项目: 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);
}
 
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);
}
 
private void registerPropertySources(Collection<? extends PropertySource<?>> propertySources,
		MutablePropertySources mutablePropertySources) {

	for (PropertySource<?> vaultPropertySource : propertySources) {

		if (mutablePropertySources.contains(vaultPropertySource.getName())) {
			continue;
		}

		mutablePropertySources.addLast(vaultPropertySource);
	}
}
 
private void defaultProperties(ConfigurableApplicationContext context) {
	MutablePropertySources sources = context.getEnvironment().getPropertySources();
	if (!sources.contains(DEFAULT_PROPERTIES)) {
		sources.addLast(
				new MapPropertySource(DEFAULT_PROPERTIES, Collections.emptyMap()));
	}
	@SuppressWarnings("unchecked")
	Map<String, Object> source = (Map<String, Object>) sources.get(DEFAULT_PROPERTIES)
			.getSource();
	Map<String, Object> map = new HashMap<>(source);
	map.put(SPRING_FUNCTIONAL_ENABLED, "true");
	map.put(SPRING_WEB_APPLICATION_TYPE, getWebApplicationType());
	sources.replace(DEFAULT_PROPERTIES,
			new MapPropertySource(DEFAULT_PROPERTIES, map));
}
 
源代码10 项目: spring-cloud-commons   文件: EnvironmentManager.java
public EnvironmentManager(ConfigurableEnvironment environment) {
	this.environment = environment;
	MutablePropertySources sources = environment.getPropertySources();
	if (sources.contains(MANAGER_PROPERTY_SOURCE)) {
		@SuppressWarnings("unchecked")
		Map<String, Object> map = (Map<String, Object>) sources
				.get(MANAGER_PROPERTY_SOURCE).getSource();
		this.map = map;
	}
}
 
private Properties mergeClientProperties(MutablePropertySources propertySources, Set<String> propertiesToSanitize) {
	Properties props = new Properties();
	if (propertySources.contains(SANITIZE_ENV_KEY)) {
		String clientProperties = Objects.requireNonNull(propertySources.get(SANITIZE_ENV_KEY)).toString();
		propertiesToSanitize.addAll(Stream.of(clientProperties.split(",")).collect(Collectors.toSet()));
	}
	props.setProperty(SANITIZE_ENV_KEY, StringUtils.arrayToCommaDelimitedString(propertiesToSanitize.toArray()));
	return props;
}
 
private Properties mergeClientProperties(MutablePropertySources propertySources, Set<String> propertiesToSanitize) {
	Properties props = new Properties();
	if (propertySources.contains(SANITIZE_ENV_KEY)) {
		String clientProperties = Objects.requireNonNull(propertySources.get(SANITIZE_ENV_KEY)).toString();
		propertiesToSanitize.addAll(Stream.of(clientProperties.split(",")).collect(Collectors.toSet()));
	}
	props.setProperty(SANITIZE_ENV_KEY, StringUtils.arrayToCommaDelimitedString(propertiesToSanitize.toArray()));
	return props;
}
 
源代码13 项目: spring-cloud-commons   文件: ContextRefresher.java
private StandardEnvironment copyEnvironment(ConfigurableEnvironment input) {
	StandardEnvironment environment = new StandardEnvironment();
	MutablePropertySources capturedPropertySources = environment.getPropertySources();
	// Only copy the default property source(s) and the profiles over from the main
	// environment (everything else should be pristine, just like it was on startup).
	for (String name : DEFAULT_PROPERTY_SOURCES) {
		if (input.getPropertySources().contains(name)) {
			if (capturedPropertySources.contains(name)) {
				capturedPropertySources.replace(name,
						input.getPropertySources().get(name));
			}
			else {
				capturedPropertySources.addLast(input.getPropertySources().get(name));
			}
		}
	}
	environment.setActiveProfiles(input.getActiveProfiles());
	environment.setDefaultProfiles(input.getDefaultProfiles());
	Map<String, Object> map = new HashMap<String, Object>();
	map.put("spring.jmx.enabled", false);
	map.put("spring.main.sources", "");
	// gh-678 without this apps with this property set to REACTIVE or SERVLET fail
	map.put("spring.main.web-application-type", "NONE");
	capturedPropertySources
			.addFirst(new MapPropertySource(REFRESH_ARGS_PROPERTY_SOURCE, map));
	return environment;
}
 
源代码14 项目: camel-spring-boot   文件: CamelAutoConfiguration.java
static CamelContext doConfigureCamelContext(ApplicationContext applicationContext,
                                            CamelContext camelContext,
                                            CamelConfigurationProperties config) throws Exception {

    camelContext.build();

    // initialize properties component eager
    PropertiesComponent pc = applicationContext.getBeanProvider(PropertiesComponent.class).getIfAvailable();
    if (pc != null) {
        pc.setCamelContext(camelContext);
        camelContext.setPropertiesComponent(pc);
    }

    final Map<String, BeanRepository> repositories = applicationContext.getBeansOfType(BeanRepository.class);
    if (!repositories.isEmpty()) {
        List<BeanRepository> reps = new ArrayList<>();
        // include default bean repository as well
        reps.add(new ApplicationContextBeanRepository(applicationContext));
        // and then any custom
        reps.addAll(repositories.values());
        // sort by ordered
        OrderComparator.sort(reps);
        // and plugin as new registry
        camelContext.adapt(ExtendedCamelContext.class).setRegistry(new DefaultRegistry(reps));
    }

    if (ObjectHelper.isNotEmpty(config.getFileConfigurations())) {
        Environment env = applicationContext.getEnvironment();
        if (env instanceof ConfigurableEnvironment) {
            MutablePropertySources sources = ((ConfigurableEnvironment) env).getPropertySources();
            if (sources != null) {
                if (!sources.contains("camel-file-configuration")) {
                    sources.addFirst(new FilePropertySource("camel-file-configuration", applicationContext, config.getFileConfigurations()));
                }
            }
        }
    }

    camelContext.adapt(ExtendedCamelContext.class).setPackageScanClassResolver(new FatJarPackageScanClassResolver());

    if (config.getRouteFilterIncludePattern() != null || config.getRouteFilterExcludePattern() != null) {
        LOG.info("Route filtering pattern: include={}, exclude={}", config.getRouteFilterIncludePattern(), config.getRouteFilterExcludePattern());
        camelContext.getExtension(Model.class).setRouteFilterPattern(config.getRouteFilterIncludePattern(), config.getRouteFilterExcludePattern());
    }

    // configure the common/default options
    DefaultConfigurationConfigurer.configure(camelContext, config);
    // lookup and configure SPI beans
    DefaultConfigurationConfigurer.afterConfigure(camelContext);
    // and call after all properties are set
    DefaultConfigurationConfigurer.afterPropertiesSet(camelContext);

    return camelContext;
}
 
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
		SpringApplication application) {
	increaseInvocationCount();
	if (CloudPlatform.CLOUD_FOUNDRY.isActive(environment)) {
		CfJdbcEnv cfJdbcEnv = new CfJdbcEnv();
		CfJdbcService cfJdbcService = null;
		try {
			cfJdbcService = cfJdbcEnv.findJdbcService();
			cfJdbcService = this.isEnabled(cfJdbcService, environment) ? cfJdbcService : null;
		} catch (Exception e) {

			List<CfJdbcService> jdbcServices = cfJdbcEnv.findJdbcServices().stream()
					.filter(service -> this.isEnabled(service, environment))
					.collect(Collectors.toList());

			if (jdbcServices.size() > 1) {
				if (invocationCount == 1) {
					DEFERRED_LOG.debug(
						"Skipping execution of CfDataSourceEnvironmentPostProcessor. "
							+ e.getMessage());
				}
				return;
			}

			cfJdbcService = jdbcServices.size() == 1 ? jdbcServices.get(0) : null;
		}
		if (cfJdbcService != null) {
			ConnectorLibraryDetector.assertNoConnectorLibrary();
			Map<String, Object> properties = new LinkedHashMap<>();
			properties.put("spring.datasource.url", cfJdbcService.getJdbcUrl());
			properties.put("spring.datasource.username", cfJdbcService.getUsername());
			properties.put("spring.datasource.password", cfJdbcService.getPassword());
			Object driverClassName = cfJdbcService.getDriverClassName();
			if (driverClassName != null) {
				properties.put("spring.datasource.driver-class-name", driverClassName);
			}

			MutablePropertySources propertySources = environment.getPropertySources();
			if (propertySources.contains(
					CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME)) {
				propertySources.addAfter(
						CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME,
						new MapPropertySource("cfenvjdbc", properties));
			}
			else {
				propertySources
						.addFirst(new MapPropertySource("cfenvjdbc", properties));
			}
			if (invocationCount == 1) {
				DEFERRED_LOG.info(
						"Setting spring.datasource properties from bound service ["
								+ cfJdbcService.getName() + "]");
			}
		}
	}
	else {
		DEFERRED_LOG.debug(
				"Not setting spring.datasource.url, not in Cloud Foundry Environment");
	}
}
 
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
	this.environment = event.getEnvironment();
	Iterable<CloudProfileProvider> cloudProfileProviders = ServiceLoader.load(CloudProfileProvider.class);

	if (ignoreFromSystemProperty()
			|| ignoreFromEnvironmentVariable()
			|| cloudProfilesAlreadySet(cloudProfileProviders)) {
		return;
	}

	boolean addedCloudProfile = false;
	boolean addedKubernetesProfile = false;
	for (CloudProfileProvider cloudProfileProvider : cloudProfileProviders) {
		if (cloudProfileProvider.isCloudPlatform(environment)) {
			String profileToAdd = cloudProfileProvider.getCloudProfile();
			if (!Arrays.asList(environment.getActiveProfiles()).contains(profileToAdd)) {
				if (profileToAdd.equals(KubernetesCloudProfileProvider.PROFILE)) {
					addedKubernetesProfile = true;
				}
				environment.addActiveProfile(profileToAdd);
				addedCloudProfile = true;
			}
		}
	}
	if (!addedKubernetesProfile) {
		Map<String, Object> properties = new LinkedHashMap<>();
		properties.put("spring.cloud.kubernetes.enabled", false);
		logger.info("Setting property 'spring.cloud.kubernetes.enabled' to false.");
		MutablePropertySources propertySources = environment.getPropertySources();
		if (propertySources != null) {
			if (propertySources.contains(
					CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME)) {
				propertySources.addAfter(
						CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME,
						new MapPropertySource("skipperProfileApplicationListener", properties));
			}
			else {
				propertySources
						.addFirst(new MapPropertySource("skipperProfileApplicationListener", properties));
			}
		}
	}
	if (!addedCloudProfile) {
		environment.addActiveProfile("local");
	}
}
 
源代码17 项目: 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));
	}
}
 
/**
 * Replace {@code Servlet}- and {@code Portlet}-based {@link
 * org.springframework.core.env.PropertySource.StubPropertySource stub property
 * sources} with actual instances populated with the given {@code servletContext},
 * {@code portletContext} and {@code portletConfig} 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 org.springframework.web.context.support.StandardServletEnvironment#SERVLET_CONTEXT_PROPERTY_SOURCE_NAME
 * servlet context property source} has already been initialized)
 * @param portletContext the current {@link PortletContext} (ignored if {@code null}
 * or if the {@link StandardPortletEnvironment#PORTLET_CONTEXT_PROPERTY_SOURCE_NAME
 * portlet context property source} has already been initialized)
 * @param portletConfig the current {@link PortletConfig} (ignored if {@code null}
 * or if the {@link StandardPortletEnvironment#PORTLET_CONFIG_PROPERTY_SOURCE_NAME
 * portlet config property source} has already been initialized)
 * @see org.springframework.core.env.PropertySource.StubPropertySource
 * @see org.springframework.web.context.support.WebApplicationContextUtils#initServletPropertySources(MutablePropertySources, ServletContext)
 * @see org.springframework.core.env.ConfigurableEnvironment#getPropertySources()
 */
public static void initPortletPropertySources(MutablePropertySources propertySources, ServletContext servletContext,
		PortletContext portletContext, PortletConfig portletConfig) {

	Assert.notNull(propertySources, "propertySources must not be null");
	WebApplicationContextUtils.initServletPropertySources(propertySources, servletContext);

	if (portletContext != null && propertySources.contains(StandardPortletEnvironment.PORTLET_CONTEXT_PROPERTY_SOURCE_NAME)) {
		propertySources.replace(StandardPortletEnvironment.PORTLET_CONTEXT_PROPERTY_SOURCE_NAME,
				new PortletContextPropertySource(StandardPortletEnvironment.PORTLET_CONTEXT_PROPERTY_SOURCE_NAME, portletContext));
	}
	if (portletConfig != null && propertySources.contains(StandardPortletEnvironment.PORTLET_CONFIG_PROPERTY_SOURCE_NAME)) {
		propertySources.replace(StandardPortletEnvironment.PORTLET_CONFIG_PROPERTY_SOURCE_NAME,
				new PortletConfigPropertySource(StandardPortletEnvironment.PORTLET_CONFIG_PROPERTY_SOURCE_NAME, portletConfig));
	}
}
 
/**
 * 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));
	}
}