Spring Boot 1.4.2.RELEASE Redis Session配置

来源:互联网 发布:淘宝怎么改收获地址 编辑:程序博客网 时间:2024/06/08 15:39

在配置session到redis时候,网上的几个方法试过不成功,捣鼓折腾几回终于可以了。

pom中添加依赖:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.2</version>
</dependency>

在application.properties中定义:

spring.redis.host=localhost
spring.redis.port=6379

添加Session配置类:

package com.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;


@Configuration  
@EnableRedisHttpSession
public class SessionConfig {  
@Autowired
private Environment env;
@Bean
public RedisConnectionFactory connectionFactory() {

JedisConnectionFactory jcf = new JedisConnectionFactory();
   jcf.setHostName(env.getProperty("spring.redis.host"));
  // jcf.setPassword(env.getProperty("redis.password"));
   jcf.setPort(env.getProperty("spring.redis.port", Integer.class));


   return jcf;
}

0 0
原创粉丝点击