在实现时,我遇到了Spring Cache Abstraction VS接口的问题.可以说我有以下界面:
package com.example.cache;
public interface IAddItMethod
{
Integer addIt(String key);
}
Run Code Online (Sandbox Code Playgroud)
以下两个实现:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
.
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);
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,IAddItMethod不是指定@Cacheable的那个.我们可以在没有@Cacheable注释的情况下使用其他实现(ex MethodImplThree).
我们有一个简单的beans.xml:
context:component-scan base-package="com.example.cache"
Run Code Online (Sandbox Code Playgroud)
除此之外,还有两个jUnit测试用例:
package com.example.cache; …Run Code Online (Sandbox Code Playgroud)