问题
I'm trying to configure an instance of JBossCache using a Spring config file (for eventual use in Tomcat). I don't see any examples online and trying to figure out the mapping between the sample JBoss Microcontainer format and Spring IoC.
Does anyone have any example Spring configs for JBoss Cache?
回答1:
One of the very appealing aspects of JBossCache (v3, at any rate) is that the API consists mainly of JavaBean-compliant classes. This makes them very easy to wire up in Spring.
The JBoss MicroContainer format isn't doing anything special with it, it's all POJO setter and constructor injection. So, rather than trying to translate JBossMC syntax into Spring, just look directly at the classes themselves. The JBossCache docs also contain plenty examples of programmatic configuration.
Here's an example from my app that uses Spring 3 @Bean
-style config. It's easy enough to translate into XML synyax, but this is much nicer:
@Bean(destroyMethod="stop")
public <K,V> Cache<K, V> csiCache() {
org.jboss.cache.config.Configuration cacheConfiguration = new org.jboss.cache.config.Configuration();
cacheConfiguration.setCacheMode(CacheMode.REPL_ASYNC);
cacheConfiguration.setTransactionManagerLookupClass(JBossTransactionManagerLookup.class.getName());
cacheConfiguration.setClusterName(cacheClusterName);
cacheConfiguration.setEvictionConfig(new EvictionConfig(new EvictionRegionConfig(
Fqn.ROOT, new ExpirationAlgorithmConfig()
)));
return new DefaultCacheFactory<K, V>().createCache(cacheConfiguration, true);
}
来源:https://stackoverflow.com/questions/4502992/spring-configuration-for-jbosscache