Spring通过xml集成Redis数据库

本文介绍了如何通过XML配置将Spring与Redis数据库集成,以提升大数据量下前端页面的加载速度。首先,需要引入相关依赖,如jedis和spring-data-redis。接着,在配置文件中设置Redis服务器的地址、端口等参数。然后,在Spring容器中注入配置文件,并配置Jedis连接池和RedisTemplate。最后,通过测试方法展示了如何使用RedisTemplate存取数据。

Spring通过xml集成Redis数据库

前台list的属性由于数据量较大,而且在后台是递归获取的,所以前端展示的页面就比较慢,所以想集成一下redis,加快数据的查询。

Spring集成Redis的主流方式大致有以下两种(以单机为例)

  1. 基于xml集成Redis
  2. 基于注解实现Redis

今天我们讲讲利用xml集成Redis数据库

  1. 导入相关依赖
<dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.4.2</version>
    </dependency>
    <!-- 2、spring整合Redis的jar包 -->
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>1.7.1.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>commons-pool</groupId>
      <artifactId>commons-pool</artifactId>
      <version>1.6</version>
    </dependency>
  1. 添加redis配置文件
    在这里插入图片描述
# 数据库所在服务器地址
redis.host=127.0.0.1
# 数据库端口号
redis.port=6379
# 连接超时时间
redis.timeout=30000
# 最大空闲连接数
redis.maxIdle=1
# 最大连接数
redis.maxTotal=5
# 是否使用连接池
redis.usePool=true
# 指明是否在从池中取出连接前进行检验,如果检验失败, 则从池中去除连接并尝试取出另一个.
redis.testOnBorrow=true

redis.blockWhenExhausted=true
  1. 容器中注入添加的配置文件
<!--加载多个外部配置文件-->
    <bean id="PropertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="1" />
        <property name="ignoreUnresolvablePlaceholders" value="true" />
        <property name="locations">
            <list>
                <!--mysql数据库配置文件-->
                <value>classpath:jdbcConfig.properties</value>
                <!--redis数据库配置文件-->
                <value>classpath:redisConfig.properties</value>
            </list>
        </property>
    </bean>

因为我配置了多个文件,需要用这种方式注入,或者通过下面这种方式注入

	<!-- 引入配置文件 -->
	<context:property-placeholder location="classpath:db.properties,classpath:redis.properties"/>

  • 可以像上述格式一样,每一个配置文件都写一个classpath,用“,”隔开

  • 也可以用“*.properties”

  • 如果配置文件分开写,即jdbc和redis中都加载各自的,可以使用ignoreUnresolvablePlaceholders进行设置,两个xml都要写!

  1. 注入相关对象到容器中
<!--redis数据库(与数据库不同,存储在内存中)-->
    <!--jedis资源池(类似于数据库连接池)-->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}"/>
        <property name="maxTotal" value="${redis.maxTotal}"/>
        <property name="blockWhenExhausted" value="${redis.blockWhenExhausted}"/>
        <property name="maxWaitMillis" value="${redis.timeout}"/>
        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
    </bean>
    <!--redis连接工厂对象-->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.host}"/>
        <property name="port" value="${redis.port}"/>
        <property name="poolConfig" ref="jedisPoolConfig"/>
        <property name="usePool" value="${redis.usePool}"/>
    </bean>
    <!--连接redis数据库模板对象-->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
        <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="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>
    </bean>

测试方法

    /**
     * 测试方法
     * */
    @Test
    public void testLixs(){

        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        final RedisTemplate<String, Object> redisTemplate = ac.getBean("redisTemplate",RedisTemplate.class);
        //添加一个 key
        ValueOperations<String, Object> value = redisTemplate.opsForValue();
        value.set("a", "hello word");
        //获取 这个 key 的值
        System.out.println(value.get("a"));
    }

结果:
在这里插入图片描述

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值