Spring基於註解的快取配置--web應用例項
阿新 • • 發佈:2019-02-15
現在介紹一下如何在基於註解springMVC的web應用中使用註解快取,其實很簡單,就是將springMVC配置檔案與快取註解檔案一起宣告到context中就OK了。
下面我就來構建一個基於spring註解小型的web應用,這裡我使用EHCache來作為快取方案。
首先來看一下目錄結構,如下:
jar依賴:
ehcache-core-1.7.2.jar
jakarta-oro-2.0.8.jar
slf4j-api-1.5.8.jar
slf4j-jdk14-1.5.8.jar
cglib-nodep-2.1_3.jar
commons-logging.jar
log4j-1.2.15.jar
spring-modules-cache.jar
spring.jar
jstl.jar
standard.jar
接著我們來編寫web.xml
Xml程式碼- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- id="WebApp_ID" version="2.4"
-
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
- <display-name>SpringCacheWeb</display-name>
- <!-- 由spring載入log4j -->
- <context-param>
- <param-name>log4jConfigLocation</param-name>
- <param-value>classpath:log4j.properties</param-value>
-
</context-param>
- <!-- 宣告spring配置檔案 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- /WEB-INF/spring-servlet.xml
- </param-value>
- </context-param>
- <!-- 使用UTF-8編碼 -->
- <filter>
- <filter-name>Set Character Encoding</filter-name>
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
- <init-param>
- <param-name>encoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>Set Character Encoding</filter-name>
- <url-pattern>*.do</url-pattern>
- </filter-mapping>
- <!-- 負責初始化log4j-->
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
- </listener>
- <!-- 負責初始化spring上下文-->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- springMVC控制器-->
- <servlet>
- <servlet-name>spring</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>spring</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- <session-config>
- <session-timeout>10</session-timeout>
- </session-config>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.htm</welcome-file>
- </welcome-file-list>
- </web-app>
接著我們來編寫spring-servlet.xml
Xml程式碼- <?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:context="http://www.springframework.org/schema/context"
- xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"
- default-lazy-init="true">
- <!--啟用註解 定義元件查詢規則 -->
- <context:component-scan base-package="com.netqin">
- <context:include-filter type="annotation"
- expression="org.springframework.stereotype.Controller" />
- <context:include-filter type="annotation"
- expression="org.springframework.stereotype.Service" />
- <context:include-filter type="annotation"
- expression="org.springframework.stereotype.Repository" />
- </context:component-scan>
- <!-- 檢視查詢器 -->
- <bean id="viewResolver"
- class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="viewClass"
- value="org.springframework.web.servlet.view.JstlView">
- </property>
- <property name="prefix" value="/WEB-INF/jsp/"></property>
- <property name="suffix" value=".jsp"></property>
- </bean>
- <!-- 載入ehcache快取配置檔案
- 說明:在這裡我遇到了這樣一個問題,當使用@Service等註解的方式將類宣告到配置檔案中時,
- 就需要將快取配置import到主配置檔案中,否則快取會不起作用
- 如果是通過<bean>宣告到配置檔案中時,
- 則只需要在web.xml的contextConfigLocation中加入applicationContext-ehcache.xml即可,
- 不過還是推薦使用如下方式吧,因為這樣不會有任何問題
- -->
- <import resource="classpath:applicationContext-ehcache.xml"/>
- </beans>
ok,我們接著編寫applicationContext-ehcache.xml,還記得之前介紹的基於名稱空間的配置嗎,如下:
Xml程式碼- <?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:ehcache="http://www.springmodules.org/schema/ehcache"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd">
- <ehcache:config configLocation="classpath:ehcache.xml"
- id="cacheProvider" />
- <ehcache:annotations providerId="cacheProvider">
- <ehcache:caching cacheName="testCache" id="testCaching" />
- <ehcache:flushing cacheNames="testCache" id="testFlushing" />
- </ehcache:annotations>
- </beans>
ehcache.xml如下:
Xml程式碼- <?xml version="1.0" encoding="UTF-8"?>
- <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
- monitoring="autodetect">
- <diskStore path="java.io.tmpdir"/>
- <defaultCache
- maxElementsInMemory="10000"
- eternal="false"
- timeToIdleSeconds="120"
- timeToLiveSeconds="120"
- overflowToDisk="true"
- maxElementsOnDisk="10000000"
- diskPersistent="false"
- diskExpiryThreadIntervalSeconds="120"
- memoryStoreEvictionPolicy="LRU"
- />
- <cache name="testCache"
- maxElementsInMemory="10000"
- maxElementsOnDisk="1000"
- eternal="false"
- overflowToDisk="true"
- diskSpoolBufferSizeMB="20"
- timeToIdleSeconds="300"
- timeToLiveSeconds="600"
- memoryStoreEvictionPolicy="LFU"
- />
- </ehcache>
ok,配置檔案都完成了,接著我們來編寫controller、service和dao
1.CacheDemoController:
Java程式碼