0

I have a common caching module which has spring boot starter cache (version 2.2.4.RELEASE) and for caching has dependencies of ehcache & caffeine. Below is the pom file

    <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>

    <dependency>
        <groupId>javax.cache</groupId>
        <artifactId>cache-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.ehcache</groupId>
        <artifactId>ehcache</artifactId>
    </dependency>
    <dependency>
        <groupId>com.github.ben-manes.caffeine</groupId>
        <artifactId>caffeine</artifactId>
    </dependency>
</dependencies>

In this module, Autowired the CacheManger and has cache update & get method using spring cache.

@Autowired
private CacheManager cacheManager;

This module is added as dependency in the app1 which is springboot application and as per the property "spring.cache.type", when app1 starts it will initialize the respective cache

For caffeine cache

spring.cache.type=caffeine
spring.cache.caffeine.spec=maximumSize=10000

For Ehcache3

spring.cache.type=caffeine
spring.cache.jcache.provider=org.ehcache.jsr107.EhcacheCachingProvider
spring.cache.jcache.config=classpath:ehcache.xml

This is all working fine for springboot application app1.

Now i am using the same caching module in app2 which has functionality same as app1 but it is not springboot application. In app2 using @Configuration , @ComponentScan to resolve the dependency. Now app2 is being used in app3 which is springboot application. All the other dependencies are resolved and working fine in app3 except the cacheManger. Getting the below error while running the app3

*\r\nAPPLICATION FAILED TO START\r\n***************************\r\n\r\nDescription:\r\n\r\nField cacheManager in commonCacheService required a bean of type 'org.springframework.cache.CacheManager' that could not be found.\r\n\r\nThe injection point has the following annotations:\r\n\t- @org.springframework.beans.factory.annotation.Autowired(required=true)\r\n\r\nThe following candidates were found but could not be injected:\r\n\t- Bean method 'cacheManager' in 'EhCacheCacheConfiguration' not loaded because @ConditionalOnClass did not find required class 'net.sf.ehcache.Cache'\r\n\t- Bean method 'cacheManager' in 'GenericCacheConfiguration' not loaded because Cache org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration unknown cache type\r\n\t- Bean method 'cacheManager' in 'JCacheCacheConfiguration' not loaded because Cache org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration unknown cache type\r\n\t- Bean method 'cacheManager' in 'NoOpCacheConfiguration' not loaded because Cache org.springframework.boot.autoconfigure.cache.NoOpCacheConfiguration unknown cache type\r\n\t- Bean method 'cacheManager' in 'SimpleCacheConfiguration' not loaded because Cache org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration unknown cache type\r\n\r\n\r\nAction:\r\n\r\nConsider revisiting the entries above or defining a bean of type 'org.springframework.cache.CacheManager' in your configuration.\r\n"}

It is working if i create bean in common cache module like below

@Bean
@ConditionalOnProperty(name="spring.cache.type", havingValue="caffeine")
@Primary
public CacheManager cacheManager() {
    CaffeineCacheManager cacheManager = new CaffeineCacheManager();
    cacheManager.setCaffeine(Caffeine.newBuilder().maximumSize(125000));
    return cacheManager;
}

But i want to avoid the bean creation in common module. Is there any annotation/configuration required in app2 (without changing app3 as don't have any update control on app3)so that it will also work in same way as it is working in app1 )?

Aky
  • 1
  • 1
  • 2

1 Answers1

0

I know it is quite late, but I faced the same issue. It was resolved by adding dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
    <groupId>javax.cache</groupId>
     <artifactId>cache-api</artifactId>
     <version>1.1.1</version>
</dependency>

And add

@EnableCaching

in the SpringBootApplciation Class

irshad.ahmad
  • 276
  • 3
  • 9
  • 24