spring MVC與Ehcache整合

分享一下我整合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%從緩存中獲取到,

很疑惑,希望有人能給指指道,小老弟不勝感激。。。

最近整理了學習材料,有需要的請下載,我放微信裏面了,方便下載,還能交流,掃描我的二維碼頭像即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章