title: How to use @EnableCache , multiple CacheManagers and a custom cache resolver
body: <p>I am trying to have two Caffeine CacheManagers, one default and one named ("new.kid").
  The named one is used for a particular method in a class NewKid and is designated by @Cacheable(cacheManager="new.kid") on the method (the annotation settings cannot be changed for the purpose of this problem). </p>
  
  <p>I have defined a custom CacheResolver, overriden getCacheNames so that it returns the new.kid cache manager cache names for for targets of type NewKid and the defautl cache names otherwise.</p>
  
  <p>There is actually not a lot of info on this, But from what I gathered, if one has more than one (the default) CacheManager, a custom CacheResolver is mandatory.
  I have defined a custom CacheResolver, overriden getCacheNames so that it returns the new.kid cache manager cache names for for targets of type NewKid and the default cache manager names otherwise.
  Full example (copied and modified form the Spring Caching Getting Started Guide) is available in my github <a href="https://github.com/tyonchev/spring-cache" rel="nofollow noreferrer">https://github.com/tyonchev/spring-cache</a></p>
  
  <pre class="lang-java prettyprint-override"><code>
      @Component
      public class NewKid {
          @Cacheable(cacheManager="new.kid")
          public String whatsYourName(String input){
              return "New Kid Name " + input;
          }
      }
  
  </code></pre>
  
  <pre class="lang-java prettyprint-override"><code>
      @Configuration
      public class NewKidCacheManager {
          @Bean("new.kid")
          public CacheManager newKidCacheManager() {
              CacheManager cmgr = new CaffeineCacheManager();
              ((CaffeineCacheManager) cmgr).setCacheNames(Arrays.asList(cacheName));
              ((CaffeineCacheManager) cmgr).setCaffeine(Caffeine.newBuilder());
              return cmgr;
          }
      }
  </code></pre>
  
  <pre class="lang-java prettyprint-override"><code>
      @Configuration
      @EnableCaching
      public class CacheManagerConfig extends CachingConfigurerSupport {
          @Autowired
          @Qualifier(value = "new.kid")
          CacheManager newKidCacheManager;
  
          @Bean
          @Override
          public CacheManager cacheManager() {
      ...
          }
  
          @Bean
          @Primary
          @Override
          public CacheResolver cacheResolver( ) {
              return new CustomResolver(newKidCacheManager);
          }
  
  </code></pre>
  
  <pre class="lang-java prettyprint-override"><code>
  class CustomResolver extends AbstractCacheResolver {
  
      public CustomResolver(CacheManager newKidCacheManager) {
          this.newKidCacheManager = newKidCacheManager;
      }
  
      @Override
      protected Collection&lt;String&gt; getCacheNames(CacheOperationInvocationContext&lt;?&gt; cacheOperationInvocationContext) {
          if (cacheOperationInvocationContext.getTarget() instanceof NewKid) {
              return newKidCacheManager.getCacheNames();
          } else {
              return getCacheManager().getCacheNames();
          }
      }
  }
  
  </code></pre>
  
  <p>If I use @EnableCaching annotation I am stuck with a SimpleCacheResolver, that completely disregards my CustomResolver, albeit annotated with @Primary @Bean.
  This causes this exception:</p>
  
  <pre><code>
      No cache could be resolved for 'Builder[public java.lang.String hello.NewKid.whatsYourName(java.lang.String)] caches=[] | key='' | keyGenerator='' | cacheManager='new.kid' | cacheR
      esolver='' | condition='' | unless='' | sync='false'' using resolver 'org.springframework.cache.interceptor.SimpleCacheResolver@171e2c13'. At least one cache should be provided per cache operation.
  
  </code></pre>
  
  <p>If I remove @EnableCaching, the correct CustomResolver is loaded, however, of course @Cacheable is completely disregarded, so method results are not cached</p>
  
  <p>Am I doing something wrong, or one simply cannot use both EnableCaching and multiple CaceManagers with custom resolver ? How does it happen I am always stuck with SimpleCacheResolver even I have another CacheResolver bean marked as @Primary ? 
  I would be happy if anyone could point me in the right direction</p>