【Redis笔记-7】整合Redis与Spring框架
来源:互联网 发布:网络与新媒体专业方向 编辑:程序博客网 时间:2024/05/15 09:04
0.本文目录
- 本文目录
- 开篇明志
- 搭建Maven SpringMVC框架
- 通过构造方法产生JedisPool实例
- 测试
- 说明
- 本文源码下载
1.开篇明志
本文将介绍如何整合 Redis
和 Spring
框架。
2.搭建Maven + SpringMVC框架
使用Maven对项目依赖的jar包进行管理。只要配置好 pom.xml 文件,Maven就会自动为我们下载该jar包及其所依赖的jar包。
- 关于Maven的配置以及SpringMVC框架的整合这方面的网上资源很多,就不再赘述了。
请参照这篇博客【这里】
3.通过构造方法产生JedisPool实例
applicationContext.xml配置
<!-- 连接池配置 --> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!-- 最大连接数 --> <property name="maxTotal" value="30" /> <!-- 最大空闲连接数 --> <property name="maxIdle" value="10" /> <!-- 每次释放连接的最大数目 --> <property name="numTestsPerEvictionRun" value="1024" /> <!-- 释放连接的扫描间隔(毫秒) --> <property name="timeBetweenEvictionRunsMillis" value="30000" /> <!-- 连接最小空闲时间 --> <property name="minEvictableIdleTimeMillis" value="1800000" /> <!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 --> <property name="softMinEvictableIdleTimeMillis" value="10000" /> <!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 --> <property name="maxWaitMillis" value="1500" /> <!-- 在获取连接的时候检查有效性, 默认false --> <property name="testOnBorrow" value="false" /> <!-- 在空闲时检查有效性, 默认false --> <property name="testWhileIdle" value="true" /> <!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true --> <property name="blockWhenExhausted" value="false" /> </bean> <!-- redis单机 通过连接池 --> <bean id="jedisPool" class="redis.clients.jedis.JedisPool" destroy-method="close"> <constructor-arg name="poolConfig" ref="jedisPoolConfig" /> <constructor-arg name="host" value="127.0.0.1" /> <constructor-arg name="port" value="6379" /> </bean>
4.测试
Java测试代码
@Test public void testJedisPool() { //创建Spring 的 IOC 容器对象 ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml"); JedisPool pool = (JedisPool) ctx.getBean("jedisPool"); Jedis jedis = null; try { jedis = pool.getResource(); jedis.set("name", "testname"); String name = jedis.get("name"); System.out.println(name); } catch (Exception ex) { ex.printStackTrace(); } finally { if (jedis != null) { // 关闭连接 jedis.close(); } } }
说明
本文只展示了如何单机配置Spring + Redis , 会在后面的博客中不断更新, 如果更新会在文末尾加上新内容的博客地址。
本文源码下载
Maven + SpringMVC + Redis单例 【下载点这里】
iMac:springredis anthony$ tree.├── pom.xml├── src│ ├── main│ │ ├── java│ │ │ └── com│ │ │ └── springmvc│ │ │ ├── beans│ │ │ │ └── HelloWorld.java│ │ │ ├── controller│ │ │ │ └── HelloController.java│ │ │ └── redis│ │ │ └── JedisTest.java│ │ ├── resources│ │ │ ├── applicationContext.xml│ │ │ └── redis.properties│ │ └── webapp│ │ ├── WEB-INF│ │ │ └── web.xml│ │ ├── index.jsp│ │ └── view│ │ └── HelloWorld.jsp│ └── test│ ├── java│ │ └── springredis│ │ └── Test.java│ └── resources└── target ├── classes │ ├── applicationContext.xml │ ├── com │ │ └── springmvc │ │ ├── beans │ │ │ └── HelloWorld.class │ │ ├── controller │ │ │ └── HelloController.class │ │ └── redis │ │ └── JedisTest.class │ └── redis.properties ├── m2e-wtp │ └── web-resources │ └── META-INF │ ├── MANIFEST.MF │ └── maven │ └── com.integration │ └── springredis │ ├── pom.properties │ └── pom.xml └── test-classes └── springredis └── Test.class31 directories, 19 files
阅读全文
0 0
- 【Redis笔记-7】整合Redis与Spring框架
- Redis与Spring整合
- redis与spring整合
- Redis与Spring整合
- Redis与Spring整合
- Spring与Redis整合
- Spring与Redis整合
- spring与spring-data-redis整合redis
- redis与spring aop整合
- Spring与Redis的整合
- Redis系列-JAVA与redis整合-spring-redis
- 唯爱小粽子:Spring+Redis框架整合
- spring与redis整合(二)--采用spring-data-redis方式
- [Spring]Redis+Spring整合
- 深入浅出Redis-Spring整合Redis
- 深入浅出Redis-Spring整合Redis
- 深入浅出Redis-Spring整合Redis
- 深入浅出Redis-Spring整合Redis
- 字母旋转方阵
- JS form 表单收集 数据 formSerialize
- Universal-Image-Loader出现oom
- Java知识点
- [PHP学习] 初识PHP
- 【Redis笔记-7】整合Redis与Spring框架
- 尺取法入门2 poj2100 Graveyard Design
- Not Equal on a Segment CodeForces
- ssm:拦截器
- vue双向数据绑定之v-model
- Android NDK(1、简单入门和遇到的问题)
- 大炮打蚊子
- [LeetCode OJ]Partition Equal Subset Sum
- linux无损调整分区大小