Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What is JCache (JSR-107), how does Ehcache 3 relate to it, and how do you configure Spring Boot to use Ehcache 3 through JCache? What happened to Ehcache 2?

JCache, defined by JSR-107, is a standard Java caching API in the javax.cache package with its own CacheManager, Cache, expiry policies, listeners and annotations, and Ehcache 3 is one of its implementations. In Spring Boot we add org.ehcache:ehcache (with the jakarta classifier) and javax.cache:cache-api, point spring.cache.jcache.config at an ehcache.xml, and Boot auto-configures a JCacheCacheManager. Ehcache 2, the net.sf.ehcache library, is legacy and not supported by Spring Boot 3, so applications move to Ehcache 3, Caffeine or a distributed cache.

Why this question is asked

Many Spring applications started with Ehcache years ago, and the upgrade to Spring Boot 3 is often when teams discover that the cache they relied on is no longer supported. Interviewers ask this question to check that we know the difference between a standard API and its implementations, how Spring Boot picks a cache provider, and what the migration options are. In this answer, we will recap what JCache standardises, see how Ehcache 3 fits in, configure it in Spring Boot step by step, look at Hibernate's second-level cache, and explain what happened to Ehcache 2.

1A Quick Recap of JCache

JCache is the short name for JSR-107, a Java specification for caching. Like JDBC for databases, it defines interfaces and leaves the implementation to providers. Its package is javax.cache, and it has kept that name even in the Jakarta era, so the API jar is javax.cache:cache-api.

The main types are simple. A CachingProvider is the entry point for one implementation, a CacheManager owns a set of named caches, and a Cache is a map-like object with get, put, remove and similar methods. Each cache can have an ExpiryPolicy, for example expire 10 minutes after creation, and listeners that are called when entries are created, updated, removed or expire. JCache also defines annotations such as @CacheResult, @CachePut and @CacheRemove, which are its own and differ from Spring's @Cacheable family, although Spring can process both.

Java
CachingProvider provider = Caching.getCachingProvider(); // finds the provider on the classpath, e.g. Ehcache 3
CacheManager manager = provider.getCacheManager();
MutableConfiguration<Long, String> cfg = new MutableConfiguration<Long, String>()
.setTypes(Long.class, String.class)
.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.TEN_MINUTES));
Cache<Long, String> names = manager.createCache("names", cfg);
names.put(1L, "Ada");
System.out.println(names.get(1L)); // Ada

Notice that nothing in this code names Ehcache. That is the point of a standard: we could swap the provider jar for another JCache implementation, such as Hazelcast or Infinispan, without changing this code.

2How Ehcache 3 Relates to JCache

Ehcache 3 is a complete rewrite of Ehcache with a new package, org.ehcache, and it implements JCache. We can use it through its own native API, which exposes all its features, or through JCache, which is portable but exposes only the standard part. Ehcache 3's main extra features are its storage tiers: entries can live on the heap, off the heap (in memory the garbage collector does not manage, which keeps big caches from slowing down GC) and on local disk. Its XML configuration describes those tiers per cache.

3Configuring Spring Boot to Use Ehcache 3 Through JCache

Spring Boot looks at the classpath to decide which cache provider to configure. When it finds a JCache provider and the cache API, it creates a JCacheCacheManager, which adapts the JCache CacheManager to Spring's own Cache abstraction. So we need three things: the dependencies, @EnableCaching, and the provider's configuration file.

XML
<!-- pom.xml -->
<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>
<classifier>jakarta</classifier> <!-- built against jakarta.* namespaces -->
</dependency>
# application.properties
spring.cache.jcache.config=classpath:ehcache.xml
<!-- ehcache.xml -->
<config xmlns="http://www.ehcache.org/v3">
<cache alias="products">
<key-type>java.lang.Long</key-type>
<value-type>com.example.Product</value-type>
<expiry><ttl unit="minutes">10</ttl></expiry>
<resources>
<heap unit="entries">10000</heap>
<offheap unit="MB">64</offheap>
</resources>
</cache>
</config>
Step throughHow a @Cacheable call reaches Ehcache 3
  1. @Cacheable("products")Spring's cache interceptor
  2. JCacheCacheManagerSpring adapter, auto-configured by Boot
  3. javax.cache CacheManagerthe standard JCache API
  4. Ehcache 3provider: heap, off-heap and disk tiers from ehcache.xml

4 layers, from the top down. Step through them one at a time.

The jakarta classifier matters because some Ehcache 3 classes, for example its JAXB-based XML parsing, depend on Java EE or Jakarta EE packages, and Spring Boot 3 uses the jakarta.* names. Without the classifier we can get class-not-found errors at startup; check your Ehcache version, since newer releases may handle this differently.

With this in place, a method with @Cacheable("products") uses the products cache defined in the XML. If we have more than one JCache provider on the classpath, we tell Boot which one with spring.cache.jcache.provider, and spring.cache.type=jcache forces the choice if another cache library is also present.

4Hibernate's Second-Level Cache Through JCache

Hibernate's second-level cache keeps entities between sessions so that repeated loads of the same row skip the database. Hibernate does not implement this cache itself; it talks to a provider. The hibernate-jcache module lets it use any JCache provider, so the same Ehcache 3 setup can serve it. We set hibernate.cache.region.factory_class to jcache, turn on the second-level cache, and give each entity region a cache configuration in the provider's XML.

5What Happened to Ehcache 2

Ehcache 2 lived in the net.sf.ehcache package and had its own API, configured with an ehcache.xml in a different format. It does not implement JCache in the same way and is no longer developed for modern platforms. Spring Framework 6 removed its Ehcache 2 support classes, and Spring Boot 3 no longer auto-configures it. Hibernate 6 also dropped its Ehcache 2 integration.

So an application moving to Spring Boot 3 has to migrate. Ehcache 3 is the closest match when it needs off-heap or disk tiers; Caffeine is the common choice for a simple, fast heap cache; and a distributed cache such as Redis fits when several instances must share data. The old XML has to be translated, because the formats and setting names differ.

6Follow-up questions

Which is better for a simple local cache in Spring Boot: Ehcache 3 or Caffeine?

For a simple heap cache, Caffeine is usually the better choice. It is small, very fast, has a good eviction algorithm, and Spring Boot configures it from a single property such as spring.cache.caffeine.spec=maximumSize=10000,expireAfterWrite=10m. Ehcache 3 is worth its extra setup when we need off-heap storage for large caches, disk persistence, or JCache portability, for example to share one provider with Hibernate's second-level cache.

Can we use JCache annotations like @CacheResult in a Spring application?

Yes. When the JCache API is on the classpath, Spring's caching support also understands the JSR-107 annotations. Mixing both styles in one code base is confusing, though, so most teams pick Spring's @Cacheable family and use JCache only as the provider API.

Is JCache a distributed cache?

No, JCache is only an API. Whether a cache is local or distributed depends on the provider. Ehcache 3 on its own is local to the JVM, while providers such as Hazelcast implement the same API over a cluster.

7Conclusion

In this answer, we saw that JCache is a standard caching API with CacheManager, Cache, expiry policies, listeners and annotations, and that Ehcache 3 is one implementation with heap, off-heap and disk tiers. Spring Boot auto-configures a JCacheCacheManager when the cache API and a provider are on the classpath, and we point it at the provider's file with spring.cache.jcache.config. Hibernate can use the same provider through hibernate-jcache, and Ehcache 2 is legacy and unsupported in Spring Boot 3, so it must be replaced.

What to say in the interviewAbout 70 seconds aloud

Open with

JCache is JSR-107, a standard caching API in the javax.cache package, a bit like JDBC for caches. It defines a CachingProvider, a CacheManager, Cache objects with expiry policies and listeners, and its own annotations like @CacheResult.

Then, if they want more

Ehcache 3 is an implementation of that API, with extra features such as heap, off-heap and disk tiers. To use it in Spring Boot, I add the cache starter, javax.cache cache-api, and org.ehcache ehcache with the jakarta classifier, enable caching, and set spring.cache.jcache.config to classpath:ehcache.xml, where I define each cache with its key and value types, expiry and resources.

Boot sees a JCache provider and auto-configures a JCacheCacheManager, so @Cacheable works on those caches. The same provider can back Hibernate's second-level cache through hibernate-jcache with the jcache region factory.

Ehcache 2, the net.sf.ehcache library, is legacy: Spring Boot 3 and Hibernate 6 no longer support it, so on an upgrade I would move to Ehcache 3 if I need off-heap or disk, to Caffeine for a simple fast local cache, or to Redis if instances must share data.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 3, Hibernate 6, Spring Framework 6
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is JCache (JSR-107), how does Ehcache 3 relate to it, and how do you configure...": https://scrivio-interview-library.netlify.app/databases/caching/15031b32ed/