- 使用Spring Boot以及Gradle建構項目
-
加入依賴
compile ‘org.springframework.session:spring-session:1.2.0.RELEASE’
compile ‘org.springframework.boot:spring-boot-starter-data-redis’
- 使用注解 @EnableRedisHttpSession
-
application.properties配置
加入redis配置:
spring.redis.database=0
spring.redis.host=localhost
spring.redis.password=
spring.redis.pool.max-active=8
spring.redis.pool.max-idle=8
spring.redis.pool.max-wait=-1
spring.redis.pool.min-idle=0
spring.redis.port=6379
spring.session.store-type=redis
@RequestMapping("/")
@ResponseBody
String home(HttpSession session) {
session.setAttribute("test", new Date());
return "Hello World!";
}
- 1