您的位置:首页 > 数据库 > Redis

Spring中的Redis配置

2018-01-27 22:38 120 查看
pom.xml中:

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.2</version>
</dependency>


applicationContext-redis.xml中:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> 
<!-- 连接池的配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大连接数 -->
<property name="maxTotal" value="${redis.maxTotal}" />
<!-- 最大等待时间 -->
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
<!-- 获取连接时检查有效性 -->
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>

<!-- Jedis连接工厂 -->
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<!-- 连接池的配置 -->
<property name="poolConfig" ref="jedisPoolConfig" />
<!-- 主机地址 -->
<property name="hostName" value="${redis.hostName}" />
<!-- 端口 -->
<property name="port" value="${redis.port}" />
<!-- 是否启用连接池 -->
<property name="usePool" value="${redis.usePool}" />
</bean>

<!-- String类型的RedisTemplate模板 -->
<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<constructor-arg index="0" ref="jedisConnectionFactory" />
</bean>
</beans>

redis.properties中:

redis.maxTotal=200
redis.maxWaitMillis=1000
redis.testOnBorrow=true
redis.hostName= 192.168.206.101
redis.port=6379
redis.usePool=true
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息