我在Spring Boot应用程序中通过以下方式注入了从.yaml读取的映射中的属性:
@Value("#{${app.map}}")
private Map<String, String> indexesMap = new HashMap<>();但两者都不是
app:
map: {Countries: 'countries.xlsx', CurrencyRates: 'rates.xlsx'}
//note values in single quotes
nor
app:
map: {Countries: "countries.xlsx", CurrencyRates: "rates.xlsx"}(如https://www.baeldung.com/spring-value-annotation所述)
nor
app:
map:
"[Countries]": countries.xslx
"[CurrencyRates]": rates.xlsx(在https://stackoverflow.com/a/51751123/2566304上建议)
works -我一直收到消息“插入自动连接的依赖项失败;嵌套的异常是java.lang.IllegalArgumentException:无法解析占位符”
同时,这也是可行的:
@Value("#{{Countries: 'countries.xlsx', CurrencyRates: 'rates.xlsx'}}")
private Map<String, String> indexesMap = new HashMap<>();但我希望将属性外部化
发布于 2019-04-17 04:37:31
按照您所链接的问题的答案之一所建议的,使用@ConfigurationProperties:
@Bean(name="AppProps")
@ConfigurationProperties(prefix="app.map")
public Map<String, String> appProps() {
return new HashMap();
}然后
@Autowired
@Qualifier("AppProps")
private Map<String, String> props;将与配置一起工作
app:
map:
Countries: 'countries.xlsx'
CurrencyRates: 'rates.xlsx'编辑:@Value注释也可以使用,但您必须将其视为YAML中的字符串:
@Value("#{${app.map}}")
private Map<String, String> props;和
app:
map: "{Countries: 'countries.xlsx', CurrencyRates: 'rates.xlsx'}"请注意map值周围的引号。显然,Spring在这种情况下是从一个字符串中解析出来的。
https://stackoverflow.com/questions/55715308
复制相似问题