Spring之整合Redis

2 篇文章 0 订阅
1 篇文章 0 订阅

如今,很多项目用到Spring以及Redis,那么这两个是如何整合在一起的呢?

首先创建一个Maven工程,然后构建pom:

<span style="font-size:12px;"><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sy</groupId>
	<artifactId>redis</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>spring-redis</name>

	<dependencies>
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.8.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-redis</artifactId>
			<version>1.6.4.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.2.5.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>
	</dependencies>
</project></span>
接下来,创建redis.properties:

#Redis settings
redis.host=127.0.0.1
redis.port=6379
redis.pass=1234
redis.timeout=3000
  
#最大能够保持idel状态的对象数
redis.maxIdle=300
#最大分配的对象数
redis.maxTotal=60000
#当调用borrow Object方法时,是否进行有效性检查
redis.testOnBorrow=true
然后创建Spring配置文件applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>

<!--
  - Application context definition for JPetStore's business layer.
  - Contains bean references to the transaction manager and to the DAOs in
  - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
  -->
<beans xmlns="http://www.springframework.org/schema/beans"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:aop="http://www.springframework.org/schema/aop"
		xmlns:tx="http://www.springframework.org/schema/tx"
		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-2.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<context:property-placeholder location="classpath:redis.properties"/>
	
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> 
		<!-- 控制一个pool最多有多少个状态为idle(空闲)的jedis实例 -->   
        <property name="maxIdle" value="${redis.maxIdle}" />  
         <!-- 控制一个pool可分配多少个jedis实例 --> 
        <property name="maxTotal" value="${redis.maxTotal}" />  
        <!-- 当调用borrow Object方法时,是否进行有效性检查 -->
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />  
    </bean> 
    
	<!-- redis的连接池pool,不是必选项:timeout/password  -->
    <bean id = "jedisPool" class="redis.clients.jedis.JedisPool">
      <constructor-arg index="0" ref="jedisPoolConfig"/>
      <constructor-arg index="1" value="${redis.host}"/>
      <constructor-arg index="2" value="${redis.port}" type="int"/>
      <constructor-arg index="3" value="${redis.timeout}" type="int"/>
      <constructor-arg index="4" value="${redis.pass}"/>
    </bean>

</beans>
最后编写测试类:

package redis;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class TestJedis {

	@Test
	public void testJedis(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		JedisPool jedisPool = (JedisPool) ac.getBean("jedisPool");
		Jedis jedis = jedisPool.getResource();
		jedis.set("hello", "world");
		String hello = jedis.get("hello");
		System.out.println(hello);
	}
}
输出结果:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
world

至此Spring与Redis整合完毕。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值