Programming & Development / April 12, 2025

Detect and Log CacheManager Provider in Spring Boot (Java Version)

Spring Boot CacheManager cache provider detection Spring cache auto-configuration caching in Java Spring Boot logging cache provider priority Java code Spring Boot cache

📦 Java Code:

java

package com.example.cache;

import org.springframework.boot.CommandLineRunner;
import org.springframework.cache.CacheManager;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class CacheProviderDetector implements CommandLineRunner {

    private final ApplicationContext context;

    public CacheProviderDetector(ApplicationContext context) {
        this.context = context;
    }

    @Override
    public void run(String... args) {
        // Check if a CacheManager bean is defined
        if (context.containsBean("cacheManager")) {
            CacheManager cacheManager = context.getBean(CacheManager.class);
            String className = cacheManager.getClass().getName();

            System.out.println("✅ Detected CacheManager: " + className);
            detectProviderFromClass(className);
        } else {
            System.out.println("⚠️ No CacheManager bean defined in context.");
        }
    }

    private void detectProviderFromClass(String className) {
        String provider;

        if (className.contains("JCache")) {
            provider = "JCache (JSR-107) – e.g., EhCache 3, Hazelcast, Infinispan";
        } else if (className.contains("Hazelcast")) {
            provider = "Hazelcast";
        } else if (className.contains("Infinispan")) {
            provider = "Infinispan";
        } else if (className.contains("Couchbase")) {
            provider = "Couchbase";
        } else if (className.contains("Redis")) {
            provider = "Redis";
        } else if (className.contains("Caffeine")) {
            provider = "Caffeine";
        } else if (className.contains("Cache2k")) {
            provider = "Cache2k";
        } else if (className.contains("ConcurrentMap")) {
            provider = "Simple (ConcurrentMap-based default)";
        } else {
            provider = "Unknown (Custom or unrecognized provider)";
        }

        System.out.println("🔍 Cache Provider Detected: " + provider);
    }
}

What this does:

  • Runs automatically on application startup.
  • Checks if a CacheManager is defined in the Spring context.
  • Extracts the class name of the actual CacheManager bean.
  • Logs a human-readable cache provider type based on known class name patterns.

📝 Usage:

  • Add this class to any Spring Boot project with caching enabled.
  • It helps understand what caching provider is auto-configured, which is useful for debugging or learning Spring Boot’s caching behavior.



Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex