我需要按名称获取特定的EhCache实例,如果可能的话我更愿意自动装配.鉴于以下自动配置的控制器,我如何在我正在寻找的缓存实例中自动装配?
@Controller
public class MyUniqueService {
...
}
Run Code Online (Sandbox Code Playgroud)
<beans ...>
<ctx:component-scan base-package="my.controllers"/>
<mvc:annotation-driven />
</beans>
Run Code Online (Sandbox Code Playgroud)
如何在我的应用程序上下文中配置EhCache?我没有看到来自EhCache的任何关于它在我的/WEB-INF/目录中加载ehcache.xml文件的日志消息.如何加载它?
如何将EhCache与我的Spring应用程序集成,以便ehcache.xml从我的/WEB-INF/目录中加载文件并通过给定名称将缓存自动装入我的MyUniqueService控制器?
jeh*_*eha 17
首先,您需要在应用程序上下文中创建一个Ehcache CacheManager单例,如下所示:
<bean id="myEhCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:my-ehcache.xml"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
这里configLocation设置为从classpath或use加载value="/WEB-INF/my-ehcache.xml".
在您的控制器中只需注入CacheManager实例:
@Controller
public class MyUniqueService {
@Resource(name="myEhCacheManager")
private CacheManager cacheManager;
...
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您想要"完全自动装配"的路线,请执行以下操作:
<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>
Run Code Online (Sandbox Code Playgroud)
像这样设置你的课程:
@Controller
public class MyUniqueService {
@Autowired
private org.springframework.cache.CacheManager cacheManager;
public org.springframework.cache.Cache getUniqueObjectCache() {
return cacheManager.getCache("uniqueObjectCache");
}
}
Run Code Online (Sandbox Code Playgroud)
uniqueObjectCache对应于ehcache.xml缓存定义中的此缓存实例:
<cache name="uniqueObjectCache"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LRU"
transactionalMode="off"/>
Run Code Online (Sandbox Code Playgroud)
没有办法注入实际的缓存实例,但如上所示,您可以注入缓存管理器并使用它来获取您感兴趣的缓存.
pet*_*syn 17
假设您已定义cacheManager:
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:/ehcache.xml"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
您可以像这样获取/注入特定的缓存:
@Value("#{cacheManager.getCache('myCacheName')}")
private Cache myCache;
Run Code Online (Sandbox Code Playgroud)
如果您有兴趣,请参阅@Value() http://www.mkyong.com/spring3/spring-el-method-invocation-example/中如何使用Spring EL的示例.
Bry*_*sen 11
如果上下文可以找到具有正确类的bean,您也可以使用autowire.这是我配置我的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>
Run Code Online (Sandbox Code Playgroud)
而我的java类
@Autowired
private net.sf.ehcache.Cache cache;
Run Code Online (Sandbox Code Playgroud)
这个设置适合我.
确实!或者如果你想使用java配置类:
@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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
41120 次 |
| 最近记录: |