添加spring的依赖

<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.yzl</groupId>
<artifactId>redis.first</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.1</version>
</dependency> <dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.0.RELEASE</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
</dependencies>
</project>

增加spring配置文件

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ss="http://www.springframework.org/schema/security"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:property-placeholder location="classpath:redis-pool.properties"/> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"></bean>
<bean class="redis.clients.jedis.JedisPool">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1" value="${redis.ip}" />
<constructor-arg index="2" value="${redis.port}" />
</bean>
</beans>

编写测试类:

 package com.yzl;

 import org.junit.After;
import org.junit.Before;
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; /**
* RedisApp之spring的测试类
*
* @author yangzhilong
* @see [相关类/方法](可选)
* @since [产品/模块版本] (可选)
*/
public class RedisApp2Test {
private JedisPool pool;
private ApplicationContext app; @Before
public void before(){
app = new ClassPathXmlApplicationContext("spring-config.xml");
pool = app.getBean(JedisPool.class);
} @Test
public void test(){
Jedis jedis = pool.getResource();
jedis.set("name", "hello"); String value = jedis.get("name");
System.out.println("get value :" + value); pool.returnResourceObject(jedis);
} @After
public void after(){
System.out.println("end~~~");
}
}

测试结果:

get value :hello
end~~~

下一篇:6、redis之使用spring-data-redis的Template

5、redis之使用spring集成commons-pool的更多相关文章

  1. 【redis】3.Spring 集成注解 redis 项目配置使用

    spring-data-redis  项目,配合 spring 特性并集成 Jedis 的一些命令和方法. 配置redis继承到spring管理项目,使用注解实现redis缓存功能. 参考:http: ...

  2. 7、redis之使用spring集成commons-pool来操作常见数据类型

    环境的搭建参见:http://www.cnblogs.com/yangzhilong/p/4729857.html 下面直接贴具体的测试代码: package com.yzl; import java ...

  3. spring 集成redis客户端jedis(java)

    spring集成jedis简单实例   jedis是redis的java客户端,spring将redis连接池作为一个bean配置. “redis.clients.jedis.JedisPool”,这 ...

  4. spring 集成 redis -- pub/sub

    redis除了常用的当做缓存外,还可以当做简单的消息中间件,实现消息发布订阅 spring集成redis,可以使用spring-data-redis 首先引入相关maven依赖(此处我spring相关 ...

  5. Spring集成Redis方案(spring-data-redis)(基于Jedis的单机模式)(待实践)

    说明:请注意Spring Data Redis的版本以及Spring的版本!最新版本的Spring Data Redis已经去除Jedis的依赖包,需要自行引入,这个是个坑点.并且会与一些低版本的Sp ...

  6. Redis篇之操作、lettuce客户端、Spring集成以及Spring Boot配置

    Redis篇之操作.lettuce客户端.Spring集成以及Spring Boot配置 目录 一.Redis简介 1.1 数据结构的操作 1.2 重要概念分析 二.Redis客户端 2.1 简介 2 ...

  7. Spring集成Redis集群(含spring集成redis代码)

    代码地址如下:http://www.demodashi.com/demo/11458.html 一.准备工作 安装 Redis 集群 安装参考: http://blog.csdn.net/zk6738 ...

  8. Redis 与 Spring 集成

    配置applicationContext.xml <!-- 连接池配置 --> <bean id="jedisPoolConfig" class="re ...

  9. 【redis】4.spring boot集成redis,实现数据缓存

    参考地址:https://spring.io/guides/gs/messaging-redis/ ================================================== ...

随机推荐

  1. COPY ORCHARD GET 404: System.UnauthorizedAccessException: mappings.bin的访问被拒绝

    COPY ORCHARD 得到 404 错误,结果翻看Logs,得到的错误是: 014-07-31 17:36:46,217 [16] Orchard.Environment.DefaultOrcha ...

  2. Statistical Artifact (error)

    In natural science and signal processing, an artifact is any error in the perception or representati ...

  3. GraphX中Pregel单源点最短路径(转)

    原文链接:GraphX中Pregel单源点最短路径 GraphX中的单源点最短路径例子,使用的是类Pregel的方式. 核心部分是三个函数: 1.节点处理消息的函数  vprog: (VertexId ...

  4. JavaScript:String 对象

    ylbtech-JavaScript:String 对象 1.返回顶部 String 对象 String 对象用于处理文本(字符串). 创建 String 对象的语法: new String(s); ...

  5. Oracle比较2个表内容

    Comparing the Contents of Two Tables A表和B表.拥有一致列,C2一列内容不同. I have two tables named A and B. They hav ...

  6. Android JNI中C和JAVA代码之间的互相调用

    关于Android studio中使用NDK/JNI环境和入门:http://blog.csdn.net/quan648997767/article/details/64923143 1. C代码回调 ...

  7. Spark的动态资源分配

    跑spark程序的时候,公司服务器需要排队等资源,参考一些设置,之前不知道,跑的很慢,懂得设置之后简直直接起飞. 简单粗暴上设置代码: def conf(self): conf = super(Tbt ...

  8. thinkcmf 角色授权支持分类

    ThinkCMF中的权限是以后台菜单为基础来进行设置的(menu table),即如果你需要一个自定义的权限,那么你需要在后台菜单里添加一项菜单,然后在角色管理里可以针对角色进行授权   而现在遇到一 ...

  9. Git 以分支的方式同时管理多个项目

    你是否遇到过这样的问题: 你的客户在你们这边做了N个项目,而项目之间又存在着某些业务关联(数据库访问等) 之前你可能是这样处理的,为客户的每个项目创建单独的Git版本 PC项目 手机项目 微信项目 其 ...

  10. Simple XOR Encryption/Decryption in C++ (And Several Other Languages)

    For details on how to implement XOR encryption using Go, see this post. If you are looking for XOR e ...