• 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数据库中可以查看到数据。

  • 相关阅读:
    操作系统知识点_用户编程接口
    操作系统知识点_内存管理
    操作系统知识点_进程管理
    LintCode 二叉树的后序遍历
    LintCode 二叉树的最大深度
    LintCode 二叉树的中序遍历
    LintCode 二叉树的前序遍历
    LintCode 删除排序链表中的重复元素
    Lintcode 二分查找
    lintcode 空格替换
  • 原文地址:https://www.cnblogs.com/lichangyun/p/11318348.html
Copyright © 2020-2023  润新知