Getting an EhCache instance with Spring… intelligently

前端 未结 4 1791
情深已故
情深已故 2020-12-09 00:29

I need to get a specific EhCache instance by name and I\'d prefer to autowire if possible. Given the following automatically configured controller, how can I autowire in the

相关标签:
4条回答
  • Assuming you have cacheManager defined:

    <bean id="cacheManager"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:/ehcache.xml"/>
    </bean>
    

    You can get/inject specific cache like this:

    @Value("#{cacheManager.getCache('myCacheName')}")
    private Cache myCache;
    

    See also examples how to use Spring EL inside the @Value() http://www.mkyong.com/spring3/spring-el-method-invocation-example/ if you are interested.

    0 讨论(0)
  • 2020-12-09 00:58

    Indeed! Or if you want to use a java config class:

            @Inject
            private ResourceLoader resourceLoader;
    
            @Bean
            public CacheManager cacheManager() {
                EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager();
                try {
                    ehCacheCacheManager.setCacheManager(ehcacheCacheManager().getObject());
                } catch (Exception e) {
                    throw new IllegalStateException("Failed to create an EhCacheManagerFactoryBean", e);
                }
                return ehCacheCacheManager;
            }
    
            @Bean
            public FactoryBean<net.sf.ehcache.CacheManager> ehcacheCacheManager() {
                EhCacheManagerFactoryBean bean = new EhCacheManagerFactoryBean();
                bean.setConfigLocation(resourceLoader.getResource("classpath:ehcache.xml"));
                return bean;
            }
    
    0 讨论(0)
  • 2020-12-09 01:04

    First you need to create a Ehcache CacheManager singleton in you app context like this:

    <bean id="myEhCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:my-ehcache.xml"/>
    </bean>
    

    Here configLocation is set to load from classpath or use value="/WEB-INF/my-ehcache.xml".

    In your controller simply inject the CacheManager instance:

    @Controller 
    public class MyUniqueService {
    
        @Resource(name="myEhCacheManager")
        private CacheManager cacheManager;
    
        ...
    }
    

    Alternatively, if you'd like to go the "entirely autowired" route, do:

    <bean class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager">
            <bean class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
                <property name="configLocation" value="/WEB-INF/ehcache.xml"/>
            </bean>
        </property>
    </bean>
    

    Setup your class like so:

    @Controller
    public class MyUniqueService { 
    
        @Autowired
        private org.springframework.cache.CacheManager cacheManager;
    
        public org.springframework.cache.Cache getUniqueObjectCache() {
            return cacheManager.getCache("uniqueObjectCache");
        }
    }
    

    uniqueObjectCache corresponds to this cache instance in your ehcache.xml cache definition:

    <cache name="uniqueObjectCache"
           maxElementsInMemory="10000"
           eternal="false"
           timeToIdleSeconds="300"
           timeToLiveSeconds="600"
           memoryStoreEvictionPolicy="LRU"
           transactionalMode="off"/>
    

    There isn't a way to inject an actual cache instance, but as shown above, you can inject a cache manager and use it to get the cache you're interested in.

    0 讨论(0)
  • 2020-12-09 01:07

    You can also use autowire if the context can find a bean with the correct class. Here is how I configured my xml

    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation">
            <value>WEB-INF/ehcache.xml</value>
        </property>
    </bean>
    
    <bean id="cache" class="net.sf.ehcache.Cache" factory-bean="cacheManager" factory-method="getCache">
        <constructor-arg value="CacheNameHere" />          
    </bean>
    

    And my java class

    @Autowired
    private net.sf.ehcache.Cache cache;
    

    This setup works for me.

    0 讨论(0)
提交回复
热议问题