在 Spring Boot 1.XX 中有一個名為 PropertySourcesBinder.class 的類。請檢查 API 鏈接。 PropertySourcesBinder.class
類的目的很簡單 - 在建構式中接收 a org.springframework.core.env.ConfigurableEnvironment environment,很容易使用extractAll (String somePrefixHere)以給定前綴開頭的屬性。然而,在 Spring Boot 2.XX 中似乎缺少這個類。
然而,這種方法非常有用,因為使用遞回匹配前綴的屬性創建 Map<String subKey, Map<String, Map>> ....而且你不知道它到底有多深。
恐怕我的問題不是很清楚,但是相信一直在使用這門課的人會明白我的問題到底是什么?
uj5u.com熱心網友回復:
聽起來您應該使用新的BinderAPI:
ConfigurableEnvironment environment = new StandardEnvironment();
Map<String, Object> source = new HashMap<>();
source.put("your.prefix.a.b.c.e.f", "one");
source.put("your.prefix.a.b.c.e.g", "two");
source.put("your.prefix.a.b.c.e.h.i.j.k", "three");
environment.getPropertySources().addFirst(new MapPropertySource("example", source));
Bindable<Map<String, Map>> bindable = Bindable.mapOf(String.class, Map.class);
Map<String, Map> bound = Binder.get(environment).bind("your.prefix", bindable).get();
System.out.println(bound);
以上將輸出以下內容:
{a={b={c={e={f=one, g=two, h={i={j={k=three}}}}}}}}
uj5u.com熱心網友回復:
你在找這個嗎?
@ConfigurationProperties(prefix = “myprops”)
這是一個例子:
ConfigurationProperties 的 Baeldung 示例
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/444343.html
上一篇:硒|影根|鍵盤無法訪問元素輸入
下一篇:正則運算式不匹配
