0

This is the set up

application.properties

Property.cache = name1,name2,...,nameN

CacheManager.java

[...]
@Scheduler(time_of_schedule)
@CacheEvict(value="#'${property.cache}.split()'")
void dueCache(){
    log.info("cache clean");
}
[...]

The issue is that @CacheEvict value throws nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'property.cache'.split(',')' in value "#{'${property.cache'.split(',')}" this is because value is a String[] normally this would work with String attributes. Is there any way to do it with SpEL?

I tryed with 4 ways:

  1. @CacheEvict(value = "#{'${property.cache}'.split(',')}", allEntries = true)
  2. @CacheEvict(value = "#{T(java.util.Arrays).asList(#root.getProperty('property.cache').split(','))}", allEntries = true)
  3. @CacheEvict(value = {"cache1","cache2"}, allEntries = true)
  4. @CacheEvict(value = "cache1", allEntries = true)

The options 1 and 2 would work if value was a String value() but value is defined as String[] value(). The options 3 and 4 work, but is unresonable to write value = {"cache1","cache2",.....,"cachen"} if there is a way to use properties.

Massirito
  • 1
  • 2
  • 1
    Your analysis of the issue is wrong. It isn't about it being a `String` or `String[]` it is complaining about your expression. I would expect that just `${property.cache}` should work (if SpEL works this should work as well). Spring will do the conversion to an array. – M. Deinum Mar 29 '23 at 08:08
  • Thanks for reaching out to help, I really appreciate it. How would you suggest I write it? I tried to write something like this @CacheEvict(value = "${property.cache}", allEntries = true) but I got an error message "Cannot find cache named '${property.cache}' "... Looks like it took the content as a string instead of an array – Massirito Mar 29 '23 at 12:20
  • Try to use Property.cache = {'name1','name2','...','nameN'} then use value="#{${Property.cache}}" See https://stackoverflow.com/questions/12576156/reading-a-list-from-properties-file-and-load-with-spring-annotation-value/59969974#59969974 – Sampisa Mar 29 '23 at 13:16
  • Apparently SpEL works but a value expression doesn't, have you tried `#{${property.cache}}`? And if that fails try `#{'${property.cache}'.split(',')}` as the current expression you have isn't really a valid one. But that shouldn't be needed and Spring should be able to convert the resulting `String` into a `String[]` itself. – M. Deinum Mar 29 '23 at 18:04
  • Hi trying with '#{${Property.cache}}' throws the following error....Cannot find cache named '#{${Property.cache}}' for Builder[public void com.exe.configuration.CacheConfig.evictCache()] caches=[#{${Property.cache}}] | key='' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='',true,false... The version #{'${property.cache}'.split(',')} does the same error but on the log you have caches=[#{'${property.cache}'.split(',')}] ... The fact is that those methods would work in a scenario when you have @Value("${property.cache}'")String[] array; but @Value has a String parameter – Massirito Mar 30 '23 at 08:19
  • @CacheEvict(value = "String[]", key="String") in this case the #{'${property.cache}'.split(',')} has effect on key, i've tryed, but doesn't seem to have effect in value. So that's why I came to the conclusion that the fact of being an array has an impact.. I might be wrong – Massirito Mar 30 '23 at 08:28

0 Answers0