Spring-boot: Failed to initialize RedisConnectionFactory bean

Created on 12 Apr 2018  路  12Comments  路  Source: spring-projects/spring-boot

Hello. I was using version 1.5.12.RELEASE, but I have recently upgraded to 2.0.1.RELEASE and the application failed to start. The error occurred during the initialization of the RedisConnectionFactory bean.

It seems that some dependencies are missing. This is not happening in version 1.5.12.RELEASE.

I would appreciate any feedback.

Code snippet:

@Bean
public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();

    template.setConnectionFactory(connectionFactory);

    return template;
}

Exception log:

...
2018-04-12 07:20:57.386  WARN 17379 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisTemplate' defined in class path resource [com/my_app/config/RedisConfig.class]: Unsatisfied dependency expressed through method 'redisTemplate' parameter 0; nested exception is 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
...
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisTemplate' defined in class path resource [com/my_app/config/RedisConfig.class]: Unsatisfied dependency expressed through method 'redisTemplate' parameter 0; nested exception is ...
...
Caused by: 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 ...
...
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory]: Factory method 'redisConnectionFactory' threw exception; nested exception is ...
...
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/pool2/impl/GenericObjectPoolConfig
        at org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration$LettucePoolingClientConfigurationBuilder.<init>(LettucePoolingClientConfiguration.java:84)
        at org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration.builder(LettucePoolingClientConfiguration.java:48)
        at org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration$PoolBuilderFactory.createBuilder(LettuceConnectionConfiguration.java:153)
        at org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration.createBuilder(LettuceConnectionConfiguration.java:110)
        at org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration.getLettuceClientConfiguration(LettuceConnectionConfiguration.java:96)
        at org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration.redisConnectionFactory(LettuceConnectionConfiguration.java:77)
        at org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration$$EnhancerBySpringCGLIB$$2763558.CGLIB$redisConnectionFactory$0(<generated>)
        at org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration$$EnhancerBySpringCGLIB$$2763558$$FastClassBySpringCGLIB$$43049682.invoke(<generated>)
        at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
        at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361)
        at org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration$$EnhancerBySpringCGLIB$$2763558.redisConnectionFactory(<generated>)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
        ... 41 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.apache.commons.pool2.impl.GenericObjectPoolConfig
...
invalid

Most helpful comment

Thanks for the sample @jbpark-sk! Please review the release notes and the migration guide when upgrading. You'll find this section in particular quite useful.

All 12 comments

@jbpark-sk it is impossible to say without more details. Lettuce requires commons-pool2 if connection pooling is enabled. Can you share a sample that demonstrates the issue? (that code snippet is irrelevant to the issue).

@snicoll Thank you for your quick reply. Do I need to explicitly add commons-pool2 dependency to pom.xml? I thought it would be added automatically if only spring-boot-starter-data-redis was specified.

Hello. I was using version 1.5.12.RELEASE, but I have recently upgraded to 2.0.1.RELEASE and the application failed to start

I'd like to understand what your upgrade experience is. Can you please answer my request?

Thanks for the sample @jbpark-sk! Please review the release notes and the migration guide when upgrading. You'll find this section in particular quite useful.

@snicoll , Hi i have the same your issues when i upgraded spring boot to v2.0.3 , do you solve it yet?

@ryrun168 There was nothing to solve in Spring Boot itself. If you do have the same issue, then the section of the migration guide that was already linked to above will explain what you need to do.

If you have any further questions, please ask on Stack Overflow or Gitter. As mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.

@wilkinsona thanks very much

You can find spring data maven version compatible with jedis maven version from bellow link: https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis/1.5.9.RELEASE

Hi. I have migrated from Spring Boot 1.5.x to Spring Boot 2.1.5 and switched from JedisConnectionFactory to LettuceConnectionFactory. Issue I am facing now is:

java.lang.ClassNotFoundException: io.lettuce.core.KeyValue

I fixed it adding lettuce-core dependency. But still have question - shouldn't lettuce-core be already present in spring-session-data-redis? For me it looks like it should, judging from migration notes:

Lettuce is now used instead of Jedis as the Redis driver when you use spring-boot-starter-data-redis. If you are using higher level Spring Data constructs you should find that the change is transparent.

@OlegKuts It's not provided by spring-session-data-redis but it is in spring-boot-starter-data-redis. The release notes assumed that the starter is being used.

Was this page helpful?
0 / 5 - 0 ratings