0

I have Two CacheManagers defined in different places (by different teams):

eg:

@Configuration
@EnableCaching
public ConfigClassOne {

        @Bean
        public CacheManager myCacheManager() {
            return new ConcurrentMapCacheManager("someCacheName");
        }
}

and elsewhere in the code (that my app consumes but is not responsible for):

@Configuration
@EnableCaching
public class CacheConfiguration {

        @Bean
        public CacheManager otherCacheManager () {
            return new ConcurrentMapCacheManager(CacheConstants.SOME_CACHE_NAME,
                                                 CacheConstants.SOME_OTHER_NAME);
        }
}

I get unsatisfied bean exception when trying to do getBean because there are two CacheManager.class results.

The problem is, because this is external to me, I do not want to rely on them making the cache, nor set mine to primary as they may receive it.

I've tried setting bean name, bean qualifier but all result in either no results or 2 results (both exceptions).

I tried creating a CompositeManager (primary) that takes theirs on with ours but this is not an ideal solution..

Even stranger, when running integration test I see the following:

In debug I can see that both autowired beans (classes that use them) on each side (mine and theirs) get their respective managers correctly even without primary, but the test fails because at some point it runs some refresh when autowiring to another class the class that holds the bean instance and THEN it tries to do getbean and fails (perhaps something with mockito?)

Is there a way to resolve this issue?

GilMos
  • 1
  • 2
  • Qualifier should work here. Using @Autowired@Qualifier("myCacheManager") will get the right bean . Please share the code where this bean is used – R.G Feb 11 '20 at 08:58
  • Qualifier works but not when used with context.getBean(CacheManager.class) which causes exception – GilMos Feb 11 '20 at 09:00
  • code eg: others (external library): '@Autowired public CacheHelper(@Qualifier("theirManagerName") CacheManager cacheManager) { this.cacheManager = cacheManager; }' mine: 'CacheableService (@Autowired @Qualifier("myCacheName") CacheManager cacheManager) { cache = ((ConcurrentMapCacheManager)cacheManager).getCache("cachename"); }' – GilMos Feb 11 '20 at 09:09
  • I assume you have shared the code that works .Please update the question with this code and explanation . Also [context.getBean("BeanName",Type)](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/support/AbstractApplicationContext.html#getBean-java.lang.String-java.lang.Class-) or context.getBean("beanName") can be used if it helps . – R.G Feb 11 '20 at 09:17
  • Hi, it doesn't work, I shared what you asked for (the code where the bean is used). I can't change the getbean, it uses the type automatically when loading via the autowiring of object CTOR that contain my cache... this is where I fall.. – GilMos Feb 11 '20 at 10:01
  • When there is _2 results_ , the exception will have the to bean names found. What happens when you use that ? Can you please share that exception trace ? – R.G Feb 11 '20 at 10:05

0 Answers0