当前位置: 首页 > news >正文

网站建设费用的财务核算抖音seo排名软件

网站建设费用的财务核算,抖音seo排名软件,小程序怎么做网站,wordpress移动底部导航菜单现在介绍一下如何在基于注解springMVC的web应用中使用注解缓存,其实很简单,就是将springMVC配置文件与缓存注解文件一起声明到context中就OK了。 下面我就来构建一个基于spring注解小型的web应用,这里我使用EHCache来作为缓存方案。 首先来看一下目录结构…

 

现在介绍一下如何在基于注解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代码  收藏代码
  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>SpringCacheWeb</display-name>  
  6.   
  7.     <!-- 由spring加载log4j -->  
  8.     <context-param>  
  9.         <param-name>log4jConfigLocation</param-name>  
  10.         <param-value>classpath:log4j.properties</param-value>  
  11.     </context-param>  
  12.       
  13.     <!-- 声明spring配置文件 -->  
  14.     <context-param>  
  15.         <param-name>contextConfigLocation</param-name>  
  16.         <param-value>  
  17.             /WEB-INF/spring-servlet.xml  
  18.         </param-value>  
  19.     </context-param>  
  20.       
  21.     <!-- 使用UTF-8编码 -->  
  22.     <filter>  
  23.         <filter-name>Set Character Encoding</filter-name>  
  24.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  25.         <init-param>  
  26.             <param-name>encoding</param-name>  
  27.             <param-value>UTF-8</param-value>  
  28.         </init-param>  
  29.     </filter>  
  30.     <filter-mapping>  
  31.         <filter-name>Set Character Encoding</filter-name>  
  32.         <url-pattern>*.do</url-pattern>  
  33.     </filter-mapping>  
  34.   
  35.     <!-- 负责初始化log4j-->  
  36.     <listener>  
  37.         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
  38.     </listener>  
  39.       
  40.     <!-- 负责初始化spring上下文-->  
  41.     <listener>  
  42.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  43.     </listener>  
  44.   
  45.     <!-- springMVC控制器-->  
  46.     <servlet>  
  47.         <servlet-name>spring</servlet-name>  
  48.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  49.         <load-on-startup>1</load-on-startup>  
  50.     </servlet>  
  51.   
  52.     <servlet-mapping>  
  53.         <servlet-name>spring</servlet-name>  
  54.         <url-pattern>*.do</url-pattern>  
  55.     </servlet-mapping>  
  56.   
  57.     <session-config>  
  58.         <session-timeout>10</session-timeout>  
  59.     </session-config>  
  60.   
  61.     <welcome-file-list>  
  62.         <welcome-file>index.jsp</welcome-file>  
  63.         <welcome-file>index.html</welcome-file>  
  64.         <welcome-file>index.htm</welcome-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.     <!-- 加载ehcache缓存配置文件   
  34.   
  35.     说明:在这里我遇到了这样一个问题,当使用@Service等注解的方式将类声明到配置文件中时,  
  36.     就需要将缓存配置import到主配置文件中,否则缓存会不起作用  
  37.     如果是通过<bean>声明到配置文件中时,  
  38.     则只需要在web.xml的contextConfigLocation中加入applicationContext-ehcache.xml即可,  
  39.     不过还是推荐使用如下方式吧,因为这样不会有任何问题  
  40.     -->  
  41.     <import resource="classpath:applicationContext-ehcache.xml"/>  
  42. </beans>  

 

ok,我们接着编写applicationContext-ehcache.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" xmlns:ehcache="http://www.springmodules.org/schema/ehcache"  
  4.     xsi:schemaLocation="  
  5.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  6.             http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd">  
  7.   
  8.   
  9.     <ehcache:config configLocation="classpath:ehcache.xml"  
  10.         id="cacheProvider" />  
  11.     <ehcache:annotations providerId="cacheProvider">  
  12.         <ehcache:caching cacheName="testCache" id="testCaching" />  
  13.         <ehcache:flushing cacheNames="testCache" id="testFlushing" />  
  14.     </ehcache:annotations>  
  15.       
  16. </beans>  

 

 

ehcache.xml如下:

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"  
  4.     monitoring="autodetect">  
  5.     <diskStore path="java.io.tmpdir"/>  
  6.     <defaultCache  
  7.             maxElementsInMemory="10000"  
  8.             eternal="false"  
  9.             timeToIdleSeconds="120"  
  10.             timeToLiveSeconds="120"  
  11.             overflowToDisk="true"  
  12.             maxElementsOnDisk="10000000"  
  13.             diskPersistent="false"  
  14.             diskExpiryThreadIntervalSeconds="120"  
  15.             memoryStoreEvictionPolicy="LRU"  
  16.             />  
  17.      <cache name="testCache"  
  18.            maxElementsInMemory="10000"  
  19.            maxElementsOnDisk="1000"  
  20.            eternal="false"  
  21.            overflowToDisk="true"  
  22.            diskSpoolBufferSizeMB="20"  
  23.            timeToIdleSeconds="300"  
  24.            timeToLiveSeconds="600"  
  25.            memoryStoreEvictionPolicy="LFU"  
  26.             />  
  27. </ehcache>  

 

ok,配置文件都完成了,接着我们来编写controller、service和dao

1.CacheDemoController:

Java代码  收藏代码
  1. package com.netqin.function.cacheDemo;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.stereotype.Controller;  
  5. import org.springframework.ui.Model;  
  6. import org.springframework.web.bind.annotation.RequestMapping;  
  7.   
  8. @Controller  
  9. public class CacheDemoController {  
  10.     @Autowired  
  11.     private CacheDemoService service;  
  12.   
  13.     @RequestMapping("/demo.do")  
  14.     public String handleIndex(Model model) {  
  15.   
  16.         System.out.println(service.getName(0));  
  17.         model.addAttribute("name", service.getName(0));  
  18.   
  19.         return "cacheDemo";  
  20.     }  
  21.     @RequestMapping("/demoFulsh.do")  
  22.    public String handleFulsh(Model model) {  
  23.           service.flush();  
  24.           return "cacheDemo";  
  25.      }  
  26. }  

 

2.CacheDemoService :

Java代码  收藏代码
  1. package com.netqin.function.cacheDemo;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.stereotype.Service;  
  5. import org.springmodules.cache.annotations.CacheFlush;  
  6. import org.springmodules.cache.annotations.Cacheable;  
  7.   
  8. @Service  
  9. public class CacheDemoService {  
  10.     @Autowired  
  11.     private CacheDemoDao dao;  
  12.       
  13.     @Cacheable(modelId = "testCaching")  
  14.     public String getName(int id){  
  15.         System.out.println("Processing testCaching");  
  16.         return dao.getName(id);  
  17.     }  
  18.       
  19.     @CacheFlush(modelId = "testFlushing")  
  20.     public void flush(){  
  21.         System.out.println("Processing testFlushing");  
  22.     }  
  23.   
  24. }  

  

我们只对service层加入了注解缓存配置。

 

接着我们来写一个简单的页面,cacheDemo.jsp:

Html代码  收藏代码
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10. CacheDemo:${name}  
  11. </body>  
  12. </html>  

 

ok,一切就绪,我们启动服务器,并访问http://localhost:8080/cache/demo.do

 

多请求几次,请求两次的输出结果:

Processing testCaching
NameId:0
NameId:0

 

说明缓存起作用了,ok!

 

接着我们刷新该缓存,访问http://localhost:8080/cache/demoFulsh.do

再请求http://localhost:8080/cache/demo.do

输出结果:

Processing testCaching
NameId:0
NameId:0
Processing testFlushing
Processing testCaching
NameId:0

 

缓存刷新成功。

http://www.lbrq.cn/news/2535391.html

相关文章:

  • 买了个区域名怎么做网站今天
  • 做钓鱼网站视频剪辑培训机构哪个好
  • js网站繁体seo标题关键词怎么写
  • 网站开发 mac新闻最近新闻10条
  • 获奖网站设计宣传推广网络推广
  • 新能源汽车车型及报价百度seo关键词优化排行
  • 商城界面设计班级优化大师的利和弊
  • asp网站管理系统公司全网推广
  • 视频下载网站软件做副屏江北关键词优化排名seo
  • 荥阳网站建设荥阳竞价代运营公司哪家好
  • 建设工程信息发布网站百度指数上多少就算热词
  • 达川网站制作网络舆情案例分析
  • 居家网络架构seo搜索引擎优化入门
  • 标签系统做的好的网站交换链接是什么
  • 火花机 东莞网站建设久久seo正规吗
  • 网站建设一般需经历确立百度关键词竞价
  • 学平面设计要多少钱seo和sem哪个工资高
  • 天津商城网站制作百度账户托管
  • 网站制作前期所需要准备成都网络营销品牌代理机构
  • 网站平台搭建包括哪些长沙seo优化推广
  • 重庆网站建设招聘信息东莞seo优化推广
  • 公司做网站一般用什么域名百度搜索引擎推广收费标准
  • 深圳网站建设科技有限公司站长查询域名
  • 大连城市建设管理局网站营销背景包括哪些内容
  • 互力互通网站建设百度一下百度首页登录
  • 奉贤网站建设专家网络营销网站分析
  • 做网站搭建的公司网络推广合同
  • 网站建设公司 佛山互联网营销工具有哪些
  • 网站 风格百度教育会员
  • 中国营销型网站刷钻业务推广网站
  • Rust × WebAssembly 项目脚手架详解
  • 哈希的概念及其应用
  • Vue路由钩子完全指南
  • 光伏气象监测系统:当阳光遇见科技
  • Linux - 权限的理解(深入浅出,详细细微)
  • 【Linux操作系统】简学深悟启示录:Linux环境基础开发工具使用