redis整合ssm
以下报错:
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name ‘poolConfig’ defined in class path resource [spring.xml]: Could not resolve placeholder ‘redis.maxIdle’ in string value “${redis.maxIdle}”; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder ‘redis.maxIdle’ in string value “${redis.maxIdle}”
是因为你配置文件里面有多个<context:property-placeholder...>
解决方式:使用通配符*放在一起
<context:property-placeholder location="classpath*:*.properties" />
以下报错:
Caused by: java.lang.NoClassDefFoundError: redis/clients/jedis/GeoUnit
我也不知道为啥。
解决方式:
<!--配置文件--> <context:property-placeholder location="classpath*:*.properties" ignore-unresolvable="true"/>
加上ignore-unresolvable="true"
redis整合成功
pom包版本信息:
<!-- jedis依赖 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.6.2.RELEASE</version>
</dependency>
redis-config.properties
redis.host=192.168.111.xxx
redis.port=6379
redis.pass=
redis.database=0
# 控制一个pool最多有多少个状态为idle(空闲的)的jedis实例
redis.maxIdle=300
# 当borrow引入一个jedis实例时,最大的等待时间毫秒,如果超过,则直接报错IedisConnectionException
redis.maxWait=3000
# jedis实例时,提前进入validate操作,如果为true,则为检测好的。
redis.testOnBorrow=true
spring.xml中添加redis的配置信息
<?xml version="1.0" encoding="utf-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config></context:annotation-config>
<!--配置文件-->
<context:property-placeholder location="classpath*:*.properties" ignore-unresolvable="true"/>
<!--相关配置-->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}"/>
<property name="maxWaitMillis" value="${redis.maxWait}"/>
<property name="testOnBorrow" value="${redis.testOnBorrow}"/>
</bean>
<!--连接工厂-->
<bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:poolConfig-ref="poolConfig"/>
<!--此类负责操作redis服务器的工具类-->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="JedisConnectionFactory"></property>
</bean>
</beans>
测试类
package com.redis.test;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations ={"classpath:spring.xml"})
public class Demo {
@Autowired
RedisTemplate redisTemplate;
@Test
public void test(){
System.out.println(redisTemplate);
}
}
测试一些API
package com.redis.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring.xml"})
public class Demo {
@Autowired
RedisTemplate redisTemplate;
@Test
public void test() {
System.out.println(redisTemplate);
}
@Test
public void testSetString() {
redisTemplate.boundValueOps("name").set("zhangsan");
}
@Test
public void testGetString() {
String val = (String) redisTemplate.boundValueOps("name").get();
System.out.println(val);
}
@Test
public void testDelString() {
redisTemplate.delete("name");
}
@Test
public void testSize() {
//返回15,也不知道为什么,因为这个API,添加的值,在命令行下查看是有一些头信息的。
System.out.println(redisTemplate.boundValueOps("name").size());
}
}