Spring メソッドのキャッシュと、ehcache によってバックアップされる休止状態のレベル 2 キャッシュの両方をセットアップしたいと思います。
hibernate レベル 2 キャッシュは、spring-context.xml で既に構成されています。
<!-- hibernate level 2 cache configuration -->
<bean id="hibernateCacheManager"
class="net.sf.ehcache.CacheManager"
factory-bean="hibernateCacheManagerFactory"
factory-method="createCacheManager"
destroy-method="shutdown" />
<bean id="hibernateCacheManagerFactory"
class="com.project.CustomEhCacheManagerFactoryBean" />
キャッシュ構成はカスタム xml ファイルから読み取られるため、必要に応じてカスタマイズし、キャッシュされたエンティティを簡単にテストできます。
public class CustomEhCacheManagerFactoryBean {
public CacheManager createCacheManager() {
final Configuration configuration = new Configuration();
// read the xml configuration
//for each node in xml
final CacheConfiguration cache = new CacheConfiguration();
// set the properties
configuration.addCache(cache);
// end for
cacheManager = CacheManager.create(configuration);
}
}
今回はアノテーションを使って別のキャッシュマネージャーでSpringメソッドキャッシングを追加したいと思います。spring-context.xml に以下を追加しました。
<!-- suport method caching through annotation -->
<cache:annotation-driven />
<!-- spring method cache configuration -->
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="springEhcache"/>
</bean>
<bean id="springEhcache"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="cacheManagerName" value="method-cache" />
</bean>
次に、次の例外が発生します。
Caused by:
java.lang.IllegalArgumentException: loadCaches must not return an empty Collection
at org.springframework.util.Assert.notEmpty(Assert.java:268)
at org.springframework.cache.support.AbstractCacheManager.afterPropertiesSet(AbstractCacheManager.java:49)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
Q1
: キャッシュは注釈によって自動的に構成されるべきではありませんか? 私は何か見落としてますか?
Q2
springCacheManager
:代わりにこの Bean ID を指定することは可能cacheManager
ですか? 今のところ例外が発生します: Bean 'cacheManager' が見つかりません
ありがとう