<!-- redis start --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.8.6.RELEASE</version> </dependency> <!-- redis end -->
<!-- 配置文件 --> <context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true" /> <!-- redis單點開始 --> <!-- string redis template definition --> <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> </bean> <!-- redis template definition --> <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" /> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="hashValueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> </property> </bean> <!-- Spring-redis鏈接池管理工廠 --> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <!-- IP地址 --> <property name="hostName" value="${spring.redis.host}" /> <!-- 端口號 --> <property name="port" value="${spring.redis.port}" /> <!-- 超時時間 默認2000--> <property name="timeout" value="${spring.redis.timeout}" /> <!-- 鏈接池配置引用 --> <property name="poolConfig" ref="jedisPoolConfig" /> <!-- Redis數據庫索引(默認爲0) --> <property name="database" value="${spring.redis.database}"/> </bean> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="${spring.redis.pool.max-active}" /> <property name="maxIdle" value="${spring.redis.pool.max-idle}" /> <property name="minIdle" value="${spring.redis.pool.min-idle}" /> <property name="maxWaitMillis" value="${spring.redis.pool.max-wait}" /> <property name="testOnBorrow" value="true" /> </bean>
redis.propertiesredis
##Redis單點ip spring.redis.host=127.0.0.1 ##Redis單點端口 spring.redis.port=6379 ## Redis數據庫索引(默認爲0) spring.redis.database=0 ## 鏈接超時時間(毫秒) spring.redis.timeout=60000 ## 最大重試次數 spring.redis.maxRedirects=3 ## 鏈接池最大鏈接數(使用負值表示沒有限制) spring.redis.pool.max-active=300 ## 鏈接池最大阻塞等待時間(使用負值表示沒有限制) spring.redis.pool.max-wait=-1 ## 鏈接池中的最大空閒鏈接 spring.redis.pool.max-idle=100 ## 鏈接池中的最小空閒鏈接 spring.redis.pool.min-idle=20
@Autowired @Resource(name="stringRedisTemplate") private StringRedisTemplate stringRedisTemplate;