redis与spring整合·

配置spring配置文件applicationContext.xml

<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

<!-- 连接池配置 -->
    <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="true" />
        <!-- 在空闲时检查有效性, 默认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="192.168.25.145"/>
        <constructor-arg name="port" value="6379"/>
    </bean>

测试:

 1 private ApplicationContext applicationContext;
 2
 3     @Before
 4     public void init() {
 5         applicationContext = new ClassPathXmlApplicationContext(
 6                 "classpath:applicationContext.xml");
 7     }
 8
 9     @Test
10     public void testJedisPool() {
11     JedisPool pool = (JedisPool) applicationContext.getBean("jedisPool");
12             try  {
13             jedis = pool.getResource();
14
15             jedis.set("name", "lisi");
16             String name = jedis.get("name");
17             System.out.println(name);
18         }catch(Exception ex){
19             ex.printStackTrace();
20         }finally{
21             if(jedis != null){
22                 //关闭连接
23                 jedis.close();
24             }
25         }
26     }

原文地址:https://www.cnblogs.com/the-wang/p/8570010.html

时间: 2024-08-28 07:08:59

redis与spring整合·的相关文章

redis与spring整合实例

1)首先是redis的配置. 使用的是maven工程,引入redis与spring整合的相关jar包 <!-- redis服务 start--> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.6.2.RELEASE</version> &

Redis和spring整合

第一种方式: 依赖: <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.6.2</version> </dependency> <!-- 连接池 --> <dependency> <groupId>org.apache.commons</grou

redis mybatis spring整合

最近想在框架里面加入redis,替换原因呢其实也没有,就是单纯的想替换掉 ---维基百科:redis介绍 一般开发中用户状态使用session或者cookie,两种方式各种利弊. Session:在InProc模式下容易丢失,并且引起并发问题.如果使用SQLServer或者SQLServer模式又消耗了性能 Cookie则容易将一些用户信息暴露,加解密同样也消耗了性能. Redis采用这样的方案解决了几个问题, 1.Redis存取速度快. 2.用户数据不容易丢失. 3.用户多的情况下容易支持集群

Redis + Jedis + Spring整合遇到的异常(转)

项目中需要用到缓存,经过比较后,选择了redis,客户端使用jedis连接,也使用到了spring提供的spring-data-redis.配置正确后启动tomcat,发现如下异常: Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisTemplate' defined in class path resource [spring.xml]:

Java中Jedis操作Redis与Spring的整合

Redis是一个key-value存储系统.它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合)和zset(有序集合).这些数据类型都支持push/pop.add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的.在此基础上,redis支持各种不同方式的排序.为了保证效率,数据都是缓存在内存中.redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步.以下是

网站性能优化小结和spring整合redis

现在越来越多的地方需要非关系型数据库了,最近网站优化,当然从页面到服务器做了相应的优化后,通过在线网站测试工具与之前没优化对比,发现有显著提升. 服务器优化目前主要优化tomcat,在tomcat目录下的server.xml文件配置如下内容: <Connector port="1818" protocol="HTTP/1.1" maxHttpHeaderSize="8192" maxThreads="1000" minS

Spring整合Redis(spring-data-redis)

历经几天看了大量的博客资料,差不多算是搞定了,目前只是针对单个数据源,集群暂时没研究 maven依赖 <properties> <!-- redis 版本 --> <redis.version>2.9.0</redis.version> <spring.redis.version>1.8.4.RELEASE</spring.redis.version> <mybatis.version>3.4.4</mybatis.

Redis 缓存 + Spring 的集成示例(转)

<整合 spring 4(包括mvc.context.orm) + mybatis 3 示例>一文简要介绍了最新版本的 Spring MVC.IOC.MyBatis ORM 三者的整合以及声明式事务处理.现在我们需要把缓存也整合进来,缓存我们选用的是 Redis,本文将在该文示例基础上介绍 Redis 缓存 + Spring 的集成.关于 Redis 服务器的搭建请参考博客<Redhat5.8 环境下编译安装 Redis 并将其注册为系统服务>. 1. 依赖包安装 pom.xml

spring整合mybatis步骤分析

1.spring配置datasource bean的时候,不同的数据库连接方式有有不同的datasource实现类. 比如采用c3p0数据库连接池,要用c3p0的datasource实现类 com.mchange.v2.c3p0.ComboPooledDataSource <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name=&