A simple example of how to use spring annotations for caching configurations (EHCache and Oscache) is described earlier in this article.
Spring annotation-based cache configuration--ehcache and Oscache
Now how to use note caching in a web app based on annotation SPRINGMVC is simply a matter of declaring the SPRINGMVC configuration file with the cached annotation file in the context.
I'm going to build a small Web application based on spring annotations, where I use Ehcache as a caching scheme.
Jar dependencies:
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
Then we'll write Web. 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/ Java EE http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "><display-name>springcacheweb</display-name ><!--loaded by spring log4j--><context-param><param-name>log4jconfiglocation</param-name> <param-value>classpath:log4j.properties</param-value></context-param><!--Declaration Spring configuration file-- ><context-param><param-name>contextconfiglocation</param-name><param-value>/web-inf /spring-servlet.xml</param-value></context-param><!--using UTF-8 encoding--><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><!-- Responsible for initializing the log4j--><listener><listener-class>org.springframework.web.util.log4jconfiglistener</ listener-class></listener><!--is responsible for initializing the spring context--><listener><listener-class> org.springframework.web.context.contextloaderlistener</listener-class></listener><!-- SPRINGMVC Controller--><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>
Then we'll write Spring-servlet.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.xsdhttp://www.springframework.org/ Schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springmodules.org /schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd "default-lazy-init=" true " ><!--enable annotations to define component lookup rules--><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><!--view finder-->< 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><!--Load Ehcache Cache profile Description: I have encountered a problem here, when using annotations such as @service to declare a class to a configuration file, you need to import the cache configuration into the master configuration file, Otherwise, the cache will not work if it is declared in the configuration file by <bean> You only need to add applicationcontext-ehcache.xml to the contextconfiglocation of Web. XML, but it is recommended to use the following method, as this will not have any problems-->< Import resource= "Classpath:applicationcontext-ehcache.xml"/></beans>
OK, let's go on to write Applicationcontext-ehcache.xml, remember the namespace-based configuration we introduced earlier, as follows:
<?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.xsdhttp://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 as follows
<?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= "timetoliveseconds=" overflowtodisk= "true" maxelementsondisk= "10000000" diskpersistent= "false" diskexpirythreadintervalseconds= "120 "memorystoreevictionpolicy=" LRU "/> <cache name=" Testcache "maxelementsinmemory=" 10 "Maxelementsondisk=" "eternal=" false "overflowtodisk=" true "Diskspoolbuffe rsizemb= "timetoidleseconds=" timetoliveseconds= "memorystoreevictionpolicy=" LFU " /></ehcache>
OK, the configuration files are complete, then we'll write the controller, service, and DAO
1.CacheDemoController:
Package Com.netqin.function.cachedemo;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.controller;import Org.springframework.ui.model;import org.springframework.web.bind.annotation.RequestMapping; @Controllerpublic class Cachedemocontroller {@ Autowiredprivate Cachedemoservice Service, @RequestMapping ("/demo.do") public String Handleindex (model model) { System.out.println (service.getname (0)); Model.addattribute ("Name", Service.getname (0)); return "Cachedemo";} @RequestMapping ("/demofulsh.do") public String Handlefulsh (model model) {Service.flush (); return "Cachedemo"; }}
2.CacheDemoService:
Package Com.netqin.function.cachedemo;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.service;import Org.springmodules.cache.annotations.cacheflush;import Org.springmodules.cache.annotations.Cacheable, @Servicepublic class Cachedemoservice {@Autowiredprivate Cachedemodao DAO; @Cacheable (modelid = "testcaching") public String getName (int id) {SYSTEM.OUT.PRINTLN ("processing Testcaching "); return Dao.getname (ID);} @CacheFlush (modelid = "testflushing") public void Flush () {System.out.println ("Processing testflushing");}}
We only added the annotation cache configuration to the service layer.
Then we'll write a simple page, cachedemo.jsp:
<%@ page language= "java" contenttype= "text/html; Charset=utf-8 " pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
OK, everything is ready, we start the server and access the Http://localhost:8080/cache/demo.do
Multiple requests, requests two times output result:
Processing testcaching
nameid:0
nameid:0
It means the cache is working, ok!.
We then refresh the cache to access the Http://localhost:8080/cache/demoFulsh.do
Request Http://localhost:8080/cache/demo.do again.
Output Result:
Processing testcaching
nameid:0
nameid:0
Processing testflushing
Processing testcaching
nameid:0
Cache refresh succeeded.
Cache initial solution (v)---springmvc annotation-based cache configuration--web Application Example