SpringMVC+redis整合-上篇

 SpringMVC+redis整合-上篇

http://blog.csdn.net/LINABC123000/article/details/68922941

之前一直有听说过redis,一直想整合SpringMVC+redis,但是无奈一直没有时间。这次趁着到新公司上班,暂时没有什么任务的间隔,搭建了SpringMVC+redis。由于多redis的理解比较粗浅,如有说的不对的地方,欢迎指正,共同进步!同时后续的文章都是基于该环境进行更高层析的修改的。

 

本文基于eclipse+springmvc+redis开发。

一、搭建SpringMVC环境

之前已经写过了一篇Spring+SpringMVC+hibernate的文章,在这里有就不做过多的解释,直接开始撸代码。

1. 引入jar包

(1)引入springMVC相关的jar包

使用的spring版本:spring-framework-4.0.0.RELEASE,在WEB-INF/lib添加如下jar包:

spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-orm-4.0.0.RELEASE.jar
spring-tx-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar

(2)加上第三方核心包

commons-logging-1.2.jar

2. 配置web.xml

主要是配置Spring容器、springMVC容器以及编码过滤器

 

[html] view plain copy
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
  5.     id="WebApp_ID" version="3.0">  
  6.   
  7.     <!-- 配置spring容器 -->  
  8.     <context-param>  
  9.         <param-name>contextConfigLocation</param-name>  
  10.         <param-value>classpath:applicationContext.xml</param-value><!-- 类路径下的applicationContext.xml文件 -->  
  11.     </context-param>  
  12.   
  13.     <listener>  
  14.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  15.     </listener>  
  16.       
  17.     <!-- 配置springmvc容器 -->  
  18.     <servlet>  
  19.         <servlet-name>springDispatcherServlet</servlet-name>  
  20.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  21.         <init-param>  
  22.             <param-name>contextConfigLocation</param-name>  
  23.             <param-value>classpath:springmvc.xml</param-value><!-- 类路径下的springmvc.xml文件 -->  
  24.         </init-param>  
  25.         <load-on-startup>1</load-on-startup>  
  26.     </servlet>  
  27.   
  28.     <servlet-mapping>  
  29.         <servlet-name>springDispatcherServlet</servlet-name>  
  30.         <url-pattern>/*</url-pattern><!-- 拦截所有请求 -->  
  31.     </servlet-mapping>  
  32.       
  33.     <!-- 编码过滤器 -->  
  34.     <filter>  
  35.         <filter-name>charsetEncodingFilter</filter-name>  
  36.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  37.         <init-param>  
  38.             <param-name>encoding</param-name>  
  39.             <param-value>UTF-8</param-value>  
  40.         </init-param>  
  41.         <init-param>  
  42.             <param-name>forceEncoding</param-name>  
  43.             <param-value>true</param-value>  
  44.         </init-param>  
  45.     </filter>  
  46.       
  47.     <filter-mapping>  
  48.         <filter-name>charsetEncodingFilter</filter-name>  
  49.         <url-pattern>/*</url-pattern>  
  50.     </filter-mapping>  
  51. </web-app>  

 

(2)配置springmvc.xml

在与src同级目录下建立"source Folder"(eclipse下),命名为config

主要是配置扫描的包、视图解析器、静态资源文件以及开启注解模式

 

[html] view plain copy
 
  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:mvc="http://www.springframework.org/schema/mvc"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd  
  7.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">  
  9.   
  10.     <!-- 配置自动扫描的包  -->  
  11.     <context:component-scan base-package="com.yusys">  
  12.         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  13.         <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>  
  14.     </context:component-scan>  
  15.       
  16.     <!-- 配置视图解析器 -->  
  17.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
  18.         <property name="prefix" value="/"></property><!-- 视图路径 -->  
  19.         <property name="suffix" value=".jsp"></property><!-- 视图后缀名 -->  
  20.     </bean>  
  21.       
  22.     <!-- 配置静态资源文件 -->  
  23.     <mvc:default-servlet-handler/>  
  24.       
  25.     <!-- 开启注解模式 -->  
  26.     <mvc:annotation-driven/>  
  27.       
  28. </beans>  


(3)配置applicationContext.xml

 

在config文件夹下建立applicationContext.xml文件

 

[html] view plain copy
 
  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:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
  8.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">  
  9.   
  10.     <!-- 配置扫描的包 -->  
  11.     <context:component-scan base-package="com.yusys">  
  12.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  13.         <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>  
  14.     </context:component-scan>  
  15.       
  16. </beans>  

 

(4)配置完后初步结果

 

(5)测试ApplicationContext

 

[java] view plain copy
 
  1. package com.yusys.junit;  
  2.   
  3. import org.junit.Test;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6.   
  7. public class SpringRedisTest {  
  8.   
  9.     private static ApplicationContext applicationContext;  
  10.       
  11.     static{  
  12.         applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");  
  13.     }  
  14.       
  15.     @Test  
  16.     public void testApplicationContext(){  
  17.         System.out.println(applicationContext);  
  18.     }  
  19.       
  20. }  


报错:

 

通过错误可以知道缺少aop包:添加spring-aop-4.0.0.RELEASE.jar 到lib文件夹下,并且Build Path --> Add to Build Path

二、整合redis

需要jar包:

jedis-2.1.0.jar
commons-pool-1.5.4.jar
spring-data-redis-1.0.2.RELEASE.jar

在这里推荐一个下载jar的地方:http://search.maven.org/

(1)在config下创建redis.properties,主要是redis的一些设置

 

[plain] view plain copy
 
  1. # Redis Setting  
  2. # Redis默认有16个库,序号是0-15,默认是选中的是0号数据库  
  3. spring.redis.database=0  
  4. # Redis服务器地址  
  5. spring.redis.host=127.0.0.1  
  6. # Redis服务器连接端口,默认是6379  
  7. spring.redis.port=6379  
  8. # Redis服务器连接密码(默认为空)  
  9. spring.redis.password=  
  10. # 连接池最大连接数(使用负值表示没有限制),根据实际情况修改  
  11. spring.redis.pool.maxActive=8  
  12. # 连接池最大阻塞等待时间(使用负值表示没有限制),根据实际情况修改  
  13. spring.redis.pool.maxWait=-1  
  14. # 连接池中的最大空闲连接,根据实际情况修改  
  15. spring.redis.pool.maxIdle=8  
  16. # 连接池中的最小空闲连接,根据实际情况修改  
  17. spring.redis.pool.minIdle=0  
  18. # 连接超时时间(毫秒),根据实际情况修改  
  19. spring.redis.timeout=2000  


(2)在config下创建spring-data-redis.xml

 

 

 

[html] view plain copy
 
  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:cache="http://www.springframework.org/schema/cache"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:redis="http://www.springframework.org/schema/redis" xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd  
  7.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
  9.         http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis-1.0.xsd  
  10.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">  
  11.   
  12.     <!-- 载入redis.properties,这里要特别注意,如果有多个properties文件,必须用逗号分开,不能写成两个 <context:property-placeholder/> -->  
  13.     <context:property-placeholder location="classpath:redis.properties" />  
  14.   
  15.     <!-- 配置JedisPoolConfig相关参数 -->  
  16.     <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
  17.         <property name="maxActive" value="${spring.redis.pool.maxActive}"></property>  
  18.         <property name="maxIdle" value="${spring.redis.pool.maxIdle}"></property>  
  19.         <property name="minIdle" value="${spring.redis.pool.minIdle}"></property>  
  20.         <property name="maxWait" value="${spring.redis.pool.maxWait}"></property>  
  21.         <property name="testOnBorrow" value="${spring.redis.pool.testOnBorrow}"></property>  
  22.         <property name="testOnReturn" value="${spring.redis.pool.testOnReturn}"></property>  
  23.     </bean>  
  24.   
  25.     <!-- 配置redis服务器信息 -->  
  26.     <bean id="connectionFactory"  
  27.         class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">  
  28.         <property name="poolConfig" ref="poolConfig"></property>  
  29.         <property name="hostName" value="${spring.redis.host}"></property>  
  30.         <property name="port" value="${spring.redis.port}"></property>  
  31.         <property name="password" value="${spring.redis.password}"></property>  
  32.         <property name="database" value="${spring.redis.database}"></property>  
  33.         <property name="timeout" value="${spring.redis.timeout}"></property>  
  34.     </bean>  
  35.   
  36.     <!-- 配置RedisTemplate -->  
  37.     <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
  38.         <property name="connectionFactory" ref="connectionFactory"></property>  
  39.         <property name="keySerializer">  
  40.             <bean  
  41.                 class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>  
  42.         </property>  
  43.         <property name="valueSerializer">  
  44.             <bean  
  45.                 class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>  
  46.         </property>  
  47.   
  48.         <property name="hashKeySerializer">  
  49.             <bean  
  50.                 class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>  
  51.         </property>  
  52.         <!-- 使用JacksonJsonRedisSerializer需要引入jar包:barchart-wrap-jackson-1.8.6-build001.jar -->  
  53.         <!-- JacksonJsonRedisSerializer 需要一个有参的构造函数,因此需要配置constructor-arg -->  
  54.         <property name="hashValueSerializer">  
  55.             <bean  
  56.                 class="org.springframework.data.redis.serializer.JacksonJsonRedisSerializer">  
  57.                 <constructor-arg type="java.lang.Class" value="java.lang.Object"></constructor-arg>  
  58.             </bean>  
  59.         </property>  
  60.     </bean>  
  61.   
  62.     <!-- 配置redis连接池 -->  
  63.     <bean class="redis.clients.jedis.JedisPool">  
  64.         <constructor-arg ref="poolConfig" />  
  65.         <constructor-arg value="${spring.redis.host}" />  
  66.         <constructor-arg type="int" value="${spring.redis.port}" />  
  67.         <constructor-arg type="int" value="${spring.redis.timeout}" />  
  68.         <constructor-arg type="java.lang.String" value="${spring.redis.password}" />  
  69.         <constructor-arg type="int" value="${spring.redis.database}" />  
  70.     </bean>  
  71. </beans>  


(3)在applicationContext.xml中引入spring-data-redis.xml文件:

 

 

[html] view plain copy
 
  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:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
  8.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">  
  9.   
  10.     <!-- 配置扫描的包 -->  
  11.     <context:component-scan base-package="com.yusys">  
  12.         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
  13.         <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>  
  14.     </context:component-scan>  
  15.       
  16.     <!-- 引入spring-data-redis.xml -->  
  17.     <import resource="spring-data-redis.xml"/>  
  18. </beans>  


(4)测试是否连接成功

 

 

[java] view plain copy
 
  1. package com.yusys.junit;  
  2.   
  3. import org.junit.Test;  
  4. import org.springframework.context.ApplicationContext;  
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  6. import org.springframework.data.redis.core.RedisTemplate;  
  7.   
  8. public class SpringRedisTest {  
  9.   
  10.     private static ApplicationContext applicationContext;  
  11.       
  12.     static{  
  13.         applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");  
  14.     }  
  15.       
  16.     @Test  
  17.     public void testRedisConnection(){  
  18.         RedisTemplate redisTemplate = (RedisTemplate)applicationContext.getBean("redisTemplate");  
  19.         redisTemplate.renameIfAbsent("abc", "bbb");//如果key=abc存在,则将key修改为bbb  
  20.         System.out.println(redisTemplate);  
  21.     }  
  22.       
  23. }  


(5)如何查看是否成功

 

这里介绍一个比较low的可视化软件:Redis Desktop Manager

通过修改可以看到key已经从abc变为bbb了,测试成功!

posted @ 2017-12-01 09:45  wjj1013  阅读(256)  评论(0编辑  收藏  举报