本篇文章只是简单的介绍一下SpringBoot集成Redis的使用(不包括Redis集群的使用),算是一篇入门文章吧。下面我们进入正题。
前期准备
我们现在pom.xml中引入redis的配置:
- <dependency>
- <groupId>org.springframework.data</groupId>
- <artifactId>spring-data-redis</artifactId>
- </dependency>
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- </dependency>
- redis.serverName = 127.0.0.1:6379
- #超时时间
- redis.timeout = 8
获取配置信息
这里我们定义了一个类用来获取Redis的各项配置信息:
- package com.zkn.learnspringboot.redis;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.stereotype.Component;
- /**
- * Created by zkn on 2016/8/14.
- */
- @ConfigurationProperties(prefix = "redis")
- @Component
- public class RedisArguments {
- /**
- * redis的服务地址
- */
- private String serverName;
- /**
- * 超时时间
- */
- private Integer timeout;
- public String getServerName() {
- return serverName;
- }
- public void setServerName(String serverName) {
- this.serverName = serverName;
- }
- public Integer getTimeout() {
- return timeout;
- }
- public void setTimeout(Integer timeout) {
- this.timeout = timeout;
- }
- }
配置Redis实例
我们定义一个类用来获取Redis的实例:
- package com.zkn.learnspringboot.redis;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Bean;
- import org.springframework.stereotype.Component;
- import redis.clients.jedis.JedisPool;
- import redis.clients.jedis.JedisPoolConfig;
- /**
- * Created by zkn on 2016/8/14.
- */
- @Component
- public class RedisExampleBean {
- @Autowired
- private RedisArguments redisArguments;
- @Bean
- private JedisPool getJedisPoll(){
- JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
- String[] strServer = redisArguments.getServerName().split(":");
- return new JedisPool(jedisPoolConfig,strServer[0],Integer.parseInt(strServer[1]));
- }
- }
Redis的基本工具类
接下来我们写一个类用来获取Reids的实例和释放Redis的连接。
- package com.zkn.learnspringboot.redis;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.JedisPool;
- /**
- * Created by zkn on 2016/8/14.
- */
- @Component
- public class RedisBaseUtil {
- @Autowired
- private JedisPool jedisPool;
- public Jedis getJedis(){
- Jedis jedis = jedisPool.getResource();
- if(jedis == null)
- return null;
- return jedis;
- }
- public void releaseJedis(Jedis jedis){
- if(jedis == null)
- return;
- jedis.close();
- }
- }
- package com.zkn.learnspringboot.redis;
- import org.springframework.stereotype.Component;
- import redis.clients.jedis.Jedis;
- /**
- * Created by zkn on 2016/8/15.
- */
- @Component
- public class RedisStringUtil extends RedisBaseUtil {
- public void putString(String key,String value){
- Jedis jedis = getJedis();
- try{
- if (jedis != null){
- jedis.set(key,value);
- jedis.expire(key,300);
- }
- }catch (Exception e){
- e.printStackTrace();
- }finally {
- releaseJedis(jedis);
- }
- }
- public String getString(String key){
- Jedis jedis = getJedis();
- try{
- if (jedis != null){
- return jedis.get(key);
- }
- }catch (Exception e){
- e.printStackTrace();
- }finally {
- releaseJedis(jedis);
- }
- return null;
- }
- }
访问应用
接下来我们来写一个Controller类来测试一下我们刚才的成果吧。
- package com.zkn.learnspringboot.controller;
- import com.zkn.learnspringboot.redis.RedisStringUtil;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.ResponseBody;
- /**
- * Created by zkn on 2016/8/15.
- */
- @Controller
- @ResponseBody
- public class RedisTestController {
- @Autowired
- private RedisStringUtil redisStringUtil;
- @RequestMapping("/putStringkey.do")
- public String putString(@RequestParam String key){
- redisStringUtil.putString(key,key);
- return "保存成功";
- }
- @RequestMapping("/getStringkey.do")
- public String getString(@RequestParam String key){
- return redisStringUtil.getString(key);
- }