下面列出了org.springframework.core.env.PropertySource#getProperty ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
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;
}
/**
* 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);
}
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;
}
@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);
}
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);
}
}
}