Redis(2)—— 基于Spring的Redis Template 工具类

1、spring相关配置如下

<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        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">

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="100" />
        <property name="maxIdle" value="10" />
    </bean>

    <bean id="jedisConnectionFactory"
          class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
        <property name="hostName" value="${redis_hostname}"/>
        <property name="port" value="${redis_port}"/>
        <property name="password" value="${redis_pwd}" />
        <property name="database" value="0" />
        <property name="timeout" value="3000"/>
        <property name="usePool" value="true"/>
        <property name="poolConfig" ref="jedisPoolConfig"/>
    </bean>


    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory"   ref="jedisConnectionFactory" />
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer">
                <constructor-arg type="java.lang.Class" value="java.lang.Object"></constructor-arg>
            </bean>
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer">
                <constructor-arg type="java.lang.Class" value="java.lang.Object"></constructor-arg>
            </bean>
        </property>
    </bean>

</beans>

2、使用:

    @Autowired
    private RedisTemplate redisTemplate;

3、pom

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.7.10.RELEASE</version>
        </dependency>

 

posted @ 2018-04-02 17:27  xu_shuyi  阅读(178)  评论(0)    收藏  举报