How to Integrate EhCache into Spring Boot for Faster Data Access
This article explains how Spring Boot automatically detects cache providers, shows how to replace the default ConcurrentHashMap with EhCache by adding the dependency, configuring ehcache.xml, and verifying the CacheManager type through unit tests, providing complete code examples and step‑by‑step instructions.
In the previous article we learned how Spring Boot uses in‑process caching to speed up data access. Spring Boot automatically configures a suitable @EnableCaching CacheManager by checking providers in the following order: Generic, JCache (JSR‑107), EhCache 2.x, Hazelcast, Infinispan, Couchbase, Redis, Caffeine, Simple.
We can also force a specific provider with the spring.cache.type property or inspect the cacheManager bean during debugging. If no third‑party implementation is specified, Spring Boot falls back to a ConcurrentHashMap cache.
Using EhCache
This guide shows how to switch the in‑process cache to EhCache in a Spring Boot application.
We start from a basic example that includes three parts:
User entity definition
@Entity
@Data
@NoArgsConstructor
public class User {
@Id
@GeneratedValue
private Long id;
private String name;
private Integer age;
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
}User repository with cache annotations
@CacheConfig(cacheNames = "users")
public interface UserRepository extends JpaRepository<User, Long> {
@Cacheable
User findByName(String name);
}Test case to verify CacheManager
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class Chapter51ApplicationTests {
@Autowired
private UserRepository userRepository;
@Autowired
private CacheManager cacheManager;
@Test
public void test() throws Exception {
userRepository.save(new User("AAA", 10));
User u1 = userRepository.findByName("AAA");
System.out.println("First query: " + u1.getAge());
User u2 = userRepository.findByName("AAA");
System.out.println("Second query: " + u2.getAge());
}
}Step 1 : Add the EhCache dependency to pom.xml.
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>When using Spring Boot’s parent pom, the version is managed automatically.
Step 2 : Create ehcache.xml under src/main/resources:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd">
<cache name="users"
maxEntriesLocalHeap="200"
timeToLiveSeconds="600"/>
</ehcache>Run the test in debug mode; the CacheManager should now be an instance of org.springframework.cache.ehcache.EhCacheCacheManager. Adding a print statement confirms this:
System.out.println("CacheManager type : " + cacheManager.getClass());Sample test output:
CacheManager type : class org.springframework.cache.ehcache.EhCacheCacheManager
... (SQL logs) ...
First query: 10
Second query: 10The first line shows the CacheManager is now EhCacheCacheManager instead of ConcurrentHashMap.
The second query produces no SQL, indicating the result was retrieved from the cache.
Integration successful!
Code Repository
All examples are available in the chapter5-2 directory of the following repositories:
GitHub: https://github.com/dyc87112/SpringBoot-Learning/
Gitee: https://gitee.com/didispace/SpringBoot-Learning/
If you find this article helpful, consider starring the repository for support.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
