org.springframework.core.env.PropertySources Java Examples

The following examples show how to use org.springframework.core.env.PropertySources. 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
Source File: ConfigPropertySourcesFactoryBean.java    From lemon with Apache License 2.0 5 votes vote down vote up
@Override
public PropertySources getObject() throws Exception {
    if (propertySources == null) {
        this.init();
    }

    return propertySources;
}
 
Example #2
Source File: ConfigGroupSourceFactory.java    From config-toolkit with 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 #3
Source File: FunctionalPropertySourcesDeducer.java    From spring-fu with Apache License 2.0 5 votes vote down vote up
public PropertySources getPropertySources() {
	MutablePropertySources sources = extractEnvironmentPropertySources();
	if (sources != null) {
		return sources;
	}
	throw new IllegalStateException("Unable to obtain PropertySources from "
			+ "PropertySourcesPlaceholderConfigurer or Environment");
}
 
Example #4
Source File: FunctionalConfigurationPropertiesBinder.java    From spring-fu with Apache License 2.0 5 votes vote down vote up
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 #5
Source File: PropertySourcesDeducer.java    From spring-cloud-gray with 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 #6
Source File: PropertySourcesUtils.java    From spring-context-support with Apache License 2.0 5 votes vote down vote up
/**
 * 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 #7
Source File: MangoConfigFactory.java    From mango-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
/**
 * 获取配置
 *
 * @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 #8
Source File: MangoConfigFactory.java    From mango-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
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 #9
Source File: MangoConfigFactory.java    From mango-spring-boot-starter with Apache License 2.0 4 votes vote down vote up
private static Iterable<ConfigurationPropertySource> getConfigurationPropertySources(PropertySources propertySources) {
    return ConfigurationPropertySources.from(propertySources);
}
 
Example #10
Source File: ConfigPropertySourcesFactoryBean.java    From lemon with Apache License 2.0 4 votes vote down vote up
@Override
public Class<?> getObjectType() {
    return PropertySources.class;
}
 
Example #11
Source File: EnvironmentDecryptApplicationInitializer.java    From spring-cloud-commons with Apache License 2.0 4 votes vote down vote up
public Map<String, Object> decrypt(PropertySources propertySources) {
	Map<String, Object> properties = merge(propertySources);
	decrypt(properties);
	return properties;
}
 
Example #12
Source File: ConfigurationPropertySourcesPlaceholderConfigurer.java    From java-platform with Apache License 2.0 4 votes vote down vote up
public PropertySources getAppliedPropertySources() throws IllegalStateException {
	Assert.state(this.appliedPropertySources != null, "PropertySources have not get been applied");
	return this.appliedPropertySources;
}
 
Example #13
Source File: ConfigurationPropertySourcesPlaceholderConfigurer.java    From java-platform with Apache License 2.0 4 votes vote down vote up
public void setPropertySources(PropertySources propertySources) {
	this.propertySources = new MutablePropertySources(propertySources);
}
 
Example #14
Source File: MangoConfigFactory.java    From mango-spring-boot-starter with Apache License 2.0 4 votes vote down vote up
private static PropertySourcesPlaceholdersResolver getPropertySourcesPlaceholdersResolver(PropertySources propertySources) {
    return new PropertySourcesPlaceholdersResolver(propertySources);
}
 
Example #15
Source File: MangoConfigFactory.java    From mango-spring-boot-starter with Apache License 2.0 4 votes vote down vote up
private static Binder getBinder(PropertySources propertySources, DefaultListableBeanFactory beanFactory) {
    return new Binder(getConfigurationPropertySources(propertySources),
                      getPropertySourcesPlaceholdersResolver(propertySources), getConversionService(),
                      getPropertyEditorInitializer(beanFactory));
}
 
Example #16
Source File: PropertySourcesPlaceholdersResolver.java    From spring-cloud-gray with Apache License 2.0 4 votes vote down vote up
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 #17
Source File: ConfigurationPropertySources.java    From spring-cloud-gray with Apache License 2.0 4 votes vote down vote up
private static Stream<PropertySource<?>> streamPropertySources(
        PropertySources sources) {
    return StreamSupport.stream(sources.spliterator(), false).flatMap(ConfigurationPropertySources::flatten)
            .filter(ConfigurationPropertySources::isIncluded);
}
 
Example #18
Source File: PropertySourcesPlaceholderConfigurer.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * 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
Source File: PropertySourcesPlaceholderConfigurer.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 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 #20
Source File: PropertySourcesPlaceholderConfigurer.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * 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 #21
Source File: PropertySourcesPlaceholderConfigurer.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * 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 #22
Source File: PropertySourcesPlaceholderConfigurer.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * 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 #23
Source File: PropertySourcesUtils.java    From spring-context-support with Apache License 2.0 2 votes vote down vote up
/**
 * 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 #24
Source File: PropertySourcesPlaceholderConfigurer.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * 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 #25
Source File: PropertySourcesPlaceholderConfigurer.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * 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 #26
Source File: PropertySourcesPlaceholderConfigurer.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * 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;
}