springmvc Redis配置使用(Jedis)

首先要在本地安装redis,具体察看(mac安装):http://blog.csdn.net/kirito_j/article/details/79079411

项目环境:maven,springmvc

springmvc Redis配置使用(Jedis)_第1张图片

pom.xml


      org.springframework.data
      spring-data-redis
      1.0.2.RELEASE
    

    
      redis.clients
      jedis
      2.1.0
    
web.xml


    springmvc
    
    org.springframework.web.servlet.DispatcherServlet

    
      contextConfigLocation
      classpath:/META-INF/springmvc-servlet.xml
    
    1
  

  
    springmvc
    /
  

applicaton.properties

# Redis settings
redis.host=127.0.0.1
redis.port=6379
redis.pass=
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true

springmvc-servlet.xml


    

    
        
        
        
        
    

    

    
        
    
使用方法,利用@Autowired注入RedisTemplate
RedisTemplate具体用法察看:https://www.jianshu.com/p/7bf5dc61ca06

TestInterceptor.java

package com.sms.interceptor;

import com.sms.model.CourseEntity;
import com.sms.repository.CourseRepository;
import com.sms.service.redis.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

public class TestInterceptor implements HandlerInterceptor {

    @Autowired
    private RedisTemplate redisTemplate;

    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {

        redisTemplate.opsForValue().set("sessionId","1111");
        System.out.println("sessionId "+ redisTemplate.opsForValue().get("sessionId"));

        return true;
    }

    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("post");
    }

    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        System.out.println("after");
    }
}





你可能感兴趣的:(spring,redis)