0

Sample yaml looks like

  "mappings":
    "name": "foo"
    "aliases": "abc"

Trying to implement it using PropertySourceFactory, but unsuccessful.

import import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
//@ConfigurationProperties
@PropertySource(value = "classpath:order_config.yml", factory = YamlPropertySourceFactory.class)
public class ValidatorConfig {

    @Value("${yaml.mappings}")
    private Map<String, String> mappings = new HashMap<>();

    @Value("${yaml.mappings.name}")
    private String create;


    public String getValidatorBean(String tenant, String requestType) {
        System.out.println(mappings);
        return "yes";
    }
}



import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory;

import java.io.IOException;
import java.util.Properties;

public class YamlPropertySourceFactory implements PropertySourceFactory {

    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource)
            throws IOException {
        YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
        factory.setResources(encodedResource.getResource());

        Properties properties = factory.getObject();

        return new PropertiesPropertySource(encodedResource.getResource().getFilename(), properties);
    }
}

Have tried using a bunch of methods using @Value, @ConfigurationProperties, but unsuccessful Can we implement it using YamlMapFactoryBean. Have not been able to find its working demonstration.

  • Have a look at this: https://stackoverflow.com/questions/24917194/spring-boot-inject-map-from-application-yml – Arun Sai Mustyala Oct 08 '21 at 06:16
  • Maybe the following will help: https://stackoverflow.com/questions/69490852/merge-property-file-into-application-properties/69491001#69491001 – Gregor Zurowski Oct 08 '21 at 08:09
  • Does this answer your question? [Spring Boot - inject map from application.yml](https://stackoverflow.com/questions/24917194/spring-boot-inject-map-from-application-yml) – João Dias Oct 08 '21 at 09:49

0 Answers0