SpringSessionRedis配置及發(fā)現(xiàn)的問題講解
最近寫項目,需要把session放入Redis中,來實現(xiàn)分布式。我本來要用Tomcat部署Redis這種方法,但是依賴于容器了。無意中發(fā)現(xiàn)了SpringSession,這可挺不錯的,寫完了發(fā)現(xiàn)不好用,問度娘也沒弄明白,最后我寫了2個demo一個springMVC的,一個spring整合struts2的,發(fā)現(xiàn)SpringSession需要SpringMVC的支持。也就是說我的項目用不了了。
先說說springsession的配置吧:
一、Maven中pom.xml文件中添加(選一種添加上就行):
<span style="white-space:pre"> </span><!--1、redis-整合--> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>1.0.2.RELEASE</version> </dependency> <!-- 2、Redis --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.4.2.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.5.2</version> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session</artifactId> <version>1.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.2</version> </dependency>
二、在spring配置文件(applicationContext.xml)中添加代碼:
<span style="white-space:pre"> </span><!-- 自動掃描 --> <context:annotation-config/> <!-- 配置spring-session --> <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> <!-- 過期時間100分鐘 --> <property name="maxInactiveIntervalInSeconds" value="6000"></property> </bean> <!-- redis連接池 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" /> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" > <property name="hostName" value="10.4.120.180" /> <property name="port" value="6379" /> <property name="poolConfig" ref="jedisPoolConfig" /> </bean>
三、在web.xml中添加過濾即可:
<span style="white-space:pre"> </span><!-- Spring Session的Filter --> <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> <session-config> <session-timeout>30</session-timeout> </session-config>
這樣就自動將session放入到reids庫中了。
補充:
Spring的版本為4.1.6以上
javax.servlet-api需要3.0.1版本以上
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
- Spring集成jedis的配置與使用簡單實例
- springmvc配置線程池Executor做多線程并發(fā)操作的代碼實例
- Spring線程池ThreadPoolExecutor配置并且得到任務(wù)執(zhí)行的結(jié)果
- mysql+spring+mybatis實現(xiàn)數(shù)據(jù)庫讀寫分離的代碼配置
- Spring框架十一種常見異常的解決方法匯總
- Spring Boot中使用Spring-data-jpa的配置方法詳解
- SpringBoot thymeleaf eclipse熱部署方案操作步驟
- Spring boot配置文件加解密詳解
- Spring AOP中的JDK和CGLib動態(tài)代理哪個效率更高?
- Spring各版本新特性的介紹
相關(guān)文章
10張圖總結(jié)出并發(fā)編程最佳學(xué)習(xí)路線
這篇文章主要介紹了并發(fā)編程的最佳學(xué)習(xí)路線,文中通過圖片介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-08-08JAVA環(huán)境搭建之MyEclipse10+jdk1.8+tomcat8環(huán)境搭建詳解
本文詳細(xì)講解了MyEclipse10+jdk1.8+tomcat8的JAVA環(huán)境搭建方法,希望能幫助到大家2018-10-10Spring?Boot之Validation自定義實現(xiàn)方式的總結(jié)
這篇文章主要介紹了Spring?Boot之Validation自定義實現(xiàn)方式的總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07關(guān)于Unsupported major.minor version 49.0的錯誤解決辦法
這篇文章主要介紹了關(guān)于Unsupported major.minor version 49.0的錯誤解決辦法的相關(guān)資料,需要的朋友可以參考下2015-11-11Java中線程組ThreadGroup與線程池的區(qū)別及示例
這篇文章主要介紹了Java中線程組與線程池的區(qū)別及示例,ThreadGroup是用來管理一組線程的,可以控制線程的執(zhí)行,查看線程的執(zhí)行狀態(tài)等操作,方便對于一組線程的統(tǒng)一管理,需要的朋友可以參考下2023-05-05Spring AOP如何實現(xiàn)注解式的Mybatis多數(shù)據(jù)源切換詳解
這篇文章主要給大家介紹了關(guān)于Spring AOP如何實現(xiàn)注解式的Mybatis多數(shù)據(jù)源切換的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11