日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringMVC4集成ehcache

發布時間:2025/5/22 javascript 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringMVC4集成ehcache 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

? ? ?使用SpringMVC4集成ehcache來緩存服務器數據。

開發環境

? ? SpringMVC4、ehcache2.6、

?

項目結構

?

SpringMVC 集成ehcache

1、pom.xml

//除了SpringMVC 、sql server相關就是ehcache-core <dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache-core</artifactId><version>2.6.11</version></dependency>

2、springmvc-servlet.xml

? ? ?主要就是開啟cache注解,同時導入spring cache命名空間。

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:cache="http://www.springframework.org/schema/cache"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache.xsd"><!--從配置文件加載數據庫信息--><bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations" value="classpath:config/jdbc.properties"/><property name="fileEncoding" value="UTF-8"/></bean><!--配置數據源,這里使用Spring默認--><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="${sqlserver.driver}"/><property name="url" value="${sqlserver.url}"/><property name="username" value="${sqlserver.username}"/><property name="password" value="${sqlserver.password}"/></bean><!--掃描Mapper--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.autohome.dao"/></bean><!--配置sqlSessionFactory--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="configLocation" value="classpath:springmvc-mybatis.xml"/><property name="dataSource" ref="dataSource"/></bean><!--啟用緩存注解--><cache:annotation-driven cache-manager="cacheManager"/><bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"><property name="configLocation" value="classpath:ehcache.xml"/></bean><bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"><property name="cacheManager" ref="cacheManagerFactory"/></bean><!--啟用最新的注解器、映射器--><mvc:annotation-driven/><!--使用默認的Servlet來響應靜態資源--><mvc:default-servlet-handler/><!--掃描Controller注解類--><context:component-scan base-package="com.autohome.controller" /><!--掃描Service注解類--><context:component-scan base-package="com.autohome.service"/><!--jsp視圖解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/"/><property name="suffix" value=".jsp"/></bean></beans>

?

3、ehcache.xml

? ? xsd我配置以后一直報紅,不知道怎么回事,后來試著運行了下發現不影響程序運行,索性就這么用著

<?xml version="1.0" encoding="gbk"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd"><diskStore path="java.io.tmpdir" /><defaultCachemaxElementsInMemory="10000"eternal="false"timeToIdleSeconds="120"timeToLiveSeconds="120"overflowToDisk="false"/><cache name="myCache"eternal="false"maxElementsInMemory="10000"overflowToDisk="false"timeToIdleSeconds="0"timeToLiveSeconds="0"memoryStoreEvictionPolicy="LFU"/></ehcache>

?

3、Service層里調用Dao層時使用緩存注解

? ? ? 在listAllUser方法上使用Cacheable注解,第一次運行時緩存里還沒數據控制臺會打印query from database...再次調用則直接讀取緩存數據

@Cacheable(value = "myCache")public List<User> listAllUser() {System.out.println("query from database...");return userDao.listAllUser();}

  

4、Controller里調用

@Controller @RequestMapping("/User") public class UserController {@AutowiredUserService userService;@RequestMapping("/index")public ModelAndView index(){System.out.println("size:"+userService.listAllUser().size());ModelAndView mav =new ModelAndView("index");return mav;} }

  

轉載于:https://www.cnblogs.com/sword-successful/p/6710106.html

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的SpringMVC4集成ehcache的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。