javauser
javauser

Reputation: 101

Not able to plug-in a custom cache manager via shared library to Spring Boot Service

I am trying to add caching behaviour to the services using Spring @cacheable annotation and by using a custom cachemanager as part of configuration to enable/disable caching at runtime.

My custom configuration class works fine when used within the service project. But, when I make it part of a shared library and reference it in the service the caching behaviour does not work.

So, I have a shared library which is now referenced by my service. I want to use the shared library to so that I can change the cacheManager strategy by changing to some other cache manager without impacting the service client.

But, now caching does not work.Within the same service it works, but not as a shared library.

I am not sure if some other cache configuration is getting configured automatically or not.

I want to understand if I need to exclude some cache configuration classes or configure my configuration before some other configuration is getting created.

My configuration class is as below. I am using a string object delimited by (,) to create the caches in the class from application configuration.

@Configuration
@EnableCaching
@RefreshScope
@AutoConfigureBefore(CacheAutoConfiguration.class)
public class MyCustomCacheConfig {

/* Flag to Determine if Cache should be Enabled or not for the service */
    @Value("${cacheEnabled}")
    private boolean cacheEnabled;

    /* Name of the caches to be created */
    @Value("${caches}")
    private String caches;


    @Bean
    @Primary
    @RefreshScope
    CacheManager cacheManager() {


        //If Cache is Enabled then swap the cacheManager for SimpleCacheManager 
        if (cacheEnabled) {
        List<String> cacheNameList = Arrays.asList(caches.split(","));
        SimpleCacheManager cacheManager = new SimpleCacheManager();

        List<ConcurrentMapCache> conMapList = new ArrayList<>();
        for (int i = 0; i < cacheNameList.size(); i++) {
            conMapList.add(new ConcurrentMapCache(cacheNameList.get(i)));
        }

        cacheManager.setCaches(conMapList);

        return cacheManager;
        }else{
            CacheManager cacheManager = new NoOpCacheManager();
            return cacheManager;
        }


    }
}

I am using the standard spring boot autoconfiguration approach by having the below entry in path src/main/resources/META-INF/spring.factories of my shared library component.

**org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.demo.autoconfigure.MyCustomCacheConfig**

And so, I expect the configuration will be picked up on startup when the service starts up. I have the library added as a dependency to the service pom.xml

Since I have multiple services to which I want to have this feature of switching cache on/off at run-time,I thought I can make the configuration as a shared library.

I also have shared the complete repository including the service, the caching-component,demo config service and the file base repo in the git repo below,

https://github.com/bsridhar123/cache-demo-repo

Can someone help me in identifying the cause of the issue.

Upvotes: 0

Views: 1127

Answers (2)

visionken
visionken

Reputation: 85

In my case, It dosen't work unless I set the application.properties to:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration

Upvotes: 0

javauser
javauser

Reputation: 101

I got the solution to my issue.

When I added an entry as below in application.properties in the shared library component and built it and referenced it in my service it worked fine.

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration

I also removed the @AutoConfigureBefore annotation I had earlier in my configuration class ins shared library.

Seems like explicitly excluding the autoconfiguration class rather than using the annotatio works, though I had to add a application.properties to my library.

Upvotes: 0

Related Questions