Failed to instantiate [org.springframework.data.redis.connection.jedis.JedisConnectionFactory]

使用spring-data-redis报错

测试代码

@Test
public void SpringRedisDemoTest() {
    RedisTemplate<String, Object> template =
            ctx.getBean("redisTemplate", RedisTemplate.class);
    // opsForValue() 返回字符串类型操作
    template.opsForValue().set("demo", "Hello World!");
    String str = (String) template.opsForValue().get("demo");
    System.out.println(str);
}

报错详情

[main] WARN org.springframework.context.support.ClassPathXmlApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jedisConnFactory' defined in class path resource [spring-redis.xml]: 
Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.
connection.jedis.JedisConnectionFactory]: No default constructor found; nested exception is java.lang.NoClassDefFoundError: redis/clients/jedis/util/Pool

用到的依赖

<dependency>
	<groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
	<version>3.3.0</version>
</dependency>
<dependency>
	<groupId>org.springframework.data</groupId>
	<artifactId>spring-data-redis</artifactId>
	<version>2.2.1.RELEASE</version>
 </dependency>

xml配置文件

<bean id="jedisConnFactory"
      class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    <property name="usePool" value="true"/>
    <property name="hostName" value="127.0.0.1"/>
    <property name="password" value="123456"/>
</bean>

<bean id="redisTemplate"
      class="org.springframework.data.redis.core.RedisTemplate">
    <property name="connectionFactory" ref="jedisConnFactory"/>
</bean>

一开始以为是xml文件配置出错,仔细查找后并没有发现错误,经过网络查找原因,最终发现是spring-data-redisjedis版本不兼容。

修改spring-data-redisjedis版本

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>2.0.7.RELEASE</version>
</dependency>

测试结果

[main] INFO org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6833ce2c: startup date [Thu Sep 22 09:47:08 CST 2022]; root of context hierarchy
[main] INFO org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [spring-redis.xml]
Hello World!
[main] INFO org.springframework.context.support.ClassPathXmlApplicationContext - Closing org.springframework.context.support.ClassPathXmlApplicationContext@6833ce2c: startup date [Thu Sep 22 09:47:08 CST 2022]; root of context hierarchy

可以看到现在测试程序执行成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值