springboot 2.1启动,redis报错Failed to instantiat ... java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig

 SpringBoot 2.1.1集成redis,在启动时报错信息如下:

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'redisConnectionFactory' defined in class path resource 
[org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.class]: Bean instantiation via factory method failed; 
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory]: 
Factory method 'redisConnectionFactory' threw exception; 
nested exception is java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig

解决方案:

引入连接池依赖

<dependency>
    <groupId>org.apache.commonsgroupId>
    <artifactId>commons-pool2artifactId>
    <version>2.8.0version>
dependency

另外:

由于从springboot2.0开始,spring-boot-starter-data-redis默认使用 lettuce 代替Jedis,因此需要把配置文件中redis配置中的jedis改为lettuce,

springboot 2.1启动,redis报错Failed to instantiat ... java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig_第1张图片

如下

#rediss
spring.redis.cluster.nodes=
spring.redis.timeout=
spring.redis.lettuce.pool.max-wait=
spring.redis.lettuce.pool.max-active=
spring.redis.lettuce.pool.max-idle=
spring.redis.lettuce.pool.min-idle=

如果使用jedis,需修改pom文件

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-data-redisartifactId>
    <exclusions>
        <exclusion>
            <groupId>io.lettucegroupId>
            <artifactId>lettuce-coreartifactId>
        exclusion>
    exclusions>
dependency>
<dependency>
    <groupId>redis.clientsgroupId>
    <artifactId>jedisartifactId>
dependency>

 RedisConfig.java

@Bean
public RedisConnectionFactory redisCF() {
    JedisConnectionFactory rcf = new JedisConnectionFactory();
    rcf.setHostName("127.0.0.1");
    rcf.setPort(6379);
    return rcf;
}

@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
    RedisTemplate template = new RedisTemplate();
    template.setConnectionFactory(factory);
    return template;
}

 如果使用Lettuce ,则pom文件如下:

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-data-redisartifactId>
dependency>

RedisConfig.java

@Bean
public RedisConnectionFactory redisCF() {
// JedisConnectionFactory rcf = new JedisConnectionFactory();
    LettuceConnectionFactory rcf = new LettuceConnectionFactory();
    rcf.setHostName("127.0.0.1");
    rcf.setPort(6379);
//        rcf.setPassword();
    return rcf;
}

@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
    RedisTemplate template = new RedisTemplate();
    template.setConnectionFactory(factory);
    return template;
}

 

你可能感兴趣的:(springboot 2.1启动,redis报错Failed to instantiat ... java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig)