Class JCacheXAutoConfiguration


  • @Configuration
    @ConditionalOnClass({io.github.dhruv1110.jcachex.impl.DefaultCache.class,org.springframework.cache.CacheManager.class})
    @ConditionalOnProperty(prefix="jcachex",
                           name="enabled",
                           havingValue="true",
                           matchIfMissing=true)
    @EnableConfigurationProperties(JCacheXProperties.class)
    @AutoConfigureBefore(name="org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration")
    public class JCacheXAutoConfiguration
    extends Object
    Spring Boot auto-configuration for JCacheX.

    This configuration class automatically sets up JCacheX caching components when the library is present on the classpath. It integrates seamlessly with Spring Boot's auto-configuration system and provides sensible defaults for most use cases.

    Auto-Configuration Features:

    • Automatic Cache Manager: Creates JCacheXCacheManager bean automatically
    • Property Configuration: Binds configuration from application properties
    • Cache Factory: Provides JCacheXCacheFactory for programmatic use
    • Health Indicators: Registers cache health indicators if Spring Boot Actuator is present
    • Metrics Integration: Integrates with Micrometer metrics if available
    • Graceful Degradation: Falls back gracefully when dependencies are missing

    Configuration Properties:

    All configuration is done through application properties under the jcachex prefix:

    
     # Enable/disable JCacheX (default: true)
     jcachex.enabled=true
    
     # Default cache configuration
     jcachex.default.maximumSize=1000
     jcachex.default.expireAfterSeconds=1800
     jcachex.default.enableStatistics=true
    
     # Cache-specific configuration
     jcachex.caches.users.maximumSize=5000
     jcachex.caches.users.expireAfterSeconds=3600
     jcachex.caches.users.evictionStrategy=LRU
    
     jcachex.caches.sessions.maximumSize=10000
     jcachex.caches.sessions.expireAfterSeconds=1800
     jcachex.caches.sessions.evictionStrategy=LFU
     

    Conditional Configuration:

    Components are only created when their dependencies are available:

    • Core Components: Always created when JCacheX is enabled
    • Health Indicators: Only when Spring Boot Actuator is present
    • Metrics: Only when Micrometer is present
    • JMX: Only when JMX is enabled

    Customization:

    You can customize the auto-configuration by providing your own beans:

     {
         @code
         @Configuration
         public class CustomCacheConfig {
    
             @Bean
             @Primary
             public JCacheXCacheManager customCacheManager() {
                 JCacheXCacheManager manager = new JCacheXCacheManager();
                 manager.setAllowNullValues(false);
                 return manager;
             }
    Since:
    1.0.0
    See Also:
    JCacheXProperties, JCacheXCacheManager, JCacheXCacheFactory
    • Constructor Detail

      • JCacheXAutoConfiguration

        public JCacheXAutoConfiguration​(JCacheXProperties properties)
        Creates a new auto-configuration with the specified properties.
        Parameters:
        properties - the JCacheX properties
    • Method Detail

      • jcacheXCacheManager

        @Bean("jcacheXCacheManager")
        @Primary
        public JCacheXCacheManager jcacheXCacheManager()
        Creates the primary JCacheX cache manager. This cache manager integrates with Spring's caching abstraction and provides all the advanced features of JCacheX including multiple eviction strategies, async operations, and comprehensive metrics.
        Returns:
        configured JCacheXCacheManager instance
      • configureCaches

        @Bean
        @ConditionalOnProperty(prefix="jcachex",
                               name="autoCreateCaches",
                               havingValue="true",
                               matchIfMissing=true)
        public Map<String,​io.github.dhruv1110.jcachex.Cache<Object,​Object>> configureCaches()
        Creates individual cache instances based on configuration. This method creates cache beans for each named cache defined in the configuration. These beans can be injected directly or accessed through the cache manager.
        Returns:
        map of cache name to cache instance
      • jcacheXCacheFactory

        @Bean("jcacheXCacheFactory")
        @ConditionalOnMissingBean(JCacheXCacheFactory.class)
        public JCacheXCacheFactory jcacheXCacheFactory()
        Creates a cache factory bean for programmatic cache creation. This factory allows creating caches at runtime with custom configurations while still respecting the global configuration defaults.
        Returns:
        the cache factory
      • evictionStrategyFactory

        @Bean
        @ConditionalOnMissingBean(EvictionStrategyFactory.class)
        public EvictionStrategyFactory evictionStrategyFactory()
        Creates eviction strategy factory for different eviction algorithms. This factory creates appropriate eviction strategy instances based on string configuration values, making it easy to configure via properties.
        Returns:
        the eviction strategy factory
      • cacheConfigurationValidator

        @Bean
        @ConditionalOnMissingBean(CacheConfigurationValidator.class)
        public CacheConfigurationValidator cacheConfigurationValidator()
        Creates a cache configuration validator. This validator ensures that cache configurations are valid and provides helpful error messages for common misconfigurations.
        Returns:
        the configuration validator
      • jcacheXAnnotationsAspect

        @Bean
        @ConditionalOnClass(name={"org.aspectj.lang.annotation.Aspect","org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator"})
        @ConditionalOnMissingBean(JCacheXAnnotationsAspect.class)
        public JCacheXAnnotationsAspect jcacheXAnnotationsAspect​(org.springframework.beans.factory.BeanFactory beanFactory)
        Registers the AOP aspect that powers @JCacheXCacheable/@JCacheXCacheEvict if Spring AOP is present.