在實現時,我遇到了Spring Cache抽象VS接口的問題。 可以說我有以下接口:Spring Cache抽象VS接口VS關鍵參數(「返回緩存操作的空鍵」錯誤)
package com.example.cache;
public interface IAddItMethod
{
Integer addIt(String key);
}
與以下兩種實現方式:
package com.example.cache;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
@Component
public class MethodImplOne implements IAddItMethod
{
@Override
@Cacheable(value="integersPlusOne", key="#keyOne")
public Integer addIt(String keyOne)
{
return new Integer(Integer.parseInt(keyOne) + 1);
}
}
。
package com.example.cache;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
@Component
public class MethodImplTwo implements IAddItMethod
{
@Override
@Cacheable(value="integersPlusTwo", key="#keyTwo")
public Integer addIt(String keyTwo)
{
return new Integer(Integer.parseInt(keyTwo) + 2);
}
}
請注意,IAddItMethod不是指定@Cacheable的那個。我們可以有沒有@Cacheable註釋的其他實現(例如MethodImplThree)。
我們已經有了一個簡單的beans.xml有:
context:component-scan base-package="com.example.cache"
添加到兩個JUnit測試案例:
package com.example.cache;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:beans.xml"})
public class MethodImplOneTest
{
@Autowired
@Qualifier("methodImplOne")
private IAddItMethod classUnderTest;
@Test
public void testInit()
{
int number = 1;
assertEquals(new Integer(number + 1), classUnderTest.addIt("" + number));
}
}
。
package com.example.cache;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:beans.xml"})
public class MethodImplTwoTest
{
@Autowired
@Qualifier("methodImplTwo")
private IAddItMethod classUnderTest;
@Test
public void testInit()
{
int number = 1;
assertEquals(new Integer(number + 2), classUnderTest.addIt("" + number));
}
}
當我單獨運行測試,他們成功了。 但是,如果我跑他們兩個在一起(選擇包,右鍵單擊,運行),第二個(不一定MethodImplTwoTest,只是第二個運行)將失敗,但以下情況除外:
java.lang.IllegalArgumentException: Null key returned for cache operation (maybe you are using named params on classes without debug info?) CacheableOperation[public java.lang.Integer com.example.cache.MethodImplOne.addIt(java.lang.String)] caches=[integersPlusOne] | condition='' | key='#keyOne' at org.springframework.cache.interceptor.CacheAspectSupport.inspectCacheables(CacheAspectSupport.java:297) at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:198) at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:66) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) at $Proxy16.addIt(Unknown Source) at com.example.cache.ITMethodImplOneIntegrationTest.testInit(ITMethodImplOneIntegrationTest.java:26) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
注意:我正在使用Eclipse STS 3.0,並且「添加變量屬性到生成的類文件」已啓用。
重要提示:如果我沒有在@Cacheable註釋中指定「key」,它將起作用。
有什麼我忘了指定?配置?註釋?
在此先感謝!
它工作的感謝!但是,我認爲這是限制性的,並且強制實施尊重這一點很複雜。我將在接口和當前的實現中添加完整的javadoc,但另一個開發人員可能會遇到同樣的問題,以便實現新的實現。再次感謝鮑里斯! – dostiguy
@dostiguy我添加了一個鏈接到文檔有關訪問參數的索引,我沒有嘗試過自己,雖然 –
好的想法,我忘了我們可以做到這一點。我嘗試了它並運行了我的測試(真正的實現有大約100個測試用例,而且鍵比這個例子更復雜),它可以工作。現在我只是不確定我喜歡哪種方式。再次感謝。 – dostiguy