2014-03-01 33 views
1

我環顧了網絡,發現了一些建議,並嘗試了不同的配置,但我完全不確定它是否正常工作。春季:我是否需要@EnableAspectJAutoProxy編譯時織入?

的pom.xml(全pom.xml中:http://pastebin.com/5Y2qksTH):

<build> 
    <pluginManagement> 
     <plugins> 
      <plugin> 
       <groupId>org.eclipse.m2e</groupId> 
       <artifactId>lifecycle-mapping</artifactId> 
       <version>1.0.0</version> 
       <configuration> 
        <lifecycleMappingMetadata> 
         <pluginExecutions> 
          <pluginExecution> 
           <pluginExecutionFilter> 
            <groupId>org.codehaus.mojo</groupId> 
            <artifactId>aspectj-maven-plugin</artifactId> 
            <versionRange>[1.0,)</versionRange> 
            <goals> 
             <goal>test-compile</goal> 
             <goal>compile</goal> 
            </goals> 
           </pluginExecutionFilter> 
           <action> 
            <execute> 
             <runOnConfiguration>true</runOnConfiguration> 
             <runOnIncremental>true</runOnIncremental> 
            </execute> 
           </action> 
          </pluginExecution> 
         </pluginExecutions> 
        </lifecycleMappingMetadata> 
       </configuration> 
      </plugin> 
     </plugins> 
    </pluginManagement> 

    <plugins> 
     <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>aspectj-maven-plugin</artifactId> 
      <version>1.5</version> 
      <configuration> 
       <Xlint>warning</Xlint> 
       <complianceLevel>1.7</complianceLevel> 
       <source>1.7</source> 
       <target>1.7</target> 
       <encoding>UTF-8</encoding> 
       <aspectLibraries> 
        <aspectLibrary> 
         <groupId>org.springframework</groupId> 
         <artifactId>spring-aspects</artifactId> 
        </aspectLibrary> 
       </aspectLibraries> 
      </configuration> 
      <executions> 
       <execution> 
        <goals> 
         <goal>compile</goal> 
         <goal>test-compile</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 

我添加

<execute> 
    <runOnConfiguration>true</runOnConfiguration> 
    <runOnIncremental>true</runOnIncremental> 
</execute> 

,因爲它看起來就像我以前一直做Project -> Clean在Eclipse和之後Tomcat -> Clean。否則它總是執行我的緩存方法。現在它似乎自動工作。

CacheableConfig.java

@EnableCaching(mode = AdviceMode.ASPECTJ) 
public class CacheableConfig implements CachingConfigurer { 
    @Override 
    public CacheManager cacheManager() { 
     return new ConcurrentMapCacheManager("testCache"); 
    } 

    @Override 
    public KeyGenerator keyGenerator() { 
     return new SimpleKeyGenerator(); 
    } 
} 

AppConfig.java

@EnableAspectJAutoProxy 
public class AppConfig {} 

沒有@EnableAspectJAutoProxy它並沒有在所有的工作。

MyTestServiceImpl.java

@Service 
public class MyTestServiceImpl implements MyTestService { 
    @Scheduled(initialDelay=10000, fixedDelay=30000) 
    public void a() { 
     long time = System.currentTimeMillis(); 
     System.out.println("START"); 
     System.out.println("returned: " + b(0)); 
     System.out.println("returned: " + b(1)); 
     System.out.println("returned: " + b(0)); 
     System.out.println("returned: " + b(1)); 
     System.out.println("returned: " + b(2)); 
     System.out.println("END: " + (System.currentTimeMillis() - time)); 
    } 

    @Cacheable("testCache") 
    public int b(int i) { 
     System.out.println("INSIDE CACHED METHOD"); 
     i++; 
     try { 
      Thread.sleep(2000); 
     } catch(InterruptedException ex) {} 
     return i; 
    } 
} 

注:我只是用@Scheduled以多次自動調用該方法。

輸出:

START 
2014-03-01 15:53:25,796 DEBUG o.s.c.annotation.AnnotationCacheOperationSource: 109 - Adding cacheable method 'b' with attribute: [CacheableOperation[public int MyTestServiceImpl.b(int)] caches=[testCache] | key='' | condition='' | unless=''] 
2014-03-01 15:53:25,797 TRACE   o.s.cache.aspectj.AnnotationCacheAspect: 318 - Computed cache key 0 for operation CacheableOperation[public int MyTestServiceImpl.b(int)] caches=[testCache] | key='' | condition='' | unless='' 
INSIDE CACHED METHOD 
returned: 1 
2014-03-01 15:53:27,798 TRACE   o.s.cache.aspectj.AnnotationCacheAspect: 318 - Computed cache key 1 for operation CacheableOperation[public int MyTestServiceImpl.b(int)] caches=[testCache] | key='' | condition='' | unless='' 
INSIDE CACHED METHOD 
returned: 2 
2014-03-01 15:53:29,799 TRACE   o.s.cache.aspectj.AnnotationCacheAspect: 318 - Computed cache key 0 for operation CacheableOperation[public int MyTestServiceImpl.b(int)] caches=[testCache] | key='' | condition='' | unless='' 
2014-03-01 15:53:29,799 TRACE   o.s.cache.aspectj.AnnotationCacheAspect: 318 - Computed cache key 0 for operation CacheableOperation[public int MyTestServiceImpl.b(int)] caches=[testCache] | key='' | condition='' | unless='' 
returned: 1 
2014-03-01 15:53:29,799 TRACE   o.s.cache.aspectj.AnnotationCacheAspect: 318 - Computed cache key 1 for operation CacheableOperation[public int MyTestServiceImpl.b(int)] caches=[testCache] | key='' | condition='' | unless='' 
2014-03-01 15:53:29,799 TRACE   o.s.cache.aspectj.AnnotationCacheAspect: 318 - Computed cache key 1 for operation CacheableOperation[public int MyTestServiceImpl.b(int)] caches=[testCache] | key='' | condition='' | unless='' 
returned: 2 
2014-03-01 15:53:29,799 TRACE   o.s.cache.aspectj.AnnotationCacheAspect: 318 - Computed cache key 2 for operation CacheableOperation[public int MyTestServiceImpl.b(int)] caches=[testCache] | key='' | condition='' | unless='' 
INSIDE CACHED METHOD 
returned: 3 
END: 6018 

START 
2014-03-01 15:54:01,801 TRACE   o.s.cache.aspectj.AnnotationCacheAspect: 318 - Computed cache key 0 for operation CacheableOperation[public int MyTestServiceImpl.b(int)] caches=[testCache] | key='' | condition='' | unless='' 
2014-03-01 15:54:01,801 TRACE   o.s.cache.aspectj.AnnotationCacheAspect: 318 - Computed cache key 0 for operation CacheableOperation[public int MyTestServiceImpl.b(int)] caches=[testCache] | key='' | condition='' | unless='' 
returned: 1 
2014-03-01 15:54:01,801 TRACE   o.s.cache.aspectj.AnnotationCacheAspect: 318 - Computed cache key 1 for operation CacheableOperation[public int MyTestServiceImpl.b(int)] caches=[testCache] | key='' | condition='' | unless='' 
2014-03-01 15:54:01,801 TRACE   o.s.cache.aspectj.AnnotationCacheAspect: 318 - Computed cache key 1 for operation CacheableOperation[public int MyTestServiceImpl.b(int)] caches=[testCache] | key='' | condition='' | unless='' 
returned: 2 
2014-03-01 15:54:01,801 TRACE   o.s.cache.aspectj.AnnotationCacheAspect: 318 - Computed cache key 0 for operation CacheableOperation[public int MyTestServiceImpl.b(int)] caches=[testCache] | key='' | condition='' | unless='' 
2014-03-01 15:54:01,802 TRACE   o.s.cache.aspectj.AnnotationCacheAspect: 318 - Computed cache key 0 for operation CacheableOperation[public int MyTestServiceImpl.b(int)] caches=[testCache] | key='' | condition='' | unless='' 
returned: 1 
2014-03-01 15:54:01,802 TRACE   o.s.cache.aspectj.AnnotationCacheAspect: 318 - Computed cache key 1 for operation CacheableOperation[public int MyTestServiceImpl.b(int)] caches=[testCache] | key='' | condition='' | unless='' 
2014-03-01 15:54:01,802 TRACE   o.s.cache.aspectj.AnnotationCacheAspect: 318 - Computed cache key 1 for operation CacheableOperation[public int MyTestServiceImpl.b(int)] caches=[testCache] | key='' | condition='' | unless='' 
returned: 2 
2014-03-01 15:54:01,802 TRACE   o.s.cache.aspectj.AnnotationCacheAspect: 318 - Computed cache key 2 for operation CacheableOperation[public int MyTestServiceImpl.b(int)] caches=[testCache] | key='' | condition='' | unless='' 
2014-03-01 15:54:01,802 TRACE   o.s.cache.aspectj.AnnotationCacheAspect: 318 - Computed cache key 2 for operation CacheableOperation[public int MyTestServiceImpl.b(int)] caches=[testCache] | key='' | condition='' | unless='' 
returned: 3 
END: 1 

基本上,這看起來不錯:

  • a()第一次調用花費6秒,因爲b()被稱爲有效的3倍。
  • 第二次調用a()需要1ms,因爲所有內容都來自緩存。
  • 返回值沒問題。

問題

  1. 爲什麼這些跟蹤日誌Computed cache key x for operation ...總是有次?看起來像計算方法被調用兩次?!

  2. 配置沒問題嗎?因爲我不確定這是否會始終按預期工作,特別是因爲我有時需要使用Project -> CleanTomcat -> Clean。 (否則它只是忽略了@Cacheable註釋和簡單的叫做方法)

謝謝!

回答

2

首先,您當前的設置將不會與@EnableAspectJAutoProxy一起使用。 Spring AOP使用代理來應用方面。你的@Scheduled在內部調用方法,因此永遠不會通過代理,你永遠不會有緩存。

除此之外,你使用編譯時編織,所以你不應該使用@EnableAspectJAutoProxy作爲方面已經編織。如果這是行不通的,你有一個問題在你的pom和你的Eclipse一起設置。

試圖讓Eclipse和Maven一起工作是(或可能)一項艱鉅的任務。關於AspectJ和Maven,請參閱aspectj-maven-plugin not covered by lifecycle in Keplerthis blog也可能會有所幫助。

+0

感謝您的建議。 ** 1。**所以如果我理解正確,我應該刪除'@ EnableAspectJAutoProxy',因爲我想使用編譯時編織。 Eclipse和Maven正在一起工作(即生成用於查詢數據庫的類文件)。 ** 2。** @ Scheduled方法的確在那裏調用了'@ Cacheable'方法,因爲這是一個只能使用AspectJ的場景。當使用標準的AOP代理時,永遠不要調用AnnotationCacheAspect。所以我認爲使用'@ Cacheable'的AspectJ能以某種方式工作?! –

+0

如果使用編譯(或加載時間)編織,它將起作用,而不是使用默認的代理方法。也許我應該重新措辭,但是讓Eclipse與所有你的Maven插件一起工作可能是一件痛苦的事情。 (這是我切換到IntelliJ的原因之一,它具有更好的Maven支持,然後Eclipse)。 –

+0

現在我很困惑':-D' ...所以這是我想要的:簡單**編譯時織入**。我需要'@ EnableAspectJAutoProxy'嗎?也許你可以詳細說一下'@ EnableAspectJAutoProxy'的作用? JavaDoc說:「支持處理標有AspectJ的@Aspect註釋的組件,類似於Spring的 XML元素中的功能,這聽起來像*它支持AspectJ *。 –

相關問題