Redis和spring整合

2021-07-08 05:05

阅读:824

标签:rip   java   red   style   row   pid   artifact   cto   配置   

 第一种方式:

依赖:

dependency>
    groupId>redis.clientsgroupId>
    artifactId>jedisartifactId>
    version>2.6.2version>
dependency>

dependency>
     groupId>org.apache.commonsgroupId>
     artifactId>commons-pool2artifactId>
     version>2.2version>
dependency>

配置文件

xml version="1.0" encoding="UTF-8"?>
beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"   
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
    http://www.springframework.org/schema/beans/spring-beans.xsd    
    http://www.springframework.org/schema/aop    
    http://www.springframework.org/schema/aop/spring-aop.xsd    
    http://www.springframework.org/schema/tx    
    http://www.springframework.org/schema/tx/spring-tx.xsd    
    http://www.springframework.org/schema/context    
    http://www.springframework.org/schema/context/spring-context.xsd">
    
    description>Redis与spring 整合description>

    
    bean id="config" class="redis.clients.jedis.JedisPoolConfig">
         property name="maxTotal" value="50">property>
         property name="maxIdle" value="10">property>
    bean>
    
    
    bean id="jedisPool" class="redis.clients.jedis.JedisPool">
        constructor-arg index="0" ref="config">constructor-arg>
        constructor-arg index="1" value="192.168.25.151" type="java.lang.String">constructor-arg>
        constructor-arg index="2" value="6379">constructor-arg>
    bean>
    
beans>

简单的测试代码

package com.ilcbs.cache.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/applicationContext-redis.xml")
public class JedisTest {
    
    @Autowired
    private JedisPool jedisPool;
    
    //设置
    @Test
    public void testSet(){
        Jedis jedis = jedisPool.getResource();
        jedis.set("张三", "23");
    } 
    
    @Test
    public void testGet(){
        Jedis jedis = jedisPool.getResource();
        String value = jedis.get("张三");
        System.out.println(value);
    }
}

第二种方式:

依赖:


dependency>
     groupId>redis.clientsgroupId>
     artifactId>jedisartifactId>
     version>2.6.2version>
dependency>

dependency>
   groupId>org.apache.commonsgroupId>
    artifactId>commons-pool2artifactId>
    version>2.2version>
dependency>

dependency>
     groupId>org.springframework.datagroupId>
     artifactId>spring-data-redisartifactId>
     version>${springdataredis.version}version>
dependency>

配置文件:

xml version="1.0" encoding="UTF-8"?>
beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"   
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
    http://www.springframework.org/schema/beans/spring-beans.xsd    
    http://www.springframework.org/schema/aop    
    http://www.springframework.org/schema/aop/spring-aop.xsd    
    http://www.springframework.org/schema/tx    
    http://www.springframework.org/schema/tx/spring-tx.xsd    
    http://www.springframework.org/schema/context    
    http://www.springframework.org/schema/context/spring-context.xsd">
    
    description>Redis与spring 整合description>

    bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
        property name="maxIdle" value="300" />        
        property name="maxWaitMillis" value="3000" />  
        property name="testOnBorrow" value="true" />  
    bean>  
    
    
    bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
            property name="hostName" value="192.168.25.151">property>
            property name="port" value="6379">property>
            property name="poolConfig" ref="poolConfig">property>
            property name="database" value="0">property>
     bean>
        
    bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">  
        property name="connectionFactory" ref="redisConnectionFactory" /> 
        property name="keySerializer">
            bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        property>
        property name="valueSerializer">
            bean class="org.springframework.data.redis.serializer.StringRedisSerializer"> 
            bean>
        property> 
    bean>  
beans>

简单测试代码

package com.ilcbs.cache.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/applicationContext-cache.xml")
public class SpringJedisTest {
    
    @Autowired
    private RedisTemplate redisTemplate;
    
    //存储
    @Test
    public void testSet(){
        redisTemplate.opsForValue().set("spring", "193");
    }
    
    //获取
    @Test
    public void testGet(){
        String value = (String) redisTemplate.opsForValue().get("spring");
        System.out.println(value);
    }
}

 

Redis和spring整合

标签:rip   java   red   style   row   pid   artifact   cto   配置   

原文地址:https://www.cnblogs.com/guo-rong/p/9583366.html


评论


亲,登录后才可以留言!