gpt4 book ai didi

java - 在spring boot中从属性文件注入(inject)值数组

转载 作者:搜寻专家 更新时间:2023-10-31 20:25:22 25 4
gpt4 key购买 nike

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

然后我尝试像这样注入(inject) 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]

Q1:基本上我做对了吗?

Q2:我在互联网上找不到任何关于如何处理属性文件中的原始元组数组的帮助,是否有可能将其注入(inject)到 spring-boot 配置中——或者我是否需要将我的配置重写为字符串,使用 split() 来获得个人值(value)(为了可维护性和可读性,我真的不想这样做)?

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

最佳答案

你需要设置你的property-prefix不需要@Configuration@Component 并使用嵌入式公共(public)静态类 来包装货币的属性

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

关于java - 在spring boot中从属性文件注入(inject)值数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50179656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com