栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring整合redis,并使用redisTemplate

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring整合redis,并使用redisTemplate

          1、导入maven依赖(     注意:一定要保持dependency版本一致!)




  4.0.0

  com.wa
  001test
  1.0-SNAPSHOT
  war

  
    UTF-8
    11
    11
    4.3.17.RELEASE
  

  
    
      junit
      junit
      4.11
      test
    
    
      org.springframework
      spring-webmvc
      ${spring.version}
    
    
      org.springframework
      spring-context
      ${spring.version}
    
    
      org.springframework
      spring-beans
      ${spring.version}
    
    
      org.springframework
      spring-aop
      ${spring.version}
    
    
      org.springframework
      spring-context-support
      ${spring.version}
    

    
      org.springframework.data
      spring-data-commons
      2.2.4.RELEASE
    

    
      org.springframework.data
      spring-data-redis
      1.7.2.RELEASE
    
    
      redis.clients
      jedis
      2.9.0
    
    
      org.apache.commons
      commons-lang3
      3.7
    

    
      org.springframework
      spring-test
      4.2.5.RELEASE
      test
    


  

  
    
      
        src/main/java
        
          ***.xml
        
        
        false
      
    

    
      
        org.apache.maven.plugins
        maven-compiler-plugin
        3.6.1
        
          11
          11
        
      
    
  


        2、redis.properties文件:
redis.maxTotal=1000
redis.maxIdle=5
redis.maxWaitMillis=2000
redis.testonBorrow=true
redis.host=192.168.249.131
redis.port=6379
redis.timeout=2000
redis.password=
redis.timeBetweenEvictionRunsMillis=30000  
redis.numTestsPerEvictionRun=50 
        3、spring配置文件:
 


        
            
            
            
                
                    classpath:redis.properties
                
            
        

        
        
            
            
            
            
            
            
            
            
        

        
        
            
            
            
            
            
            
            
            
            
            
            
        

        
        
            
            
                
            
            
                
            
            
                
            
            
                
            
            
            
        
        


        4、测试类:
package com.wang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;


@Component
public class Redis {
    @Autowired
    RedisTemplate redisTemplate;
    public void getKey(){
        System.out.println(redisTemplate.opsForValue().get("lock"));
    }
}
        5、junit测试
import com.wang.Hello;
import com.wang.Redis;
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;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class TestHello {
    @Autowired Redis redis;
    @Test
    public void redisTest(){
        redis.getKey();
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/328817.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号