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容器以及编码过滤器
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
- id="WebApp_ID" version="3.0">
- <!-- 配置spring容器 -->
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:applicationContext.xml</param-value><!-- 类路径下的applicationContext.xml文件 -->
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <!-- 配置springmvc容器 -->
- <servlet>
- <servlet-name>springDispatcherServlet</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:springmvc.xml</param-value><!-- 类路径下的springmvc.xml文件 -->
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>springDispatcherServlet</servlet-name>
- <url-pattern>/*</url-pattern><!-- 拦截所有请求 -->
- </servlet-mapping>
- <!-- 编码过滤器 -->
- <filter>
- <filter-name>charsetEncodingFilter</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>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>charsetEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- </web-app>
(2)配置springmvc.xml
在与src同级目录下建立"source Folder"(eclipse下),命名为config
主要是配置扫描的包、视图解析器、静态资源文件以及开启注解模式
- <?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:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
- <!-- 配置自动扫描的包 -->
- <context:component-scan base-package="com.yusys">
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
- </context:component-scan>
- <!-- 配置视图解析器 -->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/"></property><!-- 视图路径 -->
- <property name="suffix" value=".jsp"></property><!-- 视图后缀名 -->
- </bean>
- <!-- 配置静态资源文件 -->
- <mvc:default-servlet-handler/>
- <!-- 开启注解模式 -->
- <mvc:annotation-driven/>
- </beans>
(3)配置applicationContext.xml
在config文件夹下建立applicationContext.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:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
- <!-- 配置扫描的包 -->
- <context:component-scan base-package="com.yusys">
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
- </context:component-scan>
- </beans>
(4)配置完后初步结果
(5)测试ApplicationContext
- package com.yusys.junit;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class SpringRedisTest {
- private static ApplicationContext applicationContext;
- static{
- applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
- }
- @Test
- public void testApplicationContext(){
- System.out.println(applicationContext);
- }
- }
报错:
通过错误可以知道缺少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的一些设置
- # Redis Setting
- # Redis默认有16个库,序号是0-15,默认是选中的是0号数据库
- spring.redis.database=0
- # Redis服务器地址
- spring.redis.host=127.0.0.1
- # Redis服务器连接端口,默认是6379
- spring.redis.port=6379
- # Redis服务器连接密码(默认为空)
- spring.redis.password=
- # 连接池最大连接数(使用负值表示没有限制),根据实际情况修改
- spring.redis.pool.maxActive=8
- # 连接池最大阻塞等待时间(使用负值表示没有限制),根据实际情况修改
- spring.redis.pool.maxWait=-1
- # 连接池中的最大空闲连接,根据实际情况修改
- spring.redis.pool.maxIdle=8
- # 连接池中的最小空闲连接,根据实际情况修改
- spring.redis.pool.minIdle=0
- # 连接超时时间(毫秒),根据实际情况修改
- spring.redis.timeout=2000
(2)在config下创建spring-data-redis.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:cache="http://www.springframework.org/schema/cache"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:redis="http://www.springframework.org/schema/redis" xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis-1.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
- <!-- 载入redis.properties,这里要特别注意,如果有多个properties文件,必须用逗号分开,不能写成两个 <context:property-placeholder/> -->
- <context:property-placeholder location="classpath:redis.properties" />
- <!-- 配置JedisPoolConfig相关参数 -->
- <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
- <property name="maxActive" value="${spring.redis.pool.maxActive}"></property>
- <property name="maxIdle" value="${spring.redis.pool.maxIdle}"></property>
- <property name="minIdle" value="${spring.redis.pool.minIdle}"></property>
- <property name="maxWait" value="${spring.redis.pool.maxWait}"></property>
- <property name="testOnBorrow" value="${spring.redis.pool.testOnBorrow}"></property>
- <property name="testOnReturn" value="${spring.redis.pool.testOnReturn}"></property>
- </bean>
- <!-- 配置redis服务器信息 -->
- <bean id="connectionFactory"
- class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
- <property name="poolConfig" ref="poolConfig"></property>
- <property name="hostName" value="${spring.redis.host}"></property>
- <property name="port" value="${spring.redis.port}"></property>
- <property name="password" value="${spring.redis.password}"></property>
- <property name="database" value="${spring.redis.database}"></property>
- <property name="timeout" value="${spring.redis.timeout}"></property>
- </bean>
- <!-- 配置RedisTemplate -->
- <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
- <property name="connectionFactory" ref="connectionFactory"></property>
- <property name="keySerializer">
- <bean
- class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
- </property>
- <property name="valueSerializer">
- <bean
- class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>
- </property>
- <property name="hashKeySerializer">
- <bean
- class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
- </property>
- <!-- 使用JacksonJsonRedisSerializer需要引入jar包:barchart-wrap-jackson-1.8.6-build001.jar -->
- <!-- JacksonJsonRedisSerializer 需要一个有参的构造函数,因此需要配置constructor-arg -->
- <property name="hashValueSerializer">
- <bean
- class="org.springframework.data.redis.serializer.JacksonJsonRedisSerializer">
- <constructor-arg type="java.lang.Class" value="java.lang.Object"></constructor-arg>
- </bean>
- </property>
- </bean>
- <!-- 配置redis连接池 -->
- <bean class="redis.clients.jedis.JedisPool">
- <constructor-arg ref="poolConfig" />
- <constructor-arg value="${spring.redis.host}" />
- <constructor-arg type="int" value="${spring.redis.port}" />
- <constructor-arg type="int" value="${spring.redis.timeout}" />
- <constructor-arg type="java.lang.String" value="${spring.redis.password}" />
- <constructor-arg type="int" value="${spring.redis.database}" />
- </bean>
- </beans>
(3)在applicationContext.xml中引入spring-data-redis.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:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
- <!-- 配置扫描的包 -->
- <context:component-scan base-package="com.yusys">
- <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
- <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
- </context:component-scan>
- <!-- 引入spring-data-redis.xml -->
- <import resource="spring-data-redis.xml"/>
- </beans>
(4)测试是否连接成功
- package com.yusys.junit;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.data.redis.core.RedisTemplate;
- public class SpringRedisTest {
- private static ApplicationContext applicationContext;
- static{
- applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
- }
- @Test
- public void testRedisConnection(){
- RedisTemplate redisTemplate = (RedisTemplate)applicationContext.getBean("redisTemplate");
- redisTemplate.renameIfAbsent("abc", "bbb");//如果key=abc存在,则将key修改为bbb
- System.out.println(redisTemplate);
- }
- }
(5)如何查看是否成功
这里介绍一个比较low的可视化软件:Redis Desktop Manager
通过修改可以看到key已经从abc变为bbb了,测试成功!