【问题标题】:Injecting array of values from properties file in spring boot在spring boot中从属性文件中注入值数组
【发布时间】:2018-10-15 05:17:33
【问题描述】:

好的,所以我有一个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

@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);
        }
    }

...由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]

第一季度: 基本上我做得对吗?

第二季度: 我在互联网上找不到如何处理属性文件中的原始元组数组的任何帮助,甚至可以将其注入到 spring-boot 配置中 - 或者我是否需要使用 @987654331 将我的配置重写为字符串@ 获取单个值(为了可维护性和可读性,我真的不想这样做)?

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

【问题讨论】:

    标签: java spring spring-boot configuration properties-file


    【解决方案1】:

    你需要设置你的属性前缀 不需要 @Configuration@Component 并使用嵌入的 public static class 来包装货币的属性

     @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

     @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;
        }
    

    【讨论】:

    • Hm... 这让我想到:BeanCreationException:创建名称为“marketConfig”的 bean 时出错:无法将属性绑定到 MarketConfig(prefix=market,ignoreInvalidFields=false,ignoreUnknownFields=true,ignoreNestedProperties=false );嵌套异常是 org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) 处的 java.lang.NullPointerException ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    • 更新的答案 List 应该类似于 List
    • /src/main/resources/ 中的 config.properties 吗?
    • 编辑答案:MarketConfig 应该在 MarketApplication 中
    • 看看这里demo不要在这里获取空指针...
    猜你喜欢
    • 2019-06-04
    • 1970-01-01
    • 2015-12-26
    • 2017-03-18
    • 2017-08-23
    • 2018-10-27
    • 1970-01-01
    • 2020-07-01
    • 2023-02-03
    相关资源
    最近更新 更多