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("---------------------");
        }
    
    }
  • 相关阅读:
    Python基础语法 第2节课(数据类型转换、运算符、字符串)
    python基础语法 第5节课 ( if 、 for )
    python基础语法 第4节课 (字典 元组 集合)
    Python基础语法 第3节课 (列表)
    A. Peter and Snow Blower 解析(思維、幾何)
    C. Dima and Salad 解析(思維、DP)
    D. Serval and Rooted Tree (樹狀DP)
    C2. Balanced Removals (Harder) (幾何、思維)
    B. Two Fairs 解析(思維、DFS、組合)
    D. Bash and a Tough Math Puzzle 解析(線段樹、數論)
  • 原文地址:https://www.cnblogs.com/james-roger/p/5864538.html
Copyright © 2011-2022 走看看