在Spring Boot中从属性文件注入值数组 [英] Injecting array of values from properties file in spring boot

查看:905
本文介绍了在Spring Boot中从属性文件注入值数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,所以我有一个 config.properties ..:

Ok, so i have a config.properties ..:

market.curpairs[0].name=EuroDollar
market.curpairs[0].symbol=EURUSD
market.curpairs[0].minamount=0.1
market.curpairs[1].name=EuroFranken
market.curpairs[1].symbol=EURCHF
market.curpairs[1].minamount=0.1
market.currs[0].name=Euro
market.currs[0].symbol=EUR
market.currs[0].minamount=1.0
market.currs[0].withfee=0.1
market.currs[1].name=Dollar
market.currs[1].symbol=USD
market.currs[1].minamount=1.0
market.currs[1].withfee=0.1
market.currs[2].name=Franken
market.currs[2].symbol=CHF
market.currs[2].minamount=1.0
market.currs[2].withfee=0.1

然后我尝试将其注入 MarketConfig.java 像这样:

which i then attempt to inject into MarketConfig.java like this:

@PropertySource("classpath:config.properties")
@ConfigurationProperties(prefix = "market")
@Validated
public class MarketConfig {

    // the configured currencies
    private List<MarketCurrency> currs;

    // the configured currencypairs
    private List<MarketCurrencypair> curpairs;

  /* static classes */
  public static class MarketCurrency {
    String name;
    String symbol;
    double minamount;
    // getter and setter ommitted
  }
  public static class MarketCurrencypair {
    String name;
    String symbol;
    double minamount;
    double withfee;
    // getter and setter ommitted
  }
  // getter and setter ommitted
}

..然后在 MarketService.java 中使用:

 @Service
    public class MarketService implements IMarketService {

        private final MarketConfig config;

        // ....

         public MarketService(MarketConfig config) {
            this.config = config;
         }
        // ....
        public void printConfig() {
           System.out.println("________________ CONFIGURATION: ");
           this.config.getCurpairs().forEach(System.out::println);
           this.config.getCurrs().forEach(System.out::println);
        }
    }

...由<$ c调用$ c> Applicationmain :

@SpringBootApplication
@EnableSwagger2
@ComponentScan
@EnableConfigurationProperties({MarketConfig.class})
public class MarketApplication implements CommandLineRunner {

    private final MarketService service;

    /**
     * Constructor
     * @param service  ..the Service
     */
    public MarketApplication(MarketService service) {
        this.service = service;
    }

    public static void main(String[] args) {
        SpringApplication.run(MarketApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        this.service.printConfig();
    }
}

...让我 NullPointerException

Caused by: java.lang.NullPointerException: null
    at forex.market.service.MarketService.printConfig(MarketService.java:67) ~[classes/:na]

第一季度:
我基本上是对的吗?

Q1: Am i doing it right, basically?

第二季度:
在Internet上找不到如何处理属性文件中原始元组数组的任何帮助,甚至有可能将其注入到spring-boot配置中-还是我需要使用<$ c $将配置重写为字符串? c> split()来获取单个值(出于维护性和可读性的考虑,我真的不想这么做)?

Q2: I couldn't find any help on the internet how to handle arrays of primitive-tuples in properties files, is it even possible to inject that into a spring-boot configuration - or do i need to rewrite my config to strings, using split() to acquire the individual values (which i really don't want to for the sake of maintainability and readability) ?

在此先感谢您-如果您错过了一些信息/来源,请发表评论,我会尽快提供。

Thanks in advance - if you miss out some information/source, please comment, i will supply it shortly.

推荐答案

设置您的属性前缀
不要需要 @Configuration @Component 并使用嵌入式的公共静态类来包装货币的属性

you need to set your property-prefix don't need @Configuration and @Component and use embedded public static class to wrap properties of the currency

 @PropertySource("classpath:config.properties")
 @ConfigurationProperties(prefix = "market")
 @Validated
 public class MarketConfig {
      List<MarketCurrency> currs;
      //getters setters

      public static class MarketCurrency {

          String name;
          String symbol;
        ....
//getters setters

将MarketConfig.class添加到@EnableConfigurationProperties

add MarketConfig.class to @EnableConfigurationProperties

 @SpringBootApplication
 @EnableSwagger2
 @EnableConfigurationProperties({MarketConfig.class})
 public class MarketApplication implements CommandLineRunner {

    private final MarketService service;
    private final MarketConfig config;

    public MarketApplication(MarketService service, MarketConfig config) {
       this.service = service;
       this.config = config;
    }

这篇关于在Spring Boot中从属性文件注入值数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆