All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.beans.factory.config.YamlMapFactoryBean Maven / Gradle / Ivy

There is a newer version: 6.1.6
Show newest version
/*
 * Copyright 2002-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.beans.factory.config;

import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.lang.Nullable;

/**
 * Factory for a {@code Map} that reads from a YAML source, preserving the
 * YAML-declared value types and their structure.
 *
 * 

YAML is a nice human-readable format for configuration, and it has some * useful hierarchical properties. It's more or less a superset of JSON, so it * has a lot of similar features. * *

If multiple resources are provided the later ones will override entries in * the earlier ones hierarchically; that is, all entries with the same nested key * of type {@code Map} at any depth are merged. For example: * *

 * foo:
 *   bar:
 *    one: two
 * three: four
 * 
* * plus (later in the list) * *
 * foo:
 *   bar:
 *    one: 2
 * five: six
 * 
* * results in an effective input of * *
 * foo:
 *   bar:
 *    one: 2
 * three: four
 * five: six
 * 
* * Note that the value of "foo" in the first document is not simply replaced * with the value in the second, but its nested values are merged. * *

Requires SnakeYAML 1.18 or higher, as of Spring Framework 5.0.6. * * @author Dave Syer * @author Juergen Hoeller * @since 4.1 */ public class YamlMapFactoryBean extends YamlProcessor implements FactoryBean>, InitializingBean { private boolean singleton = true; @Nullable private Map map; /** * Set if a singleton should be created, or a new object on each request * otherwise. Default is {@code true} (a singleton). */ public void setSingleton(boolean singleton) { this.singleton = singleton; } @Override public boolean isSingleton() { return this.singleton; } @Override public void afterPropertiesSet() { if (isSingleton()) { this.map = createMap(); } } @Override @Nullable public Map getObject() { return (this.map != null ? this.map : createMap()); } @Override public Class getObjectType() { return Map.class; } /** * Template method that subclasses may override to construct the object * returned by this factory. *

Invoked lazily the first time {@link #getObject()} is invoked in * case of a shared singleton; else, on each {@link #getObject()} call. *

The default implementation returns the merged {@code Map} instance. * @return the object returned by this factory * @see #process(java.util.Map, MatchCallback) */ protected Map createMap() { Map result = new LinkedHashMap<>(); process((properties, map) -> merge(result, map)); return result; } @SuppressWarnings({"unchecked", "rawtypes"}) private void merge(Map output, Map map) { map.forEach((key, value) -> { Object existing = output.get(key); if (value instanceof Map && existing instanceof Map) { // Inner cast required by Eclipse IDE. Map result = new LinkedHashMap<>((Map) existing); merge(result, (Map) value); output.put(key, result); } else { output.put(key, value); } }); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy