1
봄에 맞춤 캐시 관리자를 사용하려고합니다. 캐쉬 관리자를 구현하려고 시도했으며 Cache를 구현하는 사용자 정의 캐시를 사용하여 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";
}}