最近在开发项目的时候遇到如下的情况:
- 前台list的属性由于数据量较大,而且在后台是递归获取的,所以前端展示的页面就比较慢,所以想集成一下redis,加快数据的查询。
Spring集成redis的主流方式大致有以下两种(以单机为例)
(1)依赖于spring-data-redis和jedis(基于java的redis)
- 我的配置过程如下:
因为是二次开发,所以是基于原有的SSM框架(Spring+Spring MVC+Mybatis)
已有的配置我就不再次赘述了
1)首先新建数据库配置文件redis.properties,类似有Mysql的配置
redis.host = localhost
redis.port = 6379
redis.password = redis
redis.maxIdle=300
redis.maxWait=2000
redis.testOnBorrow = true
redis.timeout=5000
2)在spring-jdbc.xml中引入上述配置文件
<!-- 引入配置文件 -->
<context:property-placeholder location="classpath:db.properties,classpath:redis.properties"/>
这里要明确说明一下,因为spring容器在加载的时候只会加载一个实例,所以如果想加载两个以上,有以下几种方法:
- 可以像上述格式一样,每一个配置文件都写一个classpath,用“,”隔开
- 也可以用“*.properties”
- 如果配置文件分开写,即jdbc和redis中都加载各自的,可以使用ignoreUnresolvablePlaceholders进行设置,两个xml都要写!
3)新建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" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://