[英]How to inject a map of key-value pairs into an Object with just Core Java?
[英]How to inject a key-value map with Spring 4?
VI=Visa
MC=Mastercard
...
我可以使用Spring
自動將其注入Map<String, String>
嗎?
喜歡:
@Resource("${myfile.properties}")
private Map<String, String> creditcards;
那可能嗎?
發現了一個不錯的功能:
@Bean(name = "credit")
public PropertiesFactoryBean mapper() {
PropertiesFactoryBean bean = new PropertiesFactoryBean();
bean.setLocation(new ClassPathResource("credit.properties"));
return bean;
}
將其注入到任何地方,如下所示:
@Resource(name = "credit")
private Properties credit;
您是否看到了以下內容: 如何使用@Value Spring注釋注入地圖?
此人在問題中使用地圖。 那么也許您可以使用此功能? 否則,您可以使用Properties類( https://docs.oracle.com/javase/7/docs/api/java/util/Properties.html ),如果確實需要映射,則可以將properties-object轉換為一張地圖
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.