java单体应用redis处理session共享
1. pom添加依赖
<dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>1.2.1.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.1</version> </dependency>
2. spring-redis.xml
<context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true" /> <!-- session设置 --> <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> <property name="maxInactiveIntervalInSeconds" value="3600"></property> </bean> <!-- redis连接池 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"/> <!-- redis连接工厂 --> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="${redis.hostName}"/> <property name="port" value="${redis.port}"/> <property name="database" value="${redis.database}"></property> <property name="password" value="${redis.password}"/> <property name="timeout" value="${redis.timeout}"/> <property name="poolConfig" ref="poolConfig"></property> </bean>
3. redis.properties
redis.hostName=
redis.port=6379
redis.password=
redis.database=5
redis.timeout=200000
4.web.xml添加拦截器
<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>
5.在controller里面操作session就会存到redis
//存入session、redis request.getSession().setAttribute("userLogin", user); //先会去redis找,然后session中找。 request.getSession.getAttribute("userLogin");
ya tudou