繁体   English   中英

SpringBoot将地图Yaml映射到地图

[英]SpringBoot map yaml to map of map

是否可以将yaml文件下方的Map<String, Map<String, String>注入到Spring Boot应用程序中Map<String, Map<String, String>其中tradeType将是外部映射的键,而P和B将是内部映射的键值。

tradeType:
    P: B
    S: S
securityCode: 
    ICI: ICICI Bank
    BOB: Bank of Baroda
    BOA: Bank of America
    BOS: Bank of Singapore 

如建议的那样,这是我班上的样子。

@Configuration
@PropertySource("file:data/referenceDataMapping.yaml")
@ConfigurationProperties(prefix = "map")
public class ReferenceDataMapping {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private Map<String, Map<String, String>> entries;

    @Override
    public String toString() {
        if (entries != null) {
            logger.info(entries.toString());
            return entries.toString();
        } else {
            return null;
        }
    }

    public Map<String, Map<String, String>> getEntries() {
        return entries;
    }

    public void setEntries(Map<String, Map<String, String>> entries) {
        this.entries = entries;
    }

}

来自build.gradle

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-activemq:2.1.2.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-security:2.1.2.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-web:2.1.2.RELEASE'
    compile 'org.apache.camel:camel-spring-boot-starter:2.23.1'
    compile 'org.apache.camel:camel-quartz2:2.23.1'
    compile 'org.apache.camel:camel-jms:2.23.1'
    compile 'org.apache.camel:camel-jacksonxml:2.23.1'
    compile 'com.fasterxml.jackson.core:jackson-databind:2.9.8'
    compile 'net.sf.saxon:Saxon-HE:9.9.1-1'
    testCompile 'org.springframework.boot:spring-boot-starter-test:2.1.2.RELEASE'
    testCompile 'org.springframework.security:spring-security-test:5.1.3.RELEASE'
}

referenceDataMapping.yaml

    map: 
   entries:
      tradeType:
          P: B
          S: S
      securityCode: 
          ICI: ICICI Bank
          BOB: Bank of Baroda
          BOA: Bank of America
          BOS: Bank of Singapore

是的 有可能的。

@Configuration
@ConfigurationProperties(prefix="map")
public class Test {

    private Map<String,Map<String,String>> entires;

    public Map<String, Map<String, String>> getEntires() {
        return entires;
    }

    public void setEntires(Map<String, Map<String, String>> entires) {
        this.entires = entires;
    }

}

application.yml

map:
  entires:
    tradeType:
      P: B
      S: S
    securityCode: 
      ICI: ICICI Bank
      BOB: Bank of Baroda
      BOA: Bank of America
      BOS: Bank of Singapore 

输出:

{tradeType={P=B, S=S}, securityCode={ICI=ICICI Bank, BOB=Bank of Baroda, BOA=Bank of America, BOS=Bank of Singapore}}

更新:

如本github-issue中所述 @PropertySource不支持yaml文件 在这种情况下,请遵循本指南PropertySource-with-yaml-files

如果YAML与application.yml位于不同的文件中,

@Component
public class YourClass {


    private Map<String, String> tradeType;
    private Map<String, String> securityCode;

    public Map<String, String> getTradeType() {
        return tradeType;
    }

    public Map<String, String> getSecurityCode() {
        return securityCode;
    }


    @PostConstruct
    public void yamlFactory() {
        YamlMapFactoryBean factory = new YamlMapFactoryBean();
        factory.setResources(new ClassPathResource("your.yml"));
        tradeType = (Map<String, String>) factory.getObject().get("tradeType");
        securityCode = (Map<String, String>) factory.getObject().get("securityCode");
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM