How to configure a second cache manager?

We need to cache some data retrieved outside from what DataServices already does.

However, I can’t get a secondary CacheManager to not interfere with the cache setup from dataservices.

When I configure a secondary one like this:

@Bean(name = "myAppCacheManager")
@Order(Ordered.LOWEST_PRECEDENCE)
public CacheManager myAppCacheManager() {
    CaffeineCacheManager manager = new CaffeineCacheManager("myCacheName");
    manager.setCaffeine(...)
    return manager;
}

However, when I start DataServices, it fails with a lot of errors like this one:

2026-03-05 15:18:37,378 [  restartedMain] [WARN ] c.m.a.d.m.b.BulkImportProblemReporter    : Error while importing models
java.lang.IllegalArgumentException: Cannot find cache named 'com.mgmtp.a12.dataservices.model.GenericModel' for Builder[public com.mgmtp.a12.dataservices.model.GenericModel com.mgmtp.a12.dataservices.model.persistence.GenericModelReadRepository.readModel(java.lang.String)] caches=[com.mgmtp.a12.dataservices.model.GenericModel] | key='' | keyGenerator='' | cacheManager='' | cacheResolver='' | condition='' | unless='' | sync='false'

It seems that DS tries to use the alternate cache manager despite the fact that it is not the “default” cache manager.

Any ideas?

You could create another application and configure it to your needs appropriately. Use the original application context of dataservices as the parent. Then all beans in the original application context will work as usual. But by specifying a cachemanager in the second application context this one will take precedence over the one from the original dataservices application context and hence your beans in the second application context will use your one.

Maybe spring modulith can make the setup easier, but I am not sure.

Or maybe this can help: Using Multiple Cache Managers in Spring | Baeldung

Example Implementation:

@Bean(name = "myAppCacheManager")
    @Order(Ordered.LOWEST_PRECEDENCE)
    public CacheManager cacheManager(HazelcastInstance hazelcastInstance) {
        CompositeCacheManager manager = new CompositeCacheManager();
        manager.setCacheManagers(List.of(new HazelcastCacheManager(hazelcastInstance), new CaffeineCacheManager("myCacheName")));
        return manager;
    }

Thanks, that works.