1

I am trying to load a map from .properties file into a HashMap

the properties file has the following:

try.map= one=1,\
two=2

The code:

@org.springframework.beans.factory.annotation.Value("${try.map}")
HashMap<String, String> tryMap;

And loading the property to the map results in :

 java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.HashMap]: no matching editors or conversion strategy found

Any ideas how to create a mapping strategy for this?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Gleeb
  • 10,773
  • 26
  • 92
  • 135

3 Answers3

1

i got this error too. If you want to resolve it without doing much efforts, just check your imports properly. Mostly you will get this error if you import wrong files. In case of you, I think you are importing wrong HashMap in your file. Check it should be java.util.HashMap.

1

This works for me:

try.map= {\
  one: 1,\
  two: 2\
}

The code:

@org.springframework.beans.factory.annotation.Value("#{${try.map}}")
HashMap<String, String> tryMap;

Yes, the syntax is quite odd.

This SO post has more info: How to fill HashMap from java property file with Spring @Value

StvnBrkdll
  • 3,924
  • 1
  • 24
  • 31
0

There is no default converter for that. So you have to write your own converter and register it to the conversationService.

Here an annotation-based example:

@Bean(name="conversionService") 
public static ConversionService getConversionService(){
    ConversionServiceFactoryBean conversionServiceFactoryBean = new ConversionServiceFactoryBean();
    Set<Converter<?,?>> converters = new HashSet<Converter<?,?>>();
    converters.add(new StringToHashMapConverter());
    conversionServiceFactoryBean.setConverters(converters);
    conversionServiceFactoryBean.afterPropertiesSet();
    return conversionServiceFactoryBean.getObject();
}

The Converter:

import java.util.HashMap;

import org.springframework.core.convert.converter.Converter;

public class StringToHashMapConverter implements Converter<String,HashMap> {

    @Override
    public HashMap convert(String paramS) {
           //do the coversion
    }

}
Jens
  • 67,715
  • 15
  • 98
  • 113
  • @Gleeb can you explain why? – Jens Feb 13 '15 at 08:33
  • Still getting the same error. I have a conversation service in my app. I will update the question with its confirmation maybe it's not configured properly – Gleeb Feb 13 '15 at 08:43
  • Can you try to debug your code if the Converter is called? Please check if you are using annotation configuration in your project. – Jens Feb 13 '15 at 08:45
  • ok, this works but there is a problem where in some places some configuration files are loaded before the conversion service bean, how do i guarantee that the conversion service is loaded before any other bean? – Gleeb Feb 14 '15 at 09:23
  • I think spring will load the converter before the beans are created. So you can not have such situation. If you have the Problem you can use the `@DependsOn` annotation to guarantee that the conversation service is loaded before the bean. Hope this helps. – Jens Feb 14 '15 at 09:29