5. SpringMVC+redis整合-上篇
之前一直有聽說過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了,測試成功!