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

infra.beans.factory.config.YamlPropertiesFactoryBean 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.Properties;

import infra.beans.factory.FactoryBean;
import infra.beans.factory.InitializingBean;
import infra.core.YamlProcessor;
import infra.lang.Nullable;
import infra.util.CollectionUtils;

/**
 * Factory for {@link java.util.Properties} that reads from a YAML source,
 * exposing a flat structure of String property values.
 *
 * 

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. * *

Note: All exposed values are of type {@code String} for access through * the common {@link Properties#getProperty} method * If this is not desirable, use {@link YamlMapFactoryBean} instead. * *

The Properties created by this factory have nested paths for hierarchical * objects, so for instance this YAML * *

 * environments:
 *   dev:
 *     url: https://dev.bar.com
 *     name: Developer Setup
 *   prod:
 *     url: https://foo.bar.com
 *     name: My Cool App
 * 
* * is transformed into these properties: * *
 * environments.dev.url=https://dev.bar.com
 * environments.dev.name=Developer Setup
 * environments.prod.url=https://foo.bar.com
 * environments.prod.name=My Cool App
 * 
* * Lists are split as property keys with [] dereferencers, for * example this YAML: * *
 * servers:
 * - dev.bar.com
 * - foo.bar.com
 * 
* * becomes properties like this: * *
 * servers[0]=dev.bar.com
 * servers[1]=foo.bar.com
 * 
* *

Requires SnakeYAML 2.0 or higher * * @author Dave Syer * @author Stephane Nicoll * @author Juergen Hoeller * @author Harry Yang * @since 4.0 2021/11/30 14:11 */ public class YamlPropertiesFactoryBean extends YamlProcessor implements FactoryBean, InitializingBean { private boolean singleton = true; @Nullable private Properties properties; /** * 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.properties = createProperties(); } } @Override @Nullable public Properties getObject() { return (this.properties != null ? this.properties : createProperties()); } @Override public Class getObjectType() { return Properties.class; } /** * Template method that subclasses may override to construct the object * returned by this factory. The default implementation returns a * properties with the content of all resources. *

Invoked lazily the first time {@link #getObject()} is invoked in * case of a shared singleton; else, on each {@link #getObject()} call. * * @return the object returned by this factory * @see #process(MatchCallback) */ protected Properties createProperties() { Properties result = CollectionUtils.createStringAdaptingProperties(); process((properties, map) -> result.putAll(properties)); return result; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy