Spring+Redis

<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:p="http://www.springframework.org/schema/p"
      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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<context:component-scan base-package="com.springapp.mvc"/>
<context:property-placeholder location="WEB-INF/redis.properties" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>

<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!--最大空闲数-->
       <property name="maxIdle" value="${redis.maxIdle}" />
<!--最大连接数-->
       <property name="maxActive" value="${redis.maxActive}" />
<!--最大建立连接等待时间-->
       <property name="maxWait" value="${redis.maxWait}" />
<!--指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个-->
       <property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>

<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:hostName="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}"  p:poolConfig-ref="poolConfig"/>

<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory"   ref="connectionFactory" />
</bean>
</beans>

properties

=========

class

public class RedisTest {
    @Autowired
    RedisTemplate redisTemplate;
    @Test
    public void test(){
        redisTemplate.opsForValue().set("redis", "hello world");
        System.out.println(redisTemplate.opsForValue().get("redis"));
    }
}

测试结果

Spring+Redis_第1张图片

你可能感兴趣的:(Spring+Redis)