一、基本应用

1 导入依赖

  1. <dependencies>
  2. <!--spring核心依赖-->
  3. <dependency>
  4. <groupId>org.springframework</groupId>
  5. <artifactId>spring-context</artifactId>
  6. <version>5.2.5.RELEASE</version>
  7. </dependency>
  8. <!--spring整合redis核心依赖-->
  9. <dependency>
  10. <groupId>org.springframework.data</groupId>
  11. <artifactId>spring-data-redis</artifactId>
  12. <version>1.0.3.RELEASE</version>
  13. </dependency>
  14. <!--非核心测试依赖-->
  15. <dependency>
  16. <groupId>org.springframework</groupId>
  17. <artifactId>spring-test</artifactId>
  18. <version>5.2.5.RELEASE</version>
  19. </dependency>
  20. <dependency>
  21. <groupId>junit</groupId>
  22. <artifactId>junit</artifactId>
  23. <version>4.12</version>
  24. </dependency>
  25. </dependencies>

2 配置文件

2.1 redis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

     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
">


<!--    <bean id="propertyConfigurer"-->
<!--          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">-->
<!--        <property name="locations">-->
<!--            <list>-->
<!--                <value>classpath:redis.properties</value>-->
<!--            </list>-->
<!--        </property>-->
<!--    </bean>-->

  <!--1.引入小配置文件-->
  <context:property-placeholder location="classpath:redis.properties"/>


  <!-- redis config -->
  <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
      <property name="maxActive" value="${redis.pool.maxActive}" />
      <property name="maxIdle" value="${redis.pool.maxIdle}" />
      <property name="maxWait" value="${redis.pool.maxWait}" />
      <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
  </bean>


  <bean id="jedisConnectionFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
      <property name="hostName" value="${redis.server}"/>
      <property name="port" value="${redis.port}"/>
      <property name="timeout" value="${redis.timeout}" />
      <property name="password" value="${redis.auth}"/>
      <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.StringRedisSerializer">
          </bean>
      </property>
      <property name="ValueSerializer">
          <bean class="org.springframework.data.redis.serializer.StringRedisSerializer">
          </bean>
      </property>
  </bean>
</beans>

2.2 redis.properties

redis.pool.maxActive=100
redis.pool.maxIdle=50 
redis.pool.maxWait=1000
redis.pool.testOnBorrow=true
redis.timeout=50000
redis.server=192.168.81.129
redis.port=6379
redis.auth=muZI5945@

2.3 代码测试

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

import java.io.Serializable;

@ContextConfiguration({ "classpath:redis.xml" })
public class RedisTest extends AbstractJUnit4SpringContextTests {

    @Autowired
    private RedisTemplate<Serializable, Serializable> rt;

    @Test
    public void testConn() {
        rt.opsForValue().set("name","zhangfei");
        System.out.println(rt.opsForValue().get("name"));
    }
}

执行结果: zhangfei