Java Code Examples for org.springframework.core.env.PropertySources

The following examples show how to use org.springframework.core.env.PropertySources. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
public PropertySources getPropertySources() {
	MutablePropertySources sources = extractEnvironmentPropertySources();
	if (sources != null) {
		return sources;
	}
	throw new IllegalStateException("Unable to obtain PropertySources from "
			+ "PropertySourcesPlaceholderConfigurer or Environment");
}
 
Example 2
public FunctionalConfigurationPropertiesBinder(ConfigurableApplicationContext context) {
	PropertySources propertySources = new FunctionalPropertySourcesDeducer(context).getPropertySources();
	this.context = context;
	this.binder = new Binder(ConfigurationPropertySources.from(propertySources),
       				new PropertySourcesPlaceholdersResolver(propertySources),
       				null,
			(registry) -> context.getBeanFactory().copyRegisteredEditorsTo(registry));
}
 
Example 3
Source Project: spring-cloud-gray   Source File: PropertySourcesDeducer.java    License: Apache License 2.0 5 votes vote down vote up
public PropertySources getPropertySources() {
    PropertySourcesPlaceholderConfigurer configurer = getSinglePropertySourcesPlaceholderConfigurer();
    if (configurer != null) {
        return configurer.getAppliedPropertySources();
    }
    MutablePropertySources sources = extractEnvironmentPropertySources();
    if (sources != null) {
        return sources;
    }
    throw new IllegalStateException("Unable to obtain PropertySources from "
            + "PropertySourcesPlaceholderConfigurer or Environment");
}
 
Example 4
/**
 * Get prefixed {@link Properties}
 *
 * @param propertySources  {@link PropertySources}
 * @param propertyResolver {@link PropertyResolver} to resolve the placeholder if present
 * @param prefix           the prefix of property name
 * @return Map
 * @see Properties
 * @since 1.0.3
 */
public static Map<String, Object> getSubProperties(PropertySources propertySources, PropertyResolver propertyResolver, String prefix) {

    Map<String, Object> subProperties = new LinkedHashMap<String, Object>();

    String normalizedPrefix = normalizePrefix(prefix);

    Iterator<PropertySource<?>> iterator = propertySources.iterator();

    while (iterator.hasNext()) {
        PropertySource<?> source = iterator.next();
        if (source instanceof EnumerablePropertySource) {
            for (String name : ((EnumerablePropertySource<?>) source).getPropertyNames()) {
                if (!subProperties.containsKey(name) && name.startsWith(normalizedPrefix)) {
                    String subName = name.substring(normalizedPrefix.length());
                    if (!subProperties.containsKey(subName)) { // take first one
                        Object value = source.getProperty(name);
                        if (value instanceof String) {
                            // Resolve placeholder
                            value = propertyResolver.resolvePlaceholders((String) value);
                        }
                        subProperties.put(subName, value);
                    }
                }
            }
        }
    }

    return unmodifiableMap(subProperties);
}
 
Example 5
/**
 * 获取配置
 *
 * @param beanFactory beanFactory
 * @param prefix 前缀
 * @return
 */
public static MangoConfig getMangoConfig(DefaultListableBeanFactory beanFactory, String prefix) {
    MangoConfig config = new MangoConfig();
    Bindable<?> target = Bindable.ofInstance(config);
    PropertySources propertySources = getPropertySources(beanFactory);
    BindHandler bindHandler = getBindHandler();
    BindResult configBindResult = getBinder(propertySources, beanFactory).bind(prefix, target, bindHandler);
    return (MangoConfig) configBindResult.get();
}
 
Example 6
public static PropertySources getPropertySources(DefaultListableBeanFactory beanFactory) {
    PropertySourcesPlaceholderConfigurer configurer = getSinglePropertySourcesPlaceholderConfigurer(beanFactory);
    if (configurer != null) {
        return configurer.getAppliedPropertySources();
    }
    MutablePropertySources sources = extractEnvironmentPropertySources(beanFactory);
    if (sources != null) {
        return sources;
    }
    throw new IllegalStateException("Unable to obtain PropertySources from "
                                            + "PropertySourcesPlaceholderConfigurer or Environment");
}
 
Example 7
Source Project: config-toolkit   Source File: ConfigGroupSourceFactory.java    License: Apache License 2.0 5 votes vote down vote up
public static PropertySources create(ConfigGroup... configGroups) {
	final MutablePropertySources sources = new MutablePropertySources();
	for (ConfigGroup configGroup : configGroups) {
		if (configGroup.isEnumerable()) {
			sources.addLast(new ConfigGroupEnumerableResource(configGroup));
		} else {
			sources.addLast(new ConfigGroupResource(configGroup));
		}
	}
	return sources;
}
 
Example 8
Source Project: lemon   Source File: ConfigPropertySourcesFactoryBean.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public PropertySources getObject() throws Exception {
    if (propertySources == null) {
        this.init();
    }

    return propertySources;
}
 
Example 9
private static Stream<PropertySource<?>> streamPropertySources(
        PropertySources sources) {
    return StreamSupport.stream(sources.spliterator(), false).flatMap(ConfigurationPropertySources::flatten)
            .filter(ConfigurationPropertySources::isIncluded);
}
 
Example 10
private static PropertySources getSources(Environment environment) {
    Assert.notNull(environment, "Environment must not be null");
    Assert.isInstanceOf(ConfigurableEnvironment.class, environment,
            "Environment must be a ConfigurableEnvironment");
    return ((ConfigurableEnvironment) environment).getPropertySources();
}
 
Example 11
private static Binder getBinder(PropertySources propertySources, DefaultListableBeanFactory beanFactory) {
    return new Binder(getConfigurationPropertySources(propertySources),
                      getPropertySourcesPlaceholdersResolver(propertySources), getConversionService(),
                      getPropertyEditorInitializer(beanFactory));
}
 
Example 12
private static Iterable<ConfigurationPropertySource> getConfigurationPropertySources(PropertySources propertySources) {
    return ConfigurationPropertySources.from(propertySources);
}
 
Example 13
private static PropertySourcesPlaceholdersResolver getPropertySourcesPlaceholdersResolver(PropertySources propertySources) {
    return new PropertySourcesPlaceholdersResolver(propertySources);
}
 
Example 14
public void setPropertySources(PropertySources propertySources) {
	this.propertySources = new MutablePropertySources(propertySources);
}
 
Example 15
public PropertySources getAppliedPropertySources() throws IllegalStateException {
	Assert.state(this.appliedPropertySources != null, "PropertySources have not get been applied");
	return this.appliedPropertySources;
}
 
Example 16
public Map<String, Object> decrypt(PropertySources propertySources) {
	Map<String, Object> properties = merge(propertySources);
	decrypt(properties);
	return properties;
}
 
Example 17
Source Project: lemon   Source File: ConfigPropertySourcesFactoryBean.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Class<?> getObjectType() {
    return PropertySources.class;
}
 
Example 18
/**
 * Customize the set of {@link PropertySources} to be used by this configurer.
 * <p>Setting this property indicates that environment property sources and
 * local properties should be ignored.
 * @see #postProcessBeanFactory
 */
public void setPropertySources(PropertySources propertySources) {
	this.propertySources = new MutablePropertySources(propertySources);
}
 
Example 19
/**
 * Return the property sources that were actually applied during
 * {@link #postProcessBeanFactory(ConfigurableListableBeanFactory) post-processing}.
 * @return the property sources that were applied
 * @throws IllegalStateException if the property sources have not yet been applied
 * @since 4.0
 */
public PropertySources getAppliedPropertySources() throws IllegalStateException {
	Assert.state(this.appliedPropertySources != null, "PropertySources have not yet been applied");
	return this.appliedPropertySources;
}
 
Example 20
/**
 * Customize the set of {@link PropertySources} to be used by this configurer.
 * <p>Setting this property indicates that environment property sources and
 * local properties should be ignored.
 * @see #postProcessBeanFactory
 */
public void setPropertySources(PropertySources propertySources) {
	this.propertySources = new MutablePropertySources(propertySources);
}
 
Example 21
/**
 * Return the property sources that were actually applied during
 * {@link #postProcessBeanFactory(ConfigurableListableBeanFactory) post-processing}.
 * @return the property sources that were applied
 * @throws IllegalStateException if the property sources have not yet been applied
 * @since 4.0
 */
public PropertySources getAppliedPropertySources() throws IllegalStateException {
	Assert.state(this.appliedPropertySources != null, "PropertySources have not yet been applied");
	return this.appliedPropertySources;
}
 
Example 22
/**
 * Get prefixed {@link Properties}
 *
 * @param propertySources {@link PropertySources}
 * @param prefix          the prefix of property name
 * @return Map
 * @see Properties
 * @since 1.0.3
 */
public static Map<String, Object> getSubProperties(PropertySources propertySources, String prefix) {
    return getSubProperties(propertySources, new PropertySourcesPropertyResolver(propertySources), prefix);
}
 
Example 23
/**
 * Customize the set of {@link PropertySources} to be used by this configurer.
 * Setting this property indicates that environment property sources and local
 * properties should be ignored.
 * @see #postProcessBeanFactory
 */
public void setPropertySources(PropertySources propertySources) {
	this.propertySources = new MutablePropertySources(propertySources);
}
 
Example 24
/**
 * Returns the property sources that were actually applied during
 * {@link #postProcessBeanFactory(ConfigurableListableBeanFactory) post-processing}.
 * @return the property sources that were applied
 * @throws IllegalStateException if the property sources have not yet been applied
 * @since 4.0
 */
public PropertySources getAppliedPropertySources() throws IllegalStateException {
	Assert.state(this.appliedPropertySources != null, "PropertySources have not get been applied");
	return this.appliedPropertySources;
}
 
Example 25
/**
 * Customize the set of {@link PropertySources} to be used by this configurer.
 * Setting this property indicates that environment property sources and local
 * properties should be ignored.
 * @see #postProcessBeanFactory
 */
public void setPropertySources(PropertySources propertySources) {
	this.propertySources = new MutablePropertySources(propertySources);
}
 
Example 26
/**
 * Returns the property sources that were actually applied during
 * {@link #postProcessBeanFactory(ConfigurableListableBeanFactory) post-processing}.
 * @return the property sources that were applied
 * @throws IllegalStateException if the property sources have not yet been applied
 * @since 4.0
 */
public PropertySources getAppliedPropertySources() throws IllegalStateException {
	Assert.state(this.appliedPropertySources != null, "PropertySources have not get been applied");
	return this.appliedPropertySources;
}