Spring通过xml集成Redis数据库
前台list的属性由于数据量较大,而且在后台是递归获取的,所以前端展示的页面就比较慢,所以想集成一下redis,加快数据的查询。
Spring集成Redis的主流方式大致有以下两种(以单机为例)
- 基于xml集成Redis
- 基于注解实现Redis
今天我们讲讲利用xml集成Redis数据库
- 导入相关依赖
<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>
- 添加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
- 容器中注入添加的配置文件
<!--加载多个外部配置文件-->
<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都要写!
- 注入相关对象到容器中
<!--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"));
}
结果:

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





