5、redis之使用spring集成commons-pool
添加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的更多相关文章
- 【redis】3.Spring 集成注解 redis 项目配置使用
spring-data-redis 项目,配合 spring 特性并集成 Jedis 的一些命令和方法. 配置redis继承到spring管理项目,使用注解实现redis缓存功能. 参考:http: ...
- 7、redis之使用spring集成commons-pool来操作常见数据类型
环境的搭建参见:http://www.cnblogs.com/yangzhilong/p/4729857.html 下面直接贴具体的测试代码: package com.yzl; import java ...
- spring 集成redis客户端jedis(java)
spring集成jedis简单实例 jedis是redis的java客户端,spring将redis连接池作为一个bean配置. “redis.clients.jedis.JedisPool”,这 ...
- spring 集成 redis -- pub/sub
redis除了常用的当做缓存外,还可以当做简单的消息中间件,实现消息发布订阅 spring集成redis,可以使用spring-data-redis 首先引入相关maven依赖(此处我spring相关 ...
- Spring集成Redis方案(spring-data-redis)(基于Jedis的单机模式)(待实践)
说明:请注意Spring Data Redis的版本以及Spring的版本!最新版本的Spring Data Redis已经去除Jedis的依赖包,需要自行引入,这个是个坑点.并且会与一些低版本的Sp ...
- Redis篇之操作、lettuce客户端、Spring集成以及Spring Boot配置
Redis篇之操作.lettuce客户端.Spring集成以及Spring Boot配置 目录 一.Redis简介 1.1 数据结构的操作 1.2 重要概念分析 二.Redis客户端 2.1 简介 2 ...
- Spring集成Redis集群(含spring集成redis代码)
代码地址如下:http://www.demodashi.com/demo/11458.html 一.准备工作 安装 Redis 集群 安装参考: http://blog.csdn.net/zk6738 ...
- Redis 与 Spring 集成
配置applicationContext.xml <!-- 连接池配置 --> <bean id="jedisPoolConfig" class="re ...
- 【redis】4.spring boot集成redis,实现数据缓存
参考地址:https://spring.io/guides/gs/messaging-redis/ ================================================== ...
随机推荐
- Asp.Net 获取物理路径
一.AppDomain 1.AppDomin获取当前前程序域目录 2.不需要请求上线文实例,例如在Global.ascx中访问等 //网站物理目录 AppDomain.CurrentDomain.Ba ...
- [转]mysql 存储过程中使用多游标
From : http://www.netingcn.com/mysql-procedure-muti-cursor.html mysql的存储过程可以很方便使用游标来实现一些功能,存储过程的写法大致 ...
- Arcgis ArcMap 10 如何生成msd地图文档定义【 arcgis mxd怎么转换成msd】
.mxd是arcgis 的地图文档后缀名. .msd是arcgis 的地图服务定义文件,是 map service definition 的缩写. 基于 MSD 的服务支持 Maplex.制图表达和新 ...
- go语言之进阶篇接口的定义和实现以及接口的继承
1.接口的定义和实现以及接口的继承 示例: package main import "fmt" //定义接口类型 type Humaner interface { //方法,只有声 ...
- 添加 Github follow、star按钮到网页
怎么把github的star/fork/watch三个按钮弄到自己网站上? 就是这个按钮如何弄到我的网站里面来,是否有API呢?mdo/github-buttons · GitHub这个超级方便已经添 ...
- html与css架构的一点体验
css本身,可以说是一门非常简单而容易入门的语言.制作一个页面,或者制作一个小企业站,对于css的要求都是非常低的.只要熟悉语法,通过英文单词的含义猜,都基本可以拼出一套样式.更何况市面上还有各种各样 ...
- javaScript:让文本框内的最后一个文字的后面获得焦点
//当失去交点以后 让文本框内的文字获得焦点 并且光标移到最后一个字后面 function myfocus(myid) { if(isNav){ document.getElementById(myi ...
- Linq-批量删除方法
linq中批量删除用DeleteAllOnSubmit,里面的参数是数据集 传入某要删除的ID列表,使用对象的Contains方法与数据库中值比较,相同就删除. //批量删除 public void ...
- 如何在Centos7上安装和使用ZFS
导读 ZFS文件系统的英文名称为ZettabyteFileSystem,也叫动态文件系统(DynamicFileSystem),是第一个128位文件系统.最初是由Sun公司为Solaris10操作系统 ...
- linux bash关闭标准输出1(exec 1<&-)后重新打开
linux bash shell的再次学习. 文件描述符: stdin,stdout 和 stderr 的文件描述符分别是 0,1 和 2(一个文件描述符说白了就是文件系统为了跟踪这个打开的文件而分配 ...