SpringBoot整合SpringDataReids和SpringSession實現Session共享

easonjim發表於2017-11-10

首先,需要先整合Redis的支援,參考:http://www.cnblogs.com/EasonJim/p/7805665.html

Spring Boot整合Spring Data Redis+Spring Session非常的簡單,也不用擔心版本問題,只需要引入相應的包即可。再次感嘆一下Spring Boot對於版本的控制做的真的太好了。

小提示:如果在做Spring MVC時如果問題出現在版本上出現網上找不到解決方案的BUG時,可以參考Spring Boot引入的版本來知道Redis和Session用的是什麼版本。比如這個專案上使用的1.4.7的Spring Boot,那麼MVC用的是4.3.9,Redis為1.7.11,Jedis為2.8.2。

整合步驟:

POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.jsoft.springboottest</groupId>
    <artifactId>springboottest1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>springboottest1</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.7.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Add typical dependencies for a web application -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- 熱部署模組 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional> <!-- 這個需要為 true 熱部署才有效 -->
        </dependency>
        
        <!-- Redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        
        <!-- Session -->
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>
        
    </dependencies>

    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

使用@EnableRedisHttpSession開啟Redis的Session支援,直接新建一個類。

package com.jsoft.springboottest.springboottest1;

import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 60 * 60 * 24)
public class SessionConfig {
}

maxInactiveIntervalInSeconds為過期時間,單位為秒。

通過上面基本完成了,Redis整合部分參考上一篇文章,叢集和單機的都行。

示例工程:https://github.com/easonjim/5_java_example/tree/master/springboottest/springboottest7

 

參考:

http://www.jb51.net/article/112923.htm

http://blog.csdn.net/zl18310999566/article/details/54290994

http://blog.csdn.net/l1028386804/article/details/65081718

http://blog.csdn.net/xiaoyu411502/article/details/48603597

https://www.cnblogs.com/mengmeng89012/p/5519698.html

http://www.cnblogs.com/ityouknow/p/5748830.html

https://projects.spring.io/spring-session/(Spring Session官網,一切都是這個入口,Spring Session Data Redis只是其中一個小模組)

https://docs.spring.io/spring-session/docs/current/reference/html5/guides/boot.html(官方Spring Boot整合文件)

https://docs.spring.io/spring-session/docs/current/reference/html5/(官方總配置文件,下面有個表格可以進入檢視)

https://docs.spring.io/spring-session/docs/current/reference/html5/guides/custom-cookie.html(官方文件,實現Session的key修改和作用域)

https://docs.spring.io/spring-session/docs/current/reference/html5/guides/users.html(官方文件,實現多個Session)

==>如有問題,請聯絡我:easonjim#163.com,或者下方發表評論。<==


相關文章