だから、3つのメソッドを持つDAOがあると想像してみましょう-エンティティを検索するための2つとエンティティを保存するための1つ。 適切に機能させるには、インターフェイスとそれを実装するクラスが必要です(そのため、実装に動的プロキシを追加できます)
public interface SampleDAO {
public Sample findById( int id);
public Sample findByUrl( String url);
public void save(Sample sample);
}
public class SampleDAOImpl implements SampleDAO {
public Sample findById( int id) { /* - */ }
public Sample findByUrl( String url) { /* - */ }
public void save(Sample sample) { /* - */ }
}
* This source code was highlighted with Source Code Highlighter .
そして、Springの構成におけるBeanの説明
< bean id ="sampleDAO" class ="SampleDAOImpl" >
< property name ="persistenceManagerFactory" ref ="persistenceManagerFactory" />
</ bean >
* This source code was highlighted with Source Code Highlighter .
ここで、注釈を処理し、メソッド値をキャッシュにマージする何らかの種類のコンポーネントが必要です。 Springがまだ2番目のバージョンだったとき、彼にとっては、これを可能にするspring-modulesライブラリがありました。 プロジェクトはしばらくの間中止されましたが、githubでプロジェクトの開発を開始した人々がいたので、彼らの作業を使用します。
http://github.com/abashev/spring-modulesから最新のソースコードをダウンロードして収集する必要があります。
$ git clone git://github.com/abashev/spring-modules.git
$ cd spring-modules/projects
$ mvn install // , xml
$ cd spring-modules-cache
$ mvn install -DskipTests
* This source code was highlighted with Source Code Highlighter .
そして今、ストリートマジックが始まります:) DAO実装に注釈を追加します
public class SampleDAOImpl {
@Cacheable(modelId = "findByOldId" )
public Sample findById( int id) { /* - */ }
@Cacheable(modelId = "findByUrl" )
public Sample findByUrl( String url) { /* - */ }
@CacheFlush(modelId = "save" )
public void save(Sample sample) { /* - */ }
}
* This source code was highlighted with Source Code Highlighter .
Springコンテキストにキャッシュマネージャーを追加する
<!-- Cache manager for Jsr107 cache -->
< bean id ="cacheManager" class ="org.springmodules.cache.provider.jsr107.Jsr107CacheManagerFactoryBean" />
* This source code was highlighted with Source Code Highlighter .
cacheFacadeオブジェクトは、キャッシュを作成し、そのパラメーターを構成するために使用されます。 キャッシュに特別なプロパティを追加するには(GAEの場合、これはネームスペースと有効期限の設定になります)、「cacheProperties」プロパティをオーバーライドする必要があります。
< bean id ="cacheProviderFacade" class ="org.springmodules.cache.provider.jsr107.Jsr107CacheFacade" >
< property name ="cacheManager" ref ="cacheManager" />
< property name ="cacheProperties" >
< map >
< entry key ="findByOldId" >
< map >
< entry key ="com.google.appengine.api.memcache.jsr107cache.NAMESPACE" value ="findByOldId" />
</ map >
</ entry >
< entry key ="findByUrl" >
< map >
< entry key ="com.google.appengine.api.memcache.jsr107cache.NAMESPACE" value ="findByUrl" />
</ map >
</ entry >
</ map >
</ property >
</ bean >
* This source code was highlighted with Source Code Highlighter .
Springがアノテーションを正しく処理するためには、3つのBeanが必要です-1つの属性コレクター、1つのキャッシングインターセプト、1つのキャッシングアドバイザー @Cachableアノテーションを処理するには
< bean id ="cachingAttributeSource" class ="org.springmodules.cache.annotations.AnnotationCachingAttributeSource" />
< bean id ="cachingInterceptor" class ="org.springmodules.cache.interceptor.caching.MetadataCachingInterceptor" >
< property name ="cacheProviderFacade" ref ="cacheProviderFacade" />
< property name ="cachingAttributeSource" ref ="cachingAttributeSource" />
< property name ="cachingModels" >
< map >
<!-- modelId -->
< entry key ="findByOldId" >
< bean class ="org.springmodules.cache.provider.jsr107.Jsr107CacheCachingModel" >
< constructor-arg value ="findByOldId" />
</ bean >
</ entry >
< entry key ="findByUrl" >
< bean class ="org.springmodules.cache.provider.jsr107.Jsr107CacheCachingModel" >
< constructor-arg value ="findByUrl" />
</ bean >
</ entry >
</ map >
</ property >
</ bean >
< bean id ="cachingAttributeSourceAdvisor" class ="org.springmodules.cache.interceptor.caching.CachingAttributeSourceAdvisor" >
< constructor-arg ref ="cachingInterceptor" />
</ bean >
* This source code was highlighted with Source Code Highlighter .
@CacheFlushを処理するには
< bean id ="flushingAttributeSource" class ="org.springmodules.cache.annotations.AnnotationFlushingAttributeSource" />
< bean id ="flushingInterceptor" class ="org.springmodules.cache.interceptor.flush.MetadataFlushingInterceptor" >
< property name ="cacheProviderFacade" ref ="cacheProviderFacade" />
< property name ="flushingAttributeSource" ref ="flushingAttributeSource" />
< property name ="flushingModels" >
< map >
<!-- modelId -->
< entry key ="save" >
< bean class ="org.springmodules.cache.provider.jsr107.Jsr107CacheFlushingModel" >
< constructor-arg value ="findByOldId, findByUrl" />
</ bean >
</ entry >
</ map >
</ property >
</ bean >
< bean id ="flushingAttributeSourceAdvisor" class ="org.springmodules.cache.interceptor.flush.FlushingAttributeSourceAdvisor" >
< constructor-arg ref ="flushingInterceptor" />
</ bean >
* This source code was highlighted with Source Code Highlighter .
そして最後の仕上げ
< bean class ="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />
* This source code was highlighted with Source Code Highlighter .