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("---------------------");
        }
    
    }
  • 相关阅读:
    【题解】【神奇校内POIN模拟赛】小凯的疑惑
    【题解】【CF670C】cinema
    11 Remove Duplicates from Sorted Array II
    10 3SumClosest
    7 Container With Most Water
    8 String to Integer (atoi)
    9 3Sum
    6 Palindrome Number
    5 Zigzag Conversion
    4 Longest Palindromic Substring
  • 原文地址:https://www.cnblogs.com/james-roger/p/5864538.html
Copyright © 2011-2022 走看看