linux下安裝配置redis可參考連接: http://www.javashuo.com/article/p-mutjyynk-cx.htmllinux
應用容器: tomcatweb
<dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>1.2.2.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.2</version> </dependency>
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <!-- 路徑值本身結合實際配置 --> <param-value> /WEB-INF/config/spring/spring-*.xml </param-value> </context-param> <!-- 過濾器 --> <filter> <filter-name>springSessionRepositoryFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSessionRepositoryFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
當項目開發過程當中進行單機調試時可將上面的過濾器註釋掉,以便調試redis
<!-- 對象池配置 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="3"/> <!-- 控制一個pool可分配多少個jedis實例 --> <property name="maxIdle" value="3" /> <!-- 控制一個pool最多有多少個狀態爲idle(空閒)的jedis實例 --> <property name="minIdle" value="1"/> <property name="maxWaitMillis" value="1000" /> <!-- 表示當borrow一個jedis實例時,最大的等待時間,若是超過等待時間,則直接拋出JedisConnectionException --> <property name="testOnBorrow" value="false" /> <!-- 在borrow一個jedis實例時,是否提早進行validate操做;若是爲true,則獲得的jedis實例均是可用的 --> <property name="testOnReturn" value="false"/> <property name="testWhileIdle" value="true"/> </bean> <!-- 工廠實現 --> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy"> <property name="hostName" value="192.168.133.141" /> <property name="port" value="6380" /> <property name="database" value="0" /> <property name="password" value="123456"/> <property name="poolConfig" ref="jedisPoolConfig" /> </bean> <!-- 模板類 --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory"/> </bean> <!-- 使用spring-session把http session放到redis裏 --> <bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> <!-- maxInactiveIntervalInSeconds 屬性是session的過時時間, 單位秒 --> <property name="maxInactiveIntervalInSeconds" value="3600" /> </bean>
存放的數據對象必須實現Serializable接口,且有serialVersionUID屬性,否則會出錯spring
spring-session-data-redis依賴衝突時可能致使程序報錯tomcat