spring-boot + Ehcache without XML

http://stackoverflow.com/questions/21944202/using-ehcache-in-spring-4-without-xml

1、Ehcache配置类

@Configuration

@EnableCaching

public class CachingConfig implements CachingConfigurer {

    @Bean(destroyMethod="shutdown")

    public net.sf.ehcache.CacheManager ehCacheManager() {

        CacheConfiguration cacheConfiguration = new CacheConfiguration();

        cacheConfiguration.setName("myCacheName");

        cacheConfiguration.setMemoryStoreEvictionPolicy("LRU");

        cacheConfiguration.setMaxEntriesLocalHeap(1000);



        net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();

        config.addCache(cacheConfiguration);



        return net.sf.ehcache.CacheManager.newInstance(config);

    }



    @Bean

    @Override

    public CacheManager cacheManager() {

        return new EhCacheCacheManager(ehCacheManager());

    }



    @Bean

    @Override

    public KeyGenerator keyGenerator() {

        return new SimpleKeyGenerator();

    }

}

2、调试简单配置类

@Configuration

@EnableCaching

public class CachingConfig implements CachingConfigurer {

    @Bean

    @Override

    public CacheManager cacheManager() {

        SimpleCacheManager cacheManager = new SimpleCacheManager();



        List<Cache> caches = new ArrayList<Cache>();

        caches.add(new ConcurrentMapCache("myCacheName"));

        cacheManager.setCaches(caches);



        return cacheManager;

    }



    @Bean

    @Override

    public KeyGenerator keyGenerator() {

        return new SimpleKeyGenerator();

    }

}

 

你可能感兴趣的:(spring-boot)