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

infra.beans.factory.config.YamlMapFactoryBean Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2017 - 2024 the original author or authors.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see [https://www.gnu.org/licenses/]
 */
package infra.beans.factory.config;

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

import infra.beans.factory.FactoryBean;
import infra.beans.factory.InitializingBean;
import infra.core.YamlProcessor;
import infra.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 2.0 or higher * * @author Dave Syer * @author Juergen Hoeller * @author Harry Yang * @since 4.0 2021/11/30 14:11 */ 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(MatchCallback) */ protected LinkedHashMap createMap() { LinkedHashMap result = new LinkedHashMap<>(); process((properties, map) -> merge(result, map)); return result; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy