spring 整合 redis 单机版
2024-10-21 15:47:06
增加spring配置文件: application-jedis.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- jedis客户端单机版 id是我们随便起的名字,后面全限定名要复制对,
然后还有个属性 poolConfig可以配也开不配置,不配置时会有默认配置-->
<bean id="redisClient" class="redis.clients.jedis.JedisPool">
<constructor-arg name="host" value="192.168.29.102"></constructor-arg>
<constructor-arg name="port" value="6379"></constructor-arg>
</bean> </beans>
测试代码:
//单独测试
@Test
public void testJedisPool() {
//创建连接池对象
JedisPool jedisPool = new JedisPool("192.168.29.102", 6379);
//从连接池中获取一个jedis对象
Jedis jedis = jedisPool.getResource();
jedis.set("key2", "jedisPool2");
String string = jedis.get("key2");
System.out.println(string);
//关闭jedis对象
jedis.close();
//关闭连接池
jedisPool.close();
} //整合spring的测试
@Test
public void testSpringJedisSingle(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-jedis.xml");
JedisPool pool = (JedisPool) applicationContext.getBean("redisClient");
Jedis jedis = pool.getResource();
jedis.set("key1", "key1value");
String string = jedis.get("key1");
System.out.println("------: "+string);
jedis.close();
pool.close();
}
最新文章
- 计算机程序的思维逻辑 (31) - 剖析Arrays
- eap
- 如何安装mysql服务
- 共享内存 最快IPC 的原因
- Ubuntu用户相关基本命令
- 【英语】Bingo口语笔记(28) - 表示“秘密”
- java操作spark1.2.0
- Poj 2586 / OpenJudge 2586 Y2K Accounting Bug
- ASP.NET MVC 第四回 向View传值
- ubuntu 解压,压缩
- QQ互联 回调地址
- BAT线下战争:巨额投资或培养出自己最大对手(包括美团、58、饿了么在内的公司都在计划推出自己的支付工具和金融产品,腾讯只做2不做O)
- 6_StopWatch
- Hive Server 2 安装部署测试
- UVA-514 Rails (栈)
- web框架之Spring-MVC环境搭建(转)
- 跟版网 > 织梦教程 > 织梦安装使用 > 织梦DedeCMS附件上传大
- Windbg分析蓝屏Dump文件
- P1516 青蛙的约会
- World is Exploding (容斥 + 统计)
热门文章
- OpenMPI运行问题:enough slots available in the system
- CVPR2018 关于视频目标跟踪(Object Tracking)的论文简要分析与总结
- Hyperledger fablic 1.0 在centos7环境下的安装与部署和动态增加节点
- vue.js学习之 跨域请求代理与axios传参
- 在linux下PHP和Mysql环境搞事情
- OJ错误命令解释
- Internet History
- python学习摘要(3)--字符串处理函数
- TCP系列26—重传—16、重组包
- Uncaught ReferenceError: wx is not defined