I an trying to use an "own made" object as part of the key in a @Cacheable annotation:
@Cacheable(value = "tecdocData", key = "'TCDD:stos::'.concat(#stos)")
List<TecDocData> getTecDocData(Collection<SimpleTecDocObject> stos);
This leads to an error with following stacktrace (a relevant part):
Caused by: org.springframework.expression.AccessException: Problem invoking method: public java.lang.String java.lang.String.concat(java.lang.String)
at org.springframework.expression.spel.support.ReflectiveMethodExecutor.execute(ReflectiveMethodExecutor.java:73)
at org.springframework.expression.spel.ast.MethodReference$MethodValueRef.getValue(MethodReference.java:97)
... 100 more
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1001E:(pos 0): Type conversion problem, cannot convert from java.util.HashSet<?> to java.lang.String
at org.springframework.expression.spel.support.StandardTypeConverter.convertValue(StandardTypeConverter.java:72)
at org.springframework.expression.spel.support.ReflectionHelper.convertArguments(ReflectionHelper.java:281)
at org.springframework.expression.spel.support.ReflectiveMethodExecutor.execute(ReflectiveMethodExecutor.java:61)
... 101 more
Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.util.HashSet<?> to type java.lang.String for value '[SimpleTecDocObject [guid=GUID-C2DBD976-79F0-42FB-94CE-9352DE43A184, locale=de, version=1]]'; nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type com.sxp.setix.api.SimpleTecDocObject to type java.lang.String
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:169)
at org.springframework.expression.spel.support.StandardTypeConverter.convertValue(StandardTypeConverter.java:66)
... 103 more
Caused by: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type com.sxp.setix.api.SimpleTecDocObject to type java.lang.String
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:276)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:172)
at org.springframework.core.convert.support.CollectionToStringConverter.convert(CollectionToStringConverter.java:65)
at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:35)
... 105 more
As far as I can see, spring do make an attempt to convert a HashSet but fails to convert the contained "SimpleTecDocObject" values.
In order to solve this issue I defined a converter and registered it in my Spring configuration. This was done quite straight ahead as documented in "7.5 Spring 3 Type Conversion" 7. Validation, Data Binding, and Type Conversion
Converter class:
public class SimpleTecdocObjectConverter implements Converter<SimpleTecDocObject, String> {
@Override
public String convert(final SimpleTecDocObject source) {
StringBuilder result = new StringBuilder();
result.append(source.getGuid()).append(source.getLocale()).append(source.getVersion());
return result.toString();
}
}
Spring configuration:
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean id="tdConverter" class="com.kion.lsg.service.tecdoc.converter.SimpleTecdocObjectConverter" />
</list>
</property>
</bean>
Unfortunately this seems not to work. I get exactly the same exceptions as above. Spring abstraction seems to ignore the conversionService defined above and take some default one.
Solution:
Lotsa thanks to Akshay Singhal
Instead of using a converter one can use a static key generator inside of the SpEL expression:
@Cacheable(value = "tecdocData", key = "T(com.kion.lsg.service.tecdoc.converter.SimpleTecDocObjectCollectionKeyGenerator).generateKey(#stos)")
List<TecDocData> getTecDocData(Collection<SimpleTecDocObject> stos);
For example with a KeyGenerator defined as following:
public class SimpleTecDocObjectCollectionKeyGenerator {
public static String generateKey(final Collection<SimpleTecDocObject> stos){
List<String> tecdocIdsList = new ArrayList<>();
for (SimpleTecDocObject sto : stos){
tecdocIdsList.add(sto.toString());
}
Collections.sort(tecdocIdsList);
return tecdocIdsList.toString();
}
}