Java Code Examples for org.springframework.core.env.PropertySource#getProperty()

The following examples show how to use org.springframework.core.env.PropertySource#getProperty() . 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: EnvironmentHelper.java    From jeesuite-libs with Apache License 2.0 6 votes vote down vote up
public static Map<String, Object> getAllProperties(String prefix){
	if(environment == null)init();
	MutablePropertySources propertySources = ((ConfigurableEnvironment)environment).getPropertySources();
	
	Map<String, Object> properties = new LinkedHashMap<String, Object>();
	for (PropertySource<?> source : propertySources) {
		if(source.getName().startsWith("servlet") || source.getName().startsWith("system")){
			continue;
		}
		if (source instanceof EnumerablePropertySource) {
			for (String name : ((EnumerablePropertySource<?>) source) .getPropertyNames()) {
				boolean match = StringUtils.isEmpty(prefix);
				if(!match){
					match = name.startsWith(prefix);
				}
				if(match){						
					Object value = source.getProperty(name);
					if(value != null){
						properties.put(name, value);
					}
				}
			}
		}
	}
	return Collections.unmodifiableMap(properties);
}
 
Example 2
Source File: PropertySourcesPlaceholdersResolver.java    From spring-cloud-gray with Apache License 2.0 5 votes vote down vote up
protected String resolvePlaceholder(String placeholder) {
    if (this.sources != null) {
        for (PropertySource<?> source : this.sources) {
            Object value = source.getProperty(placeholder);
            if (value != null) {
                return String.valueOf(value);
            }
        }
    }
    return null;
}
 
Example 3
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 4
Source File: EncryptablePropertySource.java    From jasypt-spring-boot with MIT License 5 votes vote down vote up
default Object getProperty(EncryptablePropertyResolver resolver, EncryptablePropertyFilter filter, PropertySource<T> source, String name) {
    Object value = source.getProperty(name);
    if (filter.shouldInclude(source, name) && value instanceof String) {
        String stringValue = String.valueOf(value);
        return resolver.resolvePropertyValue(stringValue);
    }
    return value;
}
 
Example 5
Source File: YamlPropertySourceLoaderTest.java    From onetwo with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws Exception{
	YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
	PropertySource<?> props = loader.load("application", SpringUtils.newClassPathResource("application.yaml"), null);
	Object env = props.getProperty("spring.profiles.active");
	System.out.println("env: " + env);
	env = props.getProperty("server.port");
	System.out.println("port: " + env);
}
 
Example 6
Source File: EnvironmentDecryptApplicationInitializer.java    From spring-cloud-commons with Apache License 2.0 4 votes vote down vote up
private void merge(PropertySource<?> source, Map<String, Object> properties) {
	if (source instanceof CompositePropertySource) {

		List<PropertySource<?>> sources = new ArrayList<>(
				((CompositePropertySource) source).getPropertySources());
		Collections.reverse(sources);

		for (PropertySource<?> nested : sources) {
			merge(nested, properties);
		}

	}
	else if (source instanceof EnumerablePropertySource) {
		Map<String, Object> otherCollectionProperties = new LinkedHashMap<>();
		boolean sourceHasDecryptedCollection = false;

		EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source;
		for (String key : enumerable.getPropertyNames()) {
			Object property = source.getProperty(key);
			if (property != null) {
				String value = property.toString();
				if (value.startsWith(ENCRYPTED_PROPERTY_PREFIX)) {
					properties.put(key, value);
					if (COLLECTION_PROPERTY.matcher(key).matches()) {
						sourceHasDecryptedCollection = true;
					}
				}
				else if (COLLECTION_PROPERTY.matcher(key).matches()) {
					// put non-encrypted properties so merging of index properties
					// happens correctly
					otherCollectionProperties.put(key, value);
				}
				else {
					// override previously encrypted with non-encrypted property
					properties.remove(key);
				}
			}
		}
		// copy all indexed properties even if not encrypted
		if (sourceHasDecryptedCollection && !otherCollectionProperties.isEmpty()) {
			properties.putAll(otherCollectionProperties);
		}

	}
}