2016-08-19 7 views
1

私はSpringキャッシングが初めてです。私はspring-boot-starter-1.4.0.RELEASEを使用しています。私の知る限り、このような何か取る場合、私は、ドキュメントを理解するよう:@Cacheableでアノテートされたメソッドはインターセプトされません。

@Configuration 
@EnableCaching 
public class TestApplication { 
    @Bean 
    public CacheManager cacheManager() { 
     // configure and return an implementation of Spring's CacheManager SPI 
     SimpleCacheManager cacheManager = new SimpleCacheManager(); 
     cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("default"))); 
     return cacheManager; 
    } 

    @Bean 
    public MyService myService() { 
     // configure and return a class having @Cacheable methods 
     return new MyService(); 
    } 

    public static void main(String[] args) { 
     ApplicationContext ctx = SpringApplication.run(TestConfiguration.class); 
     MyService ms = ctx.getBean(MyService.class); 
     ms.doCacheableOperation(); // calls the underlying method 
     ms.doCacheableOperation(); // SHOULD just consult the cache 
    } 
} 

をそして、このようなクラスを持っている:主な方法はMyServce#1にTestApplication、最初の呼び出しで実行されている場合

public class MyService { 
    @Cacheable 
    public String doCacheableOperation() { 
     System.out.println("======================CALLING EXPENSIVE METHOD======================="); 
     return "done"; 
    } 
} 

doCacheableOperationは画面に出力する必要がありますが、結果は最初からキャッシュされるので、2番目の値は使用しないでください。しかし、これは当てはまりません。出力は2回表示されます。

構成コードはEnableCachingのJavadocから持ち上げられる:http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/cache/annotation/EnableCaching.html私をパズルし

ことの一つは、私はデバッグしたMyServiceのインスタンスを検査する場合、それだけで生のオブジェクト、任意のCGLIBに包まれていないということですサブクラスなど

MyService#doCacheableOperationの結果がキャッシュされるように設定/アプローチを変更する必要がありますか?

答えて

2

ああ、少年。それを見つけた。私はSpringApplicationの#ランに送信されたクラスで、単純なタイプミスがありました:

SpringApplication.run(TestConfiguration.class) 

は、すべてが今の順序であるように思わ

SpringApplication.run(TestApplication.class) 

されている必要があります!

関連する問題