foo.bar.one=1
foo.bar.two=2
foo.bar.n=n
可以將所有屬性foo.bar.*注入一個欄位嗎?
偽代碼:
@Value("${foo.bar.*}")
private Map<String, Object> foobar;
uj5u.com熱心網友回復:
首先,不,你不能做你想做的事@Value。無論如何你都不應該使用@Value,至少一個原因是它不支持寬松的系結。
其次,@ConfigurationProperties會起作用,但你必須非常小心你如何命名事物。從字面上看你的例子,你需要:
@ConfigurationProperties(prefix = "foo")
public class Props {
private Map<String, String> bar = new HashMap<>();
public Map<String, String> getBar() {
return bar;
}
public void setBar(Map<String, String> bar) {
this.bar = bar;
}
}
注意prefix=foo名為 的 and 屬性bar。
uj5u.com熱心網友回復:
使用@ConfigurationProperties("prefix")(在班級級別)
所以在你的情況下:
@ConfigurationProperties("foo")
public class SomeClass {
private Map<String, Object> bar = new HashMap<>();; //the name of the map can serve as a second part of prefix
...
//dont forget getter that Spring will use to add values.
}
更多在這里
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/426026.html
