spring、redis整合,实例化ConnectionFactory出错 10C

下面是我配置connectionFactory的代码

 @Bean
    public JedisConnectionFactory connectionFactory() throws IOException{
        Properties p = new Properties();
        // 加载redis配置文件
        p.load(CreateBeans.class.getResourceAsStream("/redis.properties"));
        // 配置JedisPool
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(Integer.parseInt(p.getProperty("redis.maxIdle")));
        poolConfig.setMaxTotal(Integer.parseInt(p.getProperty("redis.maxTotal")));
        poolConfig.setMaxWaitMillis(Integer.parseInt(p.getProperty("redis.maxWaitMillis")));
        poolConfig.setTestOnBorrow(Boolean.parseBoolean(p.getProperty("redis.testOnBorrow")));

        // 配置连接信息
        RedisStandaloneConfiguration standaloneConfig = new RedisStandaloneConfiguration();
        standaloneConfig.setHostName(p.getProperty("redis.host"));
        standaloneConfig.setPort(Integer.parseInt(p.getProperty("redis.port")));
        String pass = p.getProperty("redis.pass");
        if(pass != null && !pass.equals(""))
            standaloneConfig.setPassword(RedisPassword.of(pass));
        else
            standaloneConfig.setPassword(RedisPassword.none());

        DefaultJedisClientConfigurationBuilder builder = (DefaultJedisClientConfigurationBuilder) JedisClientConfiguration.builder();
        builder.usePooling();
        builder.poolConfig(poolConfig);
        builder.connectTimeout(Duration.ofMillis(Integer.parseInt(p.getProperty("redis.connTimeout"))));
        builder.readTimeout(Duration.ofMillis(Integer.parseInt(p.getProperty("redis.readTimeout"))));
        JedisClientConfiguration clientConfig = builder.build();

        return new JedisConnectionFactory(standaloneConfig, clientConfig);
    }

所引用的包
所引用的包
以上为引用的包,当然有些是配置redis不需要的。
下面是错误信息,

Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.springframework.data.redis.connection.jedis.JedisConnectionFactory
    at com.dimbbs.beans.CreateBeans.connectionFactory(CreateBeans.java:43)
    at com.dimbbs.beans.CreateBeans$$EnhancerBySpringCGLIB$$6ae8678c.CGLIB$connectionFactory$0(<generated>)
    at com.dimbbs.beans.CreateBeans$$EnhancerBySpringCGLIB$$6ae8678c$$FastClassBySpringCGLIB$$d2c7309d.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361)
    at com.dimbbs.beans.CreateBeans$$EnhancerBySpringCGLIB$$6ae8678c.connectionFactory(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    ... 48 more

是不是我缺少什么包?

5个回答

缺少jar包吧,看看这个类需要的包

jedis客户端版本过高,

将jedis的版本号换低版本就好了

zqs164
zqs164 谢谢回复,是我少导了包
2 年多之前 回复

spring-data-redis 目前只支持到 3.x, 4.x 不支持, 如果使用spring-data-redis, 把redis降到3.x试试

zqs164
zqs164 谢谢回复,是我少导了包,将版本我这里没成功
2 年多之前 回复

我少导入了commons-beanutils-core-1.8.3.jar、spring-data-commons-2.0.8.RELEASE.jar和spring-data-keyvalue-2.0.8.RELEASE.jar三个包,导入了就ok了

和版本有关,查看一下jar的版本是否匹配

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问