Spring之整合Redis
2016-04-01 16:47
661 查看
如今,很多项目用到Spring以及Redis,那么这两个是如何整合在一起的呢?
首先创建一个Maven工程,然后构建pom:
至此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整合完毕。
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- redis安装问题小结
- maven学习
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序