SpringMVC4整合ehcache,springmvc4ehcache

來源:互聯網
上載者:User

SpringMVC4整合ehcache,springmvc4ehcache
前言

     使用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/beans       http://www.springframework.org/schema/beans/spring-beans.xsd       http://www.springframework.org/schema/context       http://www.springframework.org/schema/context/spring-context.xsd       http://www.springframework.org/schema/mvc       http://www.springframework.org/schema/mvc/spring-mvc.xsd       http://www.springframework.org/schema/cache       http://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" />      <defaultCache        maxElementsInMemory="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 {    @Autowired    UserService userService;    @RequestMapping("/index")    public ModelAndView index(){        System.out.println("size:"+userService.listAllUser().size());        ModelAndView mav =new ModelAndView("index");        return mav;    }}

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.