Spring配置redis

来源:互联网 发布:电脑手绘软件 编辑:程序博客网 时间:2024/06/13 10:49

1.引入maven依赖

  <dependency>       <groupId>org.springframework.data</groupId>         <artifactId>spring-data-redis</artifactId>         <version>1.0.2.RELEASE</version>     </dependency>     <dependency>        <groupId>redis.clients</groupId>        <artifactId>jedis</artifactId>        <version>2.1.0</version>    </dependency>  

2.xml配置

<beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="      http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     http://www.springframework.org/schema/tx       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd      http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context-3.0.xsd          ">    <!-- scanner redis properties -->    <context:property-placeholder location="/config/redis.properties" />    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">        <property name="maxIdle" value="${redis.maxIdle}" />        <property name="maxActive" value="${redis.maxActive}" />        <property name="maxWait" value="${redis.maxWait}" />        <property name="testOnBorrow" value="${redis.testOnBorrow}" />    </bean>    <bean id="connectionFactory"        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"        p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}"        p:pool-config-ref="poolConfig" />    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">        <property name="connectionFactory" ref="connectionFactory" />    </bean>    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">        <property name="connectionFactory" ref="connectionFactory" />    </bean></beans> 

3.properties配置

redis.host=127.0.0.1redis.port=6379redis.pass=redis.maxIdle=300redis.maxActive=600redis.maxWait=1000redis.testOnBorrow=true

4.spring引入在applicationContext.xml中添加

<import resource="redis-context.xml"/>

5.使用

package service.redis;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.stereotype.Service;/** * Redis公用类 * @author jinx * */@Servicepublic class RedisService{    @Autowired    protected StringRedisTemplate stringRedisTemplate;    @Autowired    protected RedisTemplate<String,Object> redisTemplate;    public String setObj(String key,Object obj){        redisTemplate.opsForValue().set(key,obj);        return key;    }    public String setString(String key,String str){        stringRedisTemplate.opsForValue().set(key,str);        return key;    }    public void clearObj(String key){        redisTemplate.opsForValue().set(key,null);    }    public String get(String key){        return stringRedisTemplate.opsForValue().get(key);    }    /**     * 取出对象     * @param key     * @return     */    public Object getObj(String key){        return redisTemplate.opsForValue().get(key);    }}
0 0
原创粉丝点击