zoukankan      html  css  js  c++  java
  • Spring整合Redis

     切记: 当出现异常时 要销毁对象 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("---------------------");
        }
    
    }
  • 相关阅读:
    背包九讲——动态规划
    Collection、Map、数组 遍历方式
    TCP三次握手与四次挥手
    数据结构——B树、B+树
    数据结构——红黑树
    数据结构——二叉查找树、AVL树
    jquery 抽奖示例
    comebotree树
    初玩Linux部署项目
    springMvc + websocket 实现点对点 聊天通信功能
  • 原文地址:https://www.cnblogs.com/james-roger/p/5864538.html
Copyright © 2011-2022 走看看