I have created a StringToMapConverter for converting string in predefined format to a Map of key-value pairs.The key can be Enum type also.
However in the converter I required the reference to org.springframework.core.convert.ConversionService instance for converting string to enum.Thus in my myapp-servlet.xml context I tried registering like:
<bean id="stringToMapConverter" class="com.myapp.util.StringToMapConverter"/>
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<bean ref="stringToMapConverter" />
</property>
</bean>
This ended me up in circular dependency error like "conversionService: FactoryBean which is currently in creation returned null from getObject".
Thus I tried using a class ConverterConfig annotated with @Configuration (code I have provided below) which resolved this circular dependency error but now I am facing the following error.
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'searchResultsRefiner' on field 'filtersMap': rejected value []; codes [typeMismatch.searchResultsRefiner.filtersMap,typeMismatch.filtersMap,typeMismatch.java.util.Map,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [searchResultsRefiner.filtersMap,filtersMap]; arguments []; default message [filtersMap]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Map' for property 'filtersMap'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Map] for property 'filtersMap': no matching editors or conversion strategy found]
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doBind(HandlerMethodInvoker.java:810)
at or
I debugged my application and found that the converter was successfully getting added to the registery.But still the framework is unable to find it at time when data is required to convert from java.lang.String to java.util.Map.
Below mentioned is the code I have written:
SearchResultsRefiner.java
public class SearchResultsRefiner {
private Map<SearchRefineFilter, String> filtersMap;
public SearchResultsRefiner() {}
public Map<SearchRefineFilter, String> getFiltersMap() {
return filtersMap;
}
public void setFiltersMap(Map<SearchRefineFilter, String> filtersMap) {
this.filtersMap = filtersMap;
}
}
ConverterConfig.java
import java.util.Collections;
@Configuration
public class ConverterConfig {
private ConversionService conversionService;
private ConverterRegistry converterRegistry;
@Bean
public ConversionService conversionService() {
this.conversionService = ConversionServiceFactory.createDefaultConversionService();
this.converterRegistry = (GenericConversionService) this.conversionService;
return conversionService;
}
@Bean
@DependsOn(value="conversionService")
public StringToMapConverter stringToMapConverter() {
StringToMapConverter stringToMapConverter = new StringToMapConverter();
stringToMapConverter.setConversionService(this.conversionService);
ConversionServiceFactory.registerConverters(
Collections.singleton(stringToMapConverter), this.converterRegistry);
return stringToMapConverter;
}
}
Server startup log
2012-01-19 20:37:08,108 INFO [ContextLoader] Root WebApplicationContext: initialization started
2012-01-19 20:37:08,142 INFO [XmlWebApplicationContext] Refreshing Root WebApplicationContext: startup date [Thu Jan 19 20:37:08 IST 2012]; root of context hierarchy
2012-01-19 20:37:08,201 INFO [XmlBeanDefinitionReader] Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/util-context.xml]
2012-01-19 20:37:08,342 INFO [XmlBeanDefinitionReader] Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/service-context.xml]
2012-01-19 20:37:08,677 INFO [XmlBeanDefinitionReader] Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/persistence-context.xml]
2012-01-19 20:37:09,215 INFO [PropertyPlaceholderConfigurer] Loading properties file from class path resource [fn-cars-bundle.properties]
2012-01-19 20:37:09,221 INFO [PropertyPlaceholderConfigurer] Loading properties file from class path resource [fn-cars-bundle.properties]
2012-01-19 20:37:09,233 INFO [DwrAnnotationPostProcessor] Detected candidate bean [asynchronousRequestHandlerServiceImpl]. Remoting using AsyncService
2012-01-19 20:37:09,246 INFO [DwrAnnotationPostProcessor] Could not infer class for [conversionService]. Is it a factory bean? Omitting bean from annotation processing
2012-01-19 20:37:09,246 INFO [DwrAnnotationPostProcessor] Could not infer class for [stringToMapConverter]. Is it a factory bean? Omitting bean from annotation processing
2012-01-19 20:37:09,436 INFO [DefaultListableBeanFactory] Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@aae8a: defining beans [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,messageSource,memcachedClient,velocityEngine,smtpAuthenticator,mailSession,mailSender,converterConfig,resourceBundleUtil,dwrAnnotationPostProcessor,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#1,locationDAO,,dataSource,sessionFactory,txManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,conversionService,stringToMapConverter,__AsyncService,__dwrConfiguration]; root of factory hierarchy
StringToMapConverter
public class StringToMapConverter implements ConditionalGenericConverter {
private static final String COMMA = ",";
private static final String COLON = ":";
private ConversionService conversionService;
/**
* @param conversionService the conversionService to set
*/
public void setConversionService(ConversionService conversionService) {
this.conversionService = conversionService;
}
@Override
public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(String.class, Map.class));
}
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
boolean matches =
this.conversionService.canConvert(sourceType, targetType.getMapKeyTypeDescriptor())
&& this.conversionService.canConvert(sourceType, targetType.getMapValueTypeDescriptor());
return matches;
}
@SuppressWarnings("unchecked")
@Override
public Object convert(Object source, TypeDescriptor sourceType,
TypeDescriptor targetType) {
if (source == null) {
return null;
}
String sourceString = (String) source;
if (sourceString.trim().isEmpty()) {
return Collections.emptyMap();
}
@SuppressWarnings("rawtypes")
Map targetMap = new HashMap();
String[] keyValuePairs = sourceString.split(COMMA);
String[] keyValueArr = null;
String key = null;
String value = null;
for (String keyValuePair : keyValuePairs) {
keyValueArr = keyValuePair.split(COLON);
key = keyValueArr[0].trim();
value = keyValueArr[1].trim();
Object targetKey =
this.conversionService.convert(
key, sourceType, targetType.getMapKeyTypeDescriptor(key));
targetMap.put(targetKey, value);
}
return targetMap;
}
}
Can anybody please explain me the reason behind such behavior because the stringToMapConverter bean is created as can be seen in server startup log shown above and how to get this resolved?
Thanks,
Jignesh