技术库 > Java

使用spring-session把http session放到redis里面

技术库:tec.5lulu.com

新建maven的webapp项目,加入spring session的相关依赖

from:tec.5lulu.com

pom.xml 如下: 

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.       
  4.     <modelVersion>4.0.0</modelVersion>  
  5.     <groupId>com.lala</groupId>  
  6.     <artifactId>leshop</artifactId>  
  7.     <packaging>war</packaging>  
  8.     <version>0.0.1-SNAPSHOT</version>  
  9.       
  10.     <name>taobao Maven Webapp</name>  
  11.     <url>http://maven.apache.org</url>  
  12.       
  13.     <dependencies>  
  14.         <dependency>  
  15.             <groupId>junit</groupId>  
  16.             <artifactId>junit</artifactId>  
  17.             <version>4.10</version>  
  18.             <scope>test</scope>  
  19.         </dependency>  
  20.         <dependency>  
  21.             <groupId>javax.servlet</groupId>  
  22.             <artifactId>jsp-api</artifactId>  
  23.             <version>2.0</version>  
  24.             <scope>provided</scope>  
  25.         </dependency>  
  26.   
  27.         <dependency>  
  28.             <groupId>javax.servlet</groupId>  
  29.             <artifactId>javax.servlet-api</artifactId>  
  30.             <version>3.0.1</version>  
  31.             <scope>provided</scope>  
  32.         </dependency>  
  33.         <dependency>  
  34.             <groupId>org.springframework.session</groupId>  
  35.             <artifactId>spring-session-data-redis</artifactId>  
  36.             <version>1.0.1.RELEASE</version>  
  37.         </dependency>  
  38.         <dependency>  
  39.             <groupId>org.springframework</groupId>  
  40.             <artifactId>spring-web</artifactId>  
  41.             <version>4.1.6.RELEASE</version>  
  42.         </dependency>  
  43.         <dependency>  
  44.             <groupId>org.springframework.session</groupId>  
  45.             <artifactId>spring-session</artifactId>  
  46.             <version>1.0.1.RELEASE</version>  
  47.         </dependency>  
  48.         <dependency>  
  49.             <groupId>com.orange.redis-embedded</groupId>  
  50.             <artifactId>embedded-redis</artifactId>  
  51.             <version>0.6</version>  
  52.         </dependency>  
  53.     </dependencies>  
  54.     <build>  
  55.         <finalName>leshop</finalName>  
  56.         <plugins>  
  57.             <plugin>  
  58.                 <groupId>org.apache.maven.plugins</groupId>  
  59.                 <artifactId>maven-compiler-plugin</artifactId>  
  60.                 <version>3.3</version>  
  61.                 <configuration>  
  62.                     <source>1.8</source>  
  63.                     <target>1.8</target>  
  64.                     <verbose>true</verbose>  
  65.                 </configuration>  
  66.             </plugin>  
  67.         </plugins>  
  68.     </build>  
  69. </project>  

配置redis环境

1:安装redis,启动 

  1. package com.lala.config;  
  2.   
  3. import org.springframework.context.annotation.Bean;  
  4. import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;  
  5. import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;  
  6.   
  7. @EnableRedisHttpSession  
  8. public class SessionConfig   
  9. {  
  10.     @Bean  
  11.     public JedisConnectionFactory connectionFactory()  
  12.     {  
  13.         JedisConnectionFactory connection = new JedisConnectionFactory();  
  14.         connection.setPort(6379);  
  15.         connection.setHostName("192.168.1.106");  
  16.         return connection;  
  17.     }  
  18. }  
 
  1. package com.lala.config;  
  2.   
  3. import org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer;  
  4.   
  5. public class SessionInitializer extends AbstractHttpSessionApplicationInitializer   
  6. {  
  7.     public SessionInitializer()   
  8.     {  
  9.         super(SessionConfig.class);  
  10.     }  
  11. }  

在servlet或者action里面 

  1. Integer uid = Integer.valueOf(req.getParameter("uid"));  
  2. req.getSession().setAttribute("user"new User(uid, "张三""这里是redis的"));  
执行之后,发现session已经保存在redis里面了。

这种做法,不需要依赖具体的容器。放到任何容器里面都可以使用。

不像网上的做法,使用其他的jar,还需要配置tomcat。这种做法,不支持其他的web server

使用spring-session把http session放到redis里面


本文链接 http://tec.5lulu.com/detail/110drn2eh9gzm854c.html

我来评分 :5
0

转载注明:转自5lulu技术库

本站遵循:署名-非商业性使用-禁止演绎 3.0 共享协议

www.5lulu.com