我環顧了網絡,發現了一些建議,並嘗試了不同的配置,但我完全不確定它是否正常工作。春季:我是否需要@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,因爲所有內容都來自緩存。 - 返回值沒問題。
問題:
爲什麼這些跟蹤日誌
Computed cache key x for operation ...
總是有次?看起來像計算方法被調用兩次?!配置沒問題嗎?因爲我不確定這是否會始終按預期工作,特別是因爲我有時需要使用
Project -> Clean
和Tomcat -> Clean
。 (否則它只是忽略了@Cacheable
註釋和簡單的叫做方法)
謝謝!
感謝您的建議。 ** 1。**所以如果我理解正確,我應該刪除'@ EnableAspectJAutoProxy',因爲我想使用編譯時編織。 Eclipse和Maven正在一起工作(即生成用於查詢數據庫的類文件)。 ** 2。** @ Scheduled方法的確在那裏調用了'@ Cacheable'方法,因爲這是一個只能使用AspectJ的場景。當使用標準的AOP代理時,永遠不要調用AnnotationCacheAspect。所以我認爲使用'@ Cacheable'的AspectJ能以某種方式工作?! –
如果使用編譯(或加載時間)編織,它將起作用,而不是使用默認的代理方法。也許我應該重新措辭,但是讓Eclipse與所有你的Maven插件一起工作可能是一件痛苦的事情。 (這是我切換到IntelliJ的原因之一,它具有更好的Maven支持,然後Eclipse)。 –
現在我很困惑':-D' ...所以這是我想要的:簡單**編譯時織入**。我需要'@ EnableAspectJAutoProxy'嗎?也許你可以詳細說一下'@ EnableAspectJAutoProxy'的作用? JavaDoc說:「支持處理標有AspectJ的@Aspect註釋的組件,類似於Spring的 XML元素中的功能,這聽起來像*它支持AspectJ *。 –