Spring基于注解的缓存配置--web应用实例

现在介绍一下如何在基于注解springMVC的web应用中使用注解缓存,其实很简单,就是将springMVC配置文件与缓存注解文件一起声明到context中就OK了。

 

下面我就来构建一个基于spring注解小型的web应用,这里我使用EHCache来作为缓存方案。

 

首先来看一下目录结构,如下:

 Spring基于注解的缓存配置--web应用实例_第1张图片

 

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代码   收藏代码
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     id="WebApp_ID" version="2.4"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  5.     <display-name>SpringCacheWebdisplay-name>  
  6.   
  7.       
  8.     <context-param>  
  9.         <param-name>log4jConfigLocationparam-name>  
  10.         <param-value>classpath:log4j.propertiesparam-value>  
  11.     context-param>  
  12.       
  13.       
  14.     <context-param>  
  15.         <param-name>contextConfigLocationparam-name>  
  16.         <param-value>  
  17.             /WEB-INF/spring-servlet.xml  
  18.         param-value>  
  19.     context-param>  
  20.       
  21.       
  22.     <filter>  
  23.         <filter-name>Set Character Encodingfilter-name>  
  24.         <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>  
  25.         <init-param>  
  26.             <param-name>encodingparam-name>  
  27.             <param-value>UTF-8param-value>  
  28.         init-param>  
  29.     filter>  
  30.     <filter-mapping>  
  31.         <filter-name>Set Character Encodingfilter-name>  
  32.         <url-pattern>*.dourl-pattern>  
  33.     filter-mapping>  
  34.   
  35.       
  36.     <listener>  
  37.         <listener-class>org.springframework.web.util.Log4jConfigListenerlistener-class>  
  38.     listener>  
  39.       
  40.       
  41.     <listener>  
  42.         <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>  
  43.     listener>  
  44.   
  45.       
  46.     <servlet>  
  47.         <servlet-name>springservlet-name>  
  48.         <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>  
  49.         <load-on-startup>1load-on-startup>  
  50.     servlet>  
  51.   
  52.     <servlet-mapping>  
  53.         <servlet-name>springservlet-name>  
  54.         <url-pattern>*.dourl-pattern>  
  55.     servlet-mapping>  
  56.   
  57.     <session-config>  
  58.         <session-timeout>10session-timeout>  
  59.     session-config>  
  60.   
  61.     <welcome-file-list>  
  62.         <welcome-file>index.jspwelcome-file>  
  63.         <welcome-file>index.htmlwelcome-file>  
  64.         <welcome-file>index.htmwelcome-file>  
  65.     welcome-file-list>  
  66. web-app>  

 

接着我们来编写spring-servlet.xml

 

Xml代码   收藏代码
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:ehcache="http://www.springmodules.org/schema/ehcache"  
  6.     xsi:schemaLocation="  
  7.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.             http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  9.             http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd"  
  10.             default-lazy-init="true">  
  11.   
  12.   
  13.       
  14.     <context:component-scan base-package="com.netqin">  
  15.         <context:include-filter type="annotation"  
  16.             expression="org.springframework.stereotype.Controller" />  
  17.         <context:include-filter type="annotation"  
  18.             expression="org.springframework.stereotype.Service" />  
  19.         <context:include-filter type="annotation"  
  20.             expression="org.springframework.stereotype.Repository" />  
  21.     context:component-scan>  
  22.   
  23.       
  24.     <bean id="viewResolver"  
  25.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  26.         <property name="viewClass"  
  27.             value="org.springframework.web.servlet.view.JstlView">  
  28.         property>  
  29.         <property name="prefix" value="/WEB-INF/jsp/">property>  
  30.         <property name="suffix" value=".jsp">property>  
  31.     bean>  
  32.       
  33.     

你可能感兴趣的:(spring)