使用自定义缓存管理器实现的Spring缓存抽象无法工作

4
我正在尝试在Spring中使用自定义缓存管理器。我已经尝试实现了Cache Manager,也扩展了AbstractCacheManager,并使用实现Cache的Custom Cache。 然而,缓存不起作用。没有从缓存中调用任何方法。 我已经阅读了多篇文章,但没有成功。可能是某些配置问题。 请帮助我解决这个问题。提前感谢您的帮助。 以下是使用的文件。 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>

root-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"/>

自定义缓存管理器

public class CustomCacheManager extends AbstractCacheManager {

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

自定义缓存

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() {

}}

测试服务实现

@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个回答

1
问题出在组件扫描上。
servlet.xml 文件和 root-context.xml 文件都扫描了相同的类文件。
修复这个问题解决了该问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接