增加spring配置文件: application-jedis.xml?xml version=1.0 encoding=UTF-8?beans xmlns=http://www.springframework.org/schema/beansxmlns:context=http://www.springframework.org/schema/context...
增加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();
}
织梦狗教程
本文标题为:spring 整合 redis 单机版
基础教程推荐
猜你喜欢
- SQL Server如何设置用户只能访问特定数据库和访问特定表或视图 2023-07-29
- Windows10系统中Oracle完全卸载正确步骤 2023-07-24
- Python常见库matplotlib学习笔记之画图中各个模块的含义及修改方法 2023-07-27
- oracle19c卸载教程的超详细教程 2023-07-23
- oracle数据库排序后如何获取第一条数据 2023-07-24
- redis 数据库 2023-09-13
- redis乐观锁与悲观锁的实战 2023-07-13
- Mariadb数据库主从复制同步配置过程实例 2023-07-25
- Java程序员从笨鸟到菜鸟(五十三) 分布式之 Redis 2023-09-11
- Python安装第三方库的方法(pip/conda、easy_install、setup.py) 2023-07-28
