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