首页 > 代码库 > Spring RedisTemplate操作-xml配置(1)

Spring RedisTemplate操作-xml配置(1)

<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="http://www.mamicode.com/${redis.maxIdle}" />
        <property name="maxWaitMillis" value="http://www.mamicode.com/${redis.maxWait}" />
        <property name="maxTotal" value="http://www.mamicode.com/${redis.maxActive}" />
        <property name="testOnBorrow" value="http://www.mamicode.com/${redis.testOnBorrow}" />
    </bean>
    
    <!-- redis服务器中心 -->
    <bean id="redisConnectionFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="poolConfig" ref="poolConfig" />
        <property name="port" value="http://www.mamicode.com/${redis.port}" />
        <property name="hostName" value="http://www.mamicode.com/${redis.host}" />
        <property name="password" value="http://www.mamicode.com/${redis.password}" />
        <property name="timeout" value="http://www.mamicode.com/${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="http://www.mamicode.com/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="http://www.mamicode.com/dddchannel"/>  
                    </bean>  
                </entry>
                <entry key-ref="sub2">  
                    <bean class="org.springframework.data.redis.listener.ChannelTopic">  
                        <constructor-arg value="http://www.mamicode.com/dddchannel"/>  
                    </bean>  
                </entry>
                <entry key-ref="sub3">  
                    <bean class="org.springframework.data.redis.listener.ChannelTopic">  
                        <constructor-arg value="http://www.mamicode.com/cccchannel"/>  
                    </bean>  
                </entry>  
            </map>  
        </property>  
    </bean> 

  

Spring RedisTemplate操作-xml配置(1)