类org.springframework.core.env.PropertySources源码实例Demo

下面列出了怎么用org.springframework.core.env.PropertySources的API类实例代码及写法,或者点击链接到github查看源代码。

public PropertySources getPropertySources() {
	MutablePropertySources sources = extractEnvironmentPropertySources();
	if (sources != null) {
		return sources;
	}
	throw new IllegalStateException("Unable to obtain PropertySources from "
			+ "PropertySourcesPlaceholderConfigurer or Environment");
}
 
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));
}
 
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");
}
 
/**
 * 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);
}
 
/**
 * 获取配置
 *
 * @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();
}
 
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");
}
 
源代码7 项目: config-toolkit   文件: ConfigGroupSourceFactory.java
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;
}
 
源代码8 项目: lemon   文件: ConfigPropertySourcesFactoryBean.java
@Override
public PropertySources getObject() throws Exception {
    if (propertySources == null) {
        this.init();
    }

    return propertySources;
}
 
private static Stream<PropertySource<?>> streamPropertySources(
        PropertySources sources) {
    return StreamSupport.stream(sources.spliterator(), false).flatMap(ConfigurationPropertySources::flatten)
            .filter(ConfigurationPropertySources::isIncluded);
}
 
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();
}
 
private static Binder getBinder(PropertySources propertySources, DefaultListableBeanFactory beanFactory) {
    return new Binder(getConfigurationPropertySources(propertySources),
                      getPropertySourcesPlaceholdersResolver(propertySources), getConversionService(),
                      getPropertyEditorInitializer(beanFactory));
}
 
private static Iterable<ConfigurationPropertySource> getConfigurationPropertySources(PropertySources propertySources) {
    return ConfigurationPropertySources.from(propertySources);
}
 
private static PropertySourcesPlaceholdersResolver getPropertySourcesPlaceholdersResolver(PropertySources propertySources) {
    return new PropertySourcesPlaceholdersResolver(propertySources);
}
 
public void setPropertySources(PropertySources propertySources) {
	this.propertySources = new MutablePropertySources(propertySources);
}
 
public PropertySources getAppliedPropertySources() throws IllegalStateException {
	Assert.state(this.appliedPropertySources != null, "PropertySources have not get been applied");
	return this.appliedPropertySources;
}
 
public Map<String, Object> decrypt(PropertySources propertySources) {
	Map<String, Object> properties = merge(propertySources);
	decrypt(properties);
	return properties;
}
 
源代码17 项目: lemon   文件: ConfigPropertySourcesFactoryBean.java
@Override
public Class<?> getObjectType() {
    return PropertySources.class;
}
 
/**
 * 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);
}
 
/**
 * 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;
}
 
/**
 * 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);
}
 
/**
 * 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;
}
 
/**
 * 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);
}
 
/**
 * 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);
}
 
/**
 * 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;
}
 
/**
 * 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);
}
 
/**
 * 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;
}