1.redis.properties
redis.host=192.168.153.132
redis.port=6379
redis.pass=123456

redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true
2.spring-mvc. 添加以下配置
<bean id=\"appProperty\"
          class=\"org.spring work.beans.factory.config.PropertyPlaceholderConfigurer\">
    <property name=\"locations\">
         <array>
            ...
            <value>classpath:redis.properties</value>
         </array>
       </property>
   </bean>

<!--redis-->
    <bean id=\"poolConfig\" class=\"redis.clients.jedis.JedisPoolConfig\">
        <property name=\"maxIdle\" value=\"${redis.maxIdle}\"/>
        <property name=\"testOnBorrow\" value=\"${redis.testOnBorrow}\"/>
    </bean>

    <bean id=\"connectionFactory\"
          class=\"org.spring work.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.spring work.data.redis.core.StringRedisTemplate\">
        <property name=\"connectionFactory\" ref=\"connectionFactory\"/>
        <!-- 		如果不配置Serializer,那么存储的时候智能使用String,如果用User类型存储,那么会提示错误User can\'t cast to String!!!
        -->
        <property name=\"keySerializer\">
            <bean
                    class=\"org.spring work.data.redis.serializer.StringRedisSerializer\"/>
        </property>
        <property name=\"valueSerializer\">
            <bean
                    class=\"org.spring work.data.redis.serializer.GenericJackson2JsonRedisSerializer\"/>
        </property>
    </bean>
    <!--End redis-->
收藏 打印