Java 在spring引导中注入属性文件中的值数组
好的,我有一个Java 在spring引导中注入属性文件中的值数组,java,spring,spring-boot,configuration,properties-file,Java,Spring,Spring Boot,Configuration,Properties File,好的,我有一个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=
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
}
…由应用程序主调用:
@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:
基本上,我做得对吗
Q2:
我在互联网上找不到任何关于如何处理属性文件中的原始元组数组的帮助,甚至可以将其注入到spring boot配置中吗?或者我需要将配置重写为字符串,使用split()
来获取各个值吗(为了可维护性和可读性,我真的不想这么做)
提前感谢-如果您遗漏了一些信息/来源,请发表评论,我将很快提供。您需要设置属性前缀
不需要@Configuration和@Component,使用嵌入的公共静态类包装货币属性
@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
嗯……这让我想到:BeanCreationException:创建名为“marketConfig”的bean时出错:无法将属性绑定到marketConfig(前缀=market,ignoreInvalidFields=false,ignoreUnknownFields=true,ignoreNestedProperties=false);嵌套异常是org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]更新的答案列表应该类似于/src/main/resources/?中的Listis config.properties。已编辑的答案:MarketConfig应该位于MarketApplications中。请查看此处,不要在此处获取nullpoiter。。。
@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
@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;
}