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)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內(nèi)容請查看下面相關鏈接
- Spring集成jedis的配置與使用簡單實例
- springmvc配置線程池Executor做多線程并發(fā)操作的代碼實例
- Spring線程池ThreadPoolExecutor配置并且得到任務執(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各版本新特性的介紹
相關文章
SpringBoot之多環(huán)境打包與配置文件排除方式
這篇文章主要介紹了SpringBoot之多環(huán)境打包與配置文件排除方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04
詳解poi+springmvc+springjdbc導入導出excel實例
本篇文章主要介紹了poi+springmvc+springjdbc導入導出excel實例,非常具有實用價值,需要的朋友可以參考下。2017-01-01
springboot?靜態(tài)方法中使用@Autowired注入方式
這篇文章主要介紹了springboot?靜態(tài)方法中使用@Autowired注入方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
為什么程序中突然多了 200 個 Dubbo-thread 線程的說明
這篇文章主要介紹了為什么程序中突然多了 200 個 Dubbo-thread 線程的說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09

