Spring Cache - Using CachingConfigurer to register Custom KeyGenerator globally

[Last Updated: Mar 21, 2018]

In the last example we saw how to use a custom KeyGenerator by using CacheConfig#keyGenerator. In this example, we will see how to register our custom KeyGenerator globally by implementing CachingConfigurer interface or by extending its adapter class CachingConfigurerSupport.


Java Config

CachingConfigurer interface is to be implemented by @Configuration classes annotated with @EnableCaching. It provides various methods to configure or customize caching abstraction.

public class AppConfig extends CachingConfigurerSupport {

  public KeyGenerator keyGenerator() {
      return new MyCustomKeyGenerator();

  public CacheManager cacheManager() {
      List<ConcurrentMapCache> caches = Arrays.asList(new ConcurrentMapCache("employee-cache"));
      SimpleCacheManager cacheManager = new SimpleCacheManager();
      return cacheManager;

We are also overriding cacheManager() method to register our cache provider.

The Custom KeyGenerator

package com.logicbig.example;

import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;

public class MyCustomKeyGenerator implements KeyGenerator {
  public Object generate(Object target, Method method, Object... params) {
      StringBuilder sb = new StringBuilder();

      if (params != null) {
          for (Object param : params) {
      return sb.toString();

A service bean

As compare to the last example, we don't need to use @CacheConfig#keyGenerator or @Cacheable#keyGenerator anymore.

@CacheConfig(cacheNames = "employee-cache")
public class EmployeeService {

  public Employee getEmployeeById(int id) {
      System.out.println("getEmployeeById() invoked");
      return new Employee(id, "Adam", "IT");

  public Employee getEmployeeByNameAndDept(String name, String dept) {
      System.out.println("getEmployeeByNameAndDept() invoked");
      return new Employee(20, name, dept);

The main class

public class ExampleMain {

  public static void main(String[] args) {
      AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
      EmployeeService employeeService = context.getBean(EmployeeService.class);
      System.out.println("-- getting employee by id --");
      Employee employee = employeeService.getEmployeeById(10);
      System.out.println("employee returned: " + employee);

      System.out.println("-- getting employee by name and dept --");
      employee = employeeService.getEmployeeByNameAndDept("Linda", "Admin");
      System.out.println("employee returned: " + employee);


  public static void printNativeCache(ApplicationContext context) {
      Cache cache = context.getBean(CacheManager.class).getCache("employee-cache");
      System.out.println("-- native cache --");
      Map<String, Object> map = (Map<String, Object>) cache.getNativeCache();
      map.forEach((key, value) -> {
          System.out.println(key + " = " + value);
-- getting employee by id --
getEmployeeById() invoked
employee returned: com.logicbig.example.Employee@6dc17b83
-- getting employee by name and dept --
getEmployeeByNameAndDept() invoked
employee returned: com.logicbig.example.Employee@5e0826e7
-- native cache --
EmployeeService-getEmployeeById-Integer:10 = com.logicbig.example.Employee@6dc17b83
EmployeeService-getEmployeeByNameAndDept-String:Linda-String:Admin = com.logicbig.example.Employee@5e0826e7

Example Project

Dependencies and Technologies Used:

  • spring-context 5.0.4.RELEASE: Spring Context.
  • JDK 1.8
  • Maven 3.3.9

Using CachingConfigurer to register custom KeyGenerator Select All Download
  • spring-using-caching-configurer
    • src
      • main
        • java
          • com
            • logicbig
              • example

    See Also