在Spring Boot中从属性文件注入值数组

在Spring Boot中从属性文件注入值数组,第1张

在Spring Boot中属性文件注入值数组

您需要设置 属性前缀 ,不需要 @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

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


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/4903213.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-12
下一篇 2022-11-12

发表评论

登录后才能评论

评论列表(0条)

保存