Spring RedisTemplate操作-xml配置(1)


 網上沒能找到全的spring redistemplate操作例子,故特意化了點時間做了接口調用練習,基本包含了所有redistemplate方法。

該操作例子是個系列,該片為spring xml配置,方便后面做各個數據類型的操作。

Redis是一個開源(BSD許可),內存存儲的數據結構服務器,可用作數據庫,高速緩存和消息隊列代理。它支持字符串、哈希表、列表、集合、有序集合,位圖,hyperloglogs等數據類型。內置復制、Lua腳本、LRU收回、事務以及不同級別磁盤持久化功能,同時通過Redis Sentinel提供高可用,通過Redis Cluster提供自動分區。

<context:annotation-config />
    
    <!-- 把非@Controller注解的類轉換為bean -->
    <context:component-scan base-package="tk.tankpao" />
    
    <cache:annotation-driven />
    
    <context:property-placeholder
        location="classpath:conf/properties/redis.properties" />
        
    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <!-- jedis 配置 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="maxWaitMillis" value="${redis.maxWait}" />
        <property name="maxTotal" value="${redis.maxActive}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean>
    
    <!-- redis服務器中心 -->
    <bean id="redisConnectionFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="poolConfig" ref="poolConfig" />
        <property name="port" value="${redis.port}" />
        <property name="hostName" value="${redis.host}" />
        <property name="password" value="${redis.password}" />
        <property name="timeout" value="${redis.timeout}"></property>
    </bean>
    
    <bean id="redisTemplate" name="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="redisConnectionFactory" />
        <property name="keySerializer" ref="stringRedisSerializer" />
        <property name="valueSerializer" ref="stringRedisSerializer" />
        <property name="hashKeySerializer" ref="stringRedisSerializer" />
        <property name="hashValueSerializer" ref="jackson2JsonRedisSerializer" />
        <!-- <property name="enableTransactionSupport" value="true"/> -->
    </bean>
    
    <bean id="jdkredisTemplate" name="jdkredisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="redisConnectionFactory" />
        <property name="keySerializer" ref="jdkSerializationRedisSerializer" />
        <property name="valueSerializer" ref="jdkSerializationRedisSerializer" />
        <property name="hashKeySerializer" ref="stringRedisSerializer" />
        <property name="hashValueSerializer" ref="jackson2JsonRedisSerializer" />
    </bean>
    
    <bean id="jacksonredisTemplate" name="jacksonredisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="redisConnectionFactory" />
        <property name="keySerializer" ref="jackson2JsonRedisSerializer" />
        <property name="valueSerializer" ref="jackson2JsonRedisSerializer" />
        <property name="hashKeySerializer" ref="stringRedisSerializer" />
        <property name="hashValueSerializer" ref="jackson2JsonRedisSerializer" />
    </bean>
    
    <bean id="stringRedisSerializer"
        class="org.springframework.data.redis.serializer.StringRedisSerializer" />
    <bean id="jackson2JsonRedisSerializer"
        class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
    <bean id="jdkSerializationRedisSerializer"
        class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
    
    <!-- 配置緩存 -->
    <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
        <constructor-arg ref="jdkredisTemplate" />
    </bean>
    
    <bean id="topicContainer" class="org.springframework.data.redis.listener.RedisMessageListenerContainer" destroy-method="destroy">  
        <property name="connectionFactory" ref="redisConnectionFactory"/>  
        <property name="messageListeners">  
            <map>  
                <entry key-ref="sub">  
                    <bean class="org.springframework.data.redis.listener.ChannelTopic">  
                        <constructor-arg value="dddchannel"/>  
                    </bean>  
                </entry>
                <entry key-ref="sub2">  
                    <bean class="org.springframework.data.redis.listener.ChannelTopic">  
                        <constructor-arg value="dddchannel"/>  
                    </bean>  
                </entry>
                <entry key-ref="sub3">  
                    <bean class="org.springframework.data.redis.listener.ChannelTopic">  
                        <constructor-arg value="cccchannel"/>  
                    </bean>  
                </entry>  
            </map>  
        </property>  
    </bean> 
  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM