【问题标题】:spring-boot yaml configuration standalonespring-boot yaml 配置独立
【发布时间】:2015-05-31 08:41:52
【问题描述】:

是否可以在 spring-boot 应用程序之外利用 Spring-Boot 的 YAML 配置?即我们可以只使用 YAML 配置功能添加 spring-boot 依赖项吗?

我的用例是一个需要配置的小型实用程序项目,YAML 方法很合适。如果我将它连接到主项目(这是一个 Spring-Boot 应用程序),一切都很好。但是,如果我想单独测试这个实用程序项目(简单的 java-app),它不会连接配置。有什么想法吗?可能是我在这里遗漏了一些基本的东西。

下面的示例代码 sn-p。下面的包是组件扫描的一部分。

@Component
@ConfigurationProperties(prefix="my.profile")
public class TestConfig {

    private List<String> items;

    public List<String> getItems() {
        return items;
    }

    public void setItems(List<String> items) {
        this.items = items;
    }
}

YAML 配置

my:
    profile:
        items:
            - item1
            - item2

【问题讨论】:

  • Yaml 和 @ConfigurationProperties 是两个不同的东西。 Yaml 支持在 Spring 本身中提供,@ConfigurationProperties 是 spring boot 的一个特性。
  • 但是,作为 Spring-Boot 应用程序运行时的相同代码在作为非 Spring Boot 应用程序运行时不会读取 yaml 配置。有什么想法吗?
  • 我说 yaml 支持是可用的,我不是说它是开箱即用的。您必须显式添加 YamlPropertiesFactoryBean 才能读取 yaml 文件并作为属性使用。它也可以从 Spring 4.1 开始使用,因此它不适用于早期版本。
  • 感谢您的回复.. 可能我应该稍微改述一下这个问题.. 简而言之,spring-boot 读取和绑定 yml 的开箱即用能力不适用于非-spring-boot 应用程序,.. 我想知道是否有一种方法可以只使用这一功能而不将其作为 spring-boot 应用程序运行(原因 - 简单,我喜欢 Spring-boot,但并不总是具有灵活性将所有内容迁移到它)。而且我已经看过 YamlPropertiesFactory,它提供的东西相当原始,与 spring-boot 开箱即用的能力不完全匹配。
  • 那么答案很简短,是否定的......您必须使用弹簧靴的一部分并将其迁移到非弹簧靴。

标签: spring spring-boot snakeyaml


【解决方案1】:

密钥是YamlPropertiesFactoryBean,正如M. Deinum 提到的那样。

import org.springframework.beans.factory.config.YamlProcessor;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
import org.springframework.boot.context.properties.source.MapConfigurationPropertySource;
import org.springframework.core.io.ClassPathResource;

import java.util.Properties;

public class PropertyLoader {

    private static Properties properties;
    
    private PropertyLoader() {}

    public static Properties load(String... activeProfiles) {
        if (properties == null) {
            YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
            factory.setResources(new ClassPathResource("application.yml"));
            factory.setDocumentMatchers((profile) -> YamlProcessor.MatchStatus.FOUND); // TODO filter on active profiles
            factory.afterPropertiesSet();
            
            properties = factory.getObject();
        }
        return properties;
    }

    public static <T> T value(String property, Class<T> target) {
        load();
        ConfigurationPropertySource propertySource = new MapConfigurationPropertySource(properties);
        Binder binder = new Binder(propertySource);
        return binder.bind(property.toLowerCase(), target).get();
    }
}

PropertyLoader#value 然后可以像这样使用:

List<String> items = PropertyLoader.value("my.profile.items", List.class);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 2020-05-07
    相关资源
    最近更新 更多