zoukankan      html  css  js  c++  java
  • redis与spring整合

    首先,任何第三方框架与spring进行集成,必须明确的是在spring中注入的是什么,下面以redis为例,需要注入哪些东西。首先,我们要注入的是jedis连接池配置对象:JedisPoolConfig,追踪该对象的源码,如下所示:

    这些变量根据需要设置。接下来注入JedisConnectionFactory,看源码:

    需要注入主机名,端口号,连接池配置信息等。最后需要注入redis模板,看源码:

    需要注入redisConnectionFactory等。通过以上所述,完整的配置文件如下所示:

    <?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:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    
        <!--设置连接池配置对象-->
        <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
            <!--最大连接数-->
            <property name="maxTotal" value="50"></property>
            <!--最大连接时间-->
            <property name="maxWaitMillis" value="1000"></property>
            <!--获取连接检查有效性-->
            <property name="testOnBorrow" value="true"></property>
        </bean>
    
        <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
              p:host-name="localhost" p:port="6379" p:pool-config-ref="jedisPoolConfig"
              p:database="0" />
    
        <!-- spring data 提供redis模版 -->
        <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
            <property name="connectionFactory" ref="jedisConnectionFactory" />
            <!-- 如果不指定Serializer,会默认使用 jdk的序列化器JdkSerializationRedisSerializer
            这里使用字符串序列化的方式即可
            -->
            <property name="keySerializer">
                <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
            </property>
    
            <property name="valueSerializer">
                <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
            </property>
        </bean>
    </beans>

    下面我们测测是否成功,测试代码如下所示:

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    /**
     * Created by Administrator on 2019/8/6.
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:spring/applicationContext-redis.xml")
    public class testspringredis {
    
        @Autowired
        private RedisTemplate redisTemplate;
    
        @Test
        public void testsetredis(){
    
            redisTemplate.opsForValue().set("name_redis","jack");
            System.out.println("name_redis:  "+redisTemplate.opsForValue().get("name_redis"));
        }
    }

    这时在redis数据库中可以查看到数据。

  • 相关阅读:
    SourceTree 跳过登陆
    input type="file"获取文件名方法
    Chrome浏览器安装vue-devtools插件
    设置文本超出既定宽度隐藏,并显示省略号
    node安装express时找不到pakage.json文件;判断安装成功?
    NoSQL:redis缓存数据库
    NoSQL:Linux操作memcached
    Python:迭代器
    Python函数篇:装饰器
    Python面向对象篇之元类,附Django Model核心原理
  • 原文地址:https://www.cnblogs.com/lichangyun/p/11318348.html
Copyright © 2011-2022 走看看