Spring Boot集成Spring Data Reids和Spring Session實現Session共享

首先,須要先集成Redis的支持,參考:http://www.cnblogs.com/EasonJim/p/7805665.htmlhtml

Spring Boot集成Spring Data Redis+Spring Session很是的簡單,也不用擔憂版本問題,只須要引入相應的包便可。再次感嘆一下Spring Boot對於版本的控制作的真的太好了。html5

小提示:若是在作Spring MVC時若是問題出如今版本上出現網上找不到解決方案的BUG時,能夠參考Spring Boot引入的版原本知道Redis和Session用的是什麼版本。好比這個項目上使用的1.4.7的Spring Boot,那麼MVC用的是4.3.9,Redis爲1.7.11,Jedis爲2.8.2。java

集成步驟:git

POM:github

<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支持,直接新建一個類。web

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

經過上面基本完成了,Redis集成部分參考上一篇文章,集羣和單機的都行。spring

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

 

參考:springboot

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

http://www.javashuo.com/article/p-gljtnqbk-ge.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)

相關文章
相關標籤/搜索