Java Code Examples for org.springframework.beans.factory.config.YamlPropertiesFactoryBean

Following code examples demonstrate how to use org.springframework.beans.factory.config.YamlPropertiesFactoryBeanfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.beans.factory.config.YamlPropertiesFactoryBeanand various code implementation of this class.

    public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(new ClassPathResource("defaults.yaml"));
        propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());
        return propertySourcesPlaceholderConfigurer;
    } 


    public static void main(String[] args) {
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(
                new ClassPathResource("application.yml")
        );
        new SpringApplicationBuilder()
                .properties(yaml.getObject())
                .sources(Application.class)
                .build().run(args);
    } 

    public PropertySourcesPlaceholderConfigurer getYamlPropertiesFactoryBean() {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(new ClassPathResource("student.yml"));
        propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());
        return propertySourcesPlaceholderConfigurer;
    } 

    public YamlPropertiesFactoryBean queries() {
        final YamlPropertiesFactoryBean yamlProperties = new YamlPropertiesFactoryBean();
        yamlProperties.setResources(new ClassPathResource("/queries.yml"));

        return yamlProperties;
    } 

    public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(new ClassPathResource("pathpool.yml"));
        propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());
        return propertySourcesPlaceholderConfigurer;
    } 

    public static PropertySourcesPlaceholderConfigurer yamlProperties(){
        PropertySourcesPlaceholderConfigurer placeholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean factoryBean = new YamlPropertiesFactoryBean();
        factoryBean.setResources(new ClassPathResource("application-myyml.yml"));

        placeholderConfigurer.setProperties(factoryBean.getObject());
        return placeholderConfigurer;
    } 

    public static Map loadYamlProperties(final Resource... resource) {
        val factory = new YamlPropertiesFactoryBean();
        factory.setResolutionMethod(YamlProcessor.ResolutionMethod.OVERRIDE);
        factory.setResources(resource);
        factory.setSingleton(true);
        factory.afterPropertiesSet();
        return factory.getObject();
    } 

    public static Properties loadYaml(String resource) {
        YamlPropertiesFactoryBean bean = new YamlPropertiesFactoryBean();
        bean.setResources(new ClassPathResource(resource));
        bean.afterPropertiesSet();

        Properties properties = bean.getObject();
        properties.entrySet().forEach(e -> e.setValue(String.valueOf(e.getValue())));
        return properties;
    } 

    public PropertySourcesPlaceholderConfigurer configurer(){
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean factoryBean = new YamlPropertiesFactoryBean();
        factoryBean.setResources(new FileSystemResource("src/main/resources/test.yaml"));
        configurer.setProperties(factoryBean.getObject());
        return configurer;
    } 

    public static Properties loadYamlProperty(String fileName) {

        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        final File file = new File(fileName);
        yaml.setResources(new FileSystemResource(file));
        return yaml.getObject();
    } 

    public Map<String, String> extract(Resource resource) {
        YamlPropertiesFactoryBean propertiesFactoryBean = new YamlPropertiesFactoryBean();
        propertiesFactoryBean.setResources(resource);
        return propertiesFactoryBean.getObject().entrySet().stream()
                .collect(Collectors.toMap(
                        e -> String.valueOf(e.getKey()),
                        e -> String.valueOf(e.getValue())));
    } 

Advertisement
Javadoc
Factory for 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 String for access through the common Properties#getProperty method (e.g. in configuration property resolution through PropertyResourceConfigurer#setProperties(Properties)). If this is not desirable, use YamlMapFactoryBean instead.

The Properties created by this factory have nested paths for hierarchical objects, so for instance this YAML environments: dev: url: http://dev.bar.com name: Developer Setup prod: url: http://foo.bar.com name: My Cool App is transformed into these properties: environments.dev.url=http://dev.bar.com environments.dev.name=Developer Setup environments.prod.url=http://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 1.18 or higher, as of Spring Framework 5.0.6. @author Dave Syer @author Stephane Nicoll @author Juergen Hoeller @since 4.1

Read More
Advertisement