原创

Spring Boot(六)集成Spring session redis


项目GitHub地址 :

https://github.com/FrameReserve/TrainingBoot


Spring Boot(六)集成Spring session redis ,标记地址:

https://github.com/FrameReserve/TrainingBoot/releases/tag/0.0.6


Centos7 安装 Redis 3.2.0

http://blog.csdn.NET/a286352250/article/details/51603189


Centos7 开机启动 Redis 3.2.0

http://blog.csdn.Net/a286352250/article/details/52595387


Spring Boot(五)集成 Redis
http://blog.csdn.net/a286352250/article/details/53157201



pom.xml

<dependency>
			<groupId>org.springframework.session</groupId>
			<artifactId>spring-session-data-redis</artifactId>
		</dependency>
		
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>


web.xml集成 Spring Session Redis

src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <display-name>Archetype Created Web Application</display-name>
    
    <!-- delegatingFilterProxy -->  
    <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>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    
</web-app>


测试Session:

src/main/java/com/training/redis/controller/DemoRedisController.java

package com.training.redis.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.training.core.controller.BaseRedisController;
import com.training.core.dto.Pair;
import com.training.core.dto.ResultDataDto;
import com.training.redis.dto.DemoSessionDto;
import com.training.redis.entity.DemoRedis;
import com.training.redis.service.DemoRedisService;

@RestController
@RequestMapping(value="/demoRedis") 
public class DemoRedisController extends BaseRedisController<DemoRedis> {

	@Resource
	private DemoRedisService demoRedisService;
	
	/**
	 * 查询所有Session
	 */
	@ApiOperation(value="查询所有Session", notes="getEntityById")
    @RequestMapping(value = "/getSesstion", method = RequestMethod.GET)
	public @ResponseBody ResultDataDto getEntityById(HttpSession httpSession) {
    	
    	Enumeration<String> sessionkeys = httpSession.getAttributeNames();
    	
    	List<Pair<String, String>> list = new ArrayList<Pair<String,String>>();
    	while (sessionkeys.hasMoreElements()) {
			String key = (String) sessionkeys.nextElement();
			list.add(new Pair<String, String>(key, httpSession.getAttribute(key).toString()));
		}
		return ResultDataDto.addSuccess().setDatas(list);
	}
    
    /**
	 * 新增测试Session
	 */
	@ApiOperation(value="新增测试Session", notes="getEntityById")
    @RequestMapping(value = "/addSesstion", method = RequestMethod.GET)
	public @ResponseBody ResultDataDto addEntity(HttpSession httpSession) {
    	DemoSessionDto session = new DemoSessionDto();
    	session.setKey("testKey");
    	session.setValue("testValue");
    	httpSession.setAttribute(session.getKey(), session.getValue());
		return ResultDataDto.addAddSuccess();
	}
    
}


测试:

http://localhost:8080/addSesstion

重启 Tomcat

http://localhost:8080/getSesstion

Session依旧存在












正文到此结束
Loading...