Springmvc + Redis 整合

来源:互联网 发布:c 俄罗斯方块源码 编辑:程序博客网 时间:2024/06/07 17:10

配置文件

redis.properties

#访问地址redis.host=123.57.36.122#访问端口redis.port=6379#注意,如果没有password,此处不设置值,但这一项要保留redis.password=sp6161266#最大空闲数,数据库连接的最大空闲时间。超过空闲时间,数据库连接将被标记为不可用,然后被释放。设为0表示无限制。redis.maxIdle=300#连接池的最大数据库连接数。设为0表示无限制redis.maxActive=600#最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。redis.maxWait=1000#在borrow一个jedis实例时,是否提前进行alidate操作;如果为true,则得到的jedis实例均是可用的;redis.testOnBorrow=true

spring-redis.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"       xsi:schemaLocation="      http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">    <!-- redis连接池 -->    <bean id="jedisConfig" class="redis.clients.jedis.JedisPoolConfig">        <property name="maxTotal" value="${redis.maxActive}"></property>        <property name="maxIdle" value="${redis.maxIdle}"></property>        <property name="maxWaitMillis" value="${redis.maxWait}"></property>        <property name="testOnBorrow" value="${redis.testOnBorrow}"></property>    </bean>    <!-- redis连接工厂 -->    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">        <property name="hostName" value="${redis.host}"></property>        <property name="port" value="${redis.port}"></property>        <property name="password" value="${redis.password}"></property>        <property name="poolConfig" ref="jedisConfig"></property>    </bean>    <!-- redis操作模板,这里采用尽量面向对象的模板 -->    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">        <property name="connectionFactory" ref="connectionFactory"/>        <!--     如果不配置Serializer,那么存储的时候只能使用String,如果用对象类型存储,那么会提示错误 can't cast to String!!!-->        <property name="keySerializer">            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>        </property>        <property name="valueSerializer">            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>        </property>        <!--开启事务-->        <property name="enableTransactionSupport" value="true"/>    </bean></beans>

spring.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:context="http://www.springframework.org/schema/context"       xmlns:aop="http://www.springframework.org/schema/aop"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">    <aop:aspectj-autoproxy/>    <!-- 不扫描带有@Controller注解的类。 -->    <context:component-scan base-package="cn.smb.api">        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>    </context:component-scan>    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">        <property name="locations">            <list>                <value>classpath:redis.properties</value>            </list>        </property>    </bean>    <import resource="classpath:spring-redis.xml"/></beans>

DEMO练习

package demo.service;import cn.smb.api.demo.dao.TestEntityMapper;import include.base.BaseService;import org.junit.Assert;import org.junit.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.dao.DataAccessException;import org.springframework.data.redis.connection.RedisConnection;import org.springframework.data.redis.connection.StringRedisConnection;import org.springframework.data.redis.core.*;import org.springframework.data.redis.serializer.RedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;import org.springframework.stereotype.Service;@Servicepublic class TestService {    @Autowired    StringRedisTemplate redisTemplate;    public void selectRedis() {        testOpsForList();        testFirst();        testBound();        testValueOperation();        testopsForList2();        testSize();    }    public void testOpsForList() {        String key = "alex";        ListOperations<String, String> lop = redisTemplate.opsForList();        RedisSerializer<String> serializer = new StringRedisSerializer();        redisTemplate.setKeySerializer(serializer);        redisTemplate.setValueSerializer(serializer);        // rt.setDefaultSerializer(serializer);        lop.leftPush(key, "111111111111");        /*lop.leftPush(key, "bbb");        long size = lop.size(key); // rt.boundListOps(key).size();        Assert.assertEquals(2, size);*/    }    @Test    public void testFirst() {        // String读写        redisTemplate.delete("myStr");        redisTemplate.opsForValue().set("myStr", "vvvvvvvvvvvvvvvv");        System.out.println(redisTemplate.opsForValue().get("myStr"));        // set username        redisTemplate.opsForValue().set("username", "aaaaaaaaaaaaa");        // get username        System.out.println(redisTemplate.opsForValue().get("username"));    }    @Test    public void testBound(){        BoundValueOperations<String, String> boundValueOps = redisTemplate.boundValueOps("username");        System.out.println(boundValueOps.get());        boundValueOps.set("ssssssssssssss");        System.out.println(boundValueOps.get());    }    // 测试便捷对象template    @Test    public void testValueOperation() {        ValueOperations<String, String> vop = redisTemplate.opsForValue();        String key = "pig";        String v = "big pig";        vop.set(key, v);        String value = vop.get(key);        Assert.assertEquals("failure - strings not same", v, value);    }    // 测试RedisTemplate,自主处理key的可读性(String序列号)    @Test    public void testopsForList2() {        String key = "spring";        ListOperations<String, String> lop = redisTemplate.opsForList();        /*RedisSerializer<String> serializer = new StringRedisSerializer();        redisTemplate.setKeySerializer(serializer);        redisTemplate.setValueSerializer(serializer);*/        // rt.setDefaultSerializer(serializer);        lop.leftPush(key, "aaa");        lop.leftPush(key, "bbb");        long size = lop.size(key); // rt.boundListOps(key).size();        Assert.assertEquals(2, size);    }    // 测试Callback    @Test    public void testSize() {        Long dbsize = (Long) redisTemplate                .execute(new RedisCallback<Object>() {                    @Override                    public Long doInRedis(RedisConnection connection)                            throws DataAccessException {                        StringRedisConnection stringRedisConnection=(StringRedisConnection)connection;                        return stringRedisConnection.dbSize();                    }                });        System.out.println("dbsize:" + dbsize);    }}