0
VI=Visa
MC=Mastercard
...

Can I automatically inject this into a Map<String, String> with Spring?

Like:

@Resource("${myfile.properties}")
private Map<String, String> creditcards;

Is that possible?

membersound
  • 81,582
  • 193
  • 585
  • 1,120

2 Answers2

2

Found a nice feature:

@Bean(name = "credit")
public PropertiesFactoryBean mapper() {
    PropertiesFactoryBean bean = new PropertiesFactoryBean();
    bean.setLocation(new ClassPathResource("credit.properties"));
    return bean;
}

inject it anywhere as follows:

@Resource(name = "credit")
private Properties credit;
membersound
  • 81,582
  • 193
  • 585
  • 1,120
0

Did you saw this: How to inject a Map using the @Value Spring Annotation?

Here in the Question the person uses a map. So perhaps you can use this? Otherwise you can use the Properties class (https://docs.oracle.com/javase/7/docs/api/java/util/Properties.html) and if you really need a map, you can transform the properties-object to a map

Community
  • 1
  • 1
cproeller
  • 107
  • 1
  • 12