码迷,mamicode.com
首页 > 编程语言 > 详细

Spring整合Redis

时间:2016-09-12 14:18:32      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

 切记: 当出现异常时 要销毁对象 returnBrokenResource,  使用完之后要 还会连接returnResource

applicationContext.xml

<?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:jee="http://www.springframework.org/schema/jee" 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/context http://www.springframework.org/schema/context/spring-context.xsd">  
  
   <!--  <context:property-placeholder location="classpath:redis.properties" />   -->
  
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">  
        <property name="maxIdle" value="300" />  
        <property name="maxActive" value="1000" />  
        <property name="maxWait" value="10000" />  
        <property name="testOnBorrow" value="true" />  
    </bean>  
      
    <!-- <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"  
        p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}"  p:pool-config-ref="poolConfig"/>  
      
    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">  
        <property name="connectionFactory"   ref="connectionFactory" />  
    </bean>         
      
    <bean id="userDao" class="com.x.dao.impl.UserDao" />    -->
    <bean id="jedisShardInfo" class="redis.clients.jedis.JedisShardInfo">
        <constructor-arg index="0" value="localhost" />
        <constructor-arg index="1" value="6379" type="int"/>
        <!-- <property name="password" value="123456"/> -->
    </bean>
    <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
        <constructor-arg index="0" ref="jedisPoolConfig" />
        <constructor-arg index="1">
            <list>
                <ref bean="jedisShardInfo" />
            </list>
        </constructor-arg>
    </bean>
</beans>  

redis.properties:

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

Test:

package com.redis.redis;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

public class Test {
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        ShardedJedisPool shardedJedisPool = (ShardedJedisPool) ctx.getBean("shardedJedisPool");
        ShardedJedis jedis = shardedJedisPool.getResource();
        jedis.set("ke", "luojie");
        System.out.println("---------------------");
    }

}

 

Spring整合Redis

标签:

原文地址:http://www.cnblogs.com/james-roger/p/5864538.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!