分享一下我整合spring MVC与Ehcache遇到的问题:
1 下面这段配置是必须的了,但是如果单独写在一个xml文件中,并能被spring加载,却不能生效,但是如果写在spirng MVC的配置文件中就能够起作用
<!-- 缓存 属性-->
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"><property name="configLocation" value="classpath:config/ehcache.xml"/>
</bean>
<!-- 支持缓存注解 -->
<cache:annotation-driven cache-manager="cacheManager" />
<!-- 默认是cacheManager -->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="cacheManagerFactory"/>
</bean>
2 本人比较笨,还遇到下面的问题,问题虽然解决了但是不知道是怎么回事。
package com.fsj.spring.base.se;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import sinonet.framework.service.GenericCrudService;
import com.fsj.util.JsonUtils;
import com.nxy.domain.common.Syresourcetype;
@Controller
@RequestMapping("/base")
public class ResourceTypeController {
@Autowired
private UserService userService;
@Autowired
private GenericCrudService genericCrudService;
@RequestMapping ("/doNotNeedSecurity_combobox")
@Cacheable(value = "serviceCache")
public List<Syresourcetype> doNotNeedSecurity_combobox()
{
return userService.getSyresourcetypeList();
// return getSyresourcetypeList();
}
// @Cacheable(value = "serviceCache")
public List<Syresourcetype> getSyresourcetypeList(){
System.out.println("doNotNeedSecurity_combobox 非缓存查询");
List<Syresourcetype> syresourcetypeList = (List<Syresourcetype>) genericCrudService.hql("select distinct t from Syresourcetype t");
return syresourcetypeList;
}
}
和
public void doNotNeedSecurity_combobox(HttpServletRequest request,HttpServletResponse response)
{
new JsonUtils().writeJson(request, response, getSyresourcetypeList());
}
两段代码相比,虽然只差了请求参数不一样,但是导致的结果也不一样,上面的代码除了第一次外,每次都能从缓存中查找到,而用下面的方法,就会不是100%从缓存中获取到,
很疑惑,希望有人能给指指道,小老弟不胜感激。。。
最近整理了学习材料,有需要的请下载,我放微信里面了,方便下载,还能交流,扫描我的二维码头像即可。