Spring Cache Abstraction与Custom Cache Manager实现无法正常工作

时间:2016-06-16 06:16:43

标签: java spring caching

我正在尝试在春天使用自定义缓存管理器。我试图实现缓存管理器,并使用实现缓存的自定义缓存扩展了AbstractCacheManager。 但是,缓存不起作用。没有调用缓存中的方法。

我已阅读多篇帖子,但没有成功。可能是一些配置问题。

请帮我解决这个问题。提前谢谢。

以下是正在使用的文件。

web.xml

<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Spring MVC Application</display-name>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/root-context.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

根context.xml中

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
   xmlns:cache="http://www.springframework.org/schema/cache"
   xsi:schemaLocation="
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.springapp.mvc.*"/>
<cache:annotation-driven/>
<bean id="cacheManager" class="com.springapp.mvc.cache.CustomCacheManager"/>

CustomCacheManager

public class CustomCacheManager extends AbstractCacheManager {

@Override
protected Collection<? extends Cache> loadCaches() {
    Collection<CustomCache> caches = new ArrayList<CustomCache>();
    caches.add(new CustomCache("testCache"));
    return caches;
}}

CustomCache

public class CustomCache implements Cache {

private String name;
private final ConcurrentMap<Object, Object> map = new ConcurrentHashMap<Object, Object>();

public CustomCache(String name) {
    this.name = name;
}

@Override
public String getName() {
    return name;
}

@Override
public Object getNativeCache() {
    return null;
}

@Override
public ValueWrapper get(Object o) {
    System.out.println("In get of value wrapper cache");
    return new SimpleValueWrapper("fafa");
}

@Override
public <T> T get(Object o, Class<T> aClass) {
    System.out.println("In get of Custom Cache");
    this.get(o);
    return (T) "Test";
}

@Override
public void put(Object key, Object value) {
    System.out.println("Adding to cache");
    this.map.put(key, value);
}

@Override
public ValueWrapper putIfAbsent(Object o, Object o1) {
    System.out.println("Adding to Cache if Absent");
    return new SimpleValueWrapper("Test value from cache");
}

@Override
public void evict(Object o) {

}

@Override
public void clear() {

}}

测试服务Impl

@Service("testService")
public class TestServiceImpl implements ITestService {

    @Cacheable(value = "testCache", key = "'key'")
    public String testMethod(String key) {
        System.out.println("In Test Method");
        return "fadfa";
    }}

1 个答案:

答案 0 :(得分:0)

问题在于组件扫描。

servlet.xml文件和root-context.xml扫描了相同的类文件。

修复此问题解决了这个问题。