Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data Redis NoSuchBeanDefinitionException: No qualifying bean of type

When I try to inject repository that implements CrudRepository from Spring Data Redis, I get NoSuchBeanDefinitionException.

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [bluh.bluh.repository.XxxRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

However configuration is there, it's annotated with @EnableRedisRepositories("bluh.bluh.repository")

@Configuration
@EnableRedisRepositories
public class ApplicationConfig {

    @Bean
    RedisConnectionFactory connectionFactory() {
        return new JedisConnectionFactory();
    }

    @Bean
    RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory connectionFactory) {

        RedisTemplate<byte[], byte[]> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);

        return template;
    }

}

Repository interface looks like:

import org.springframework.data.repository.CrudRepository;

public interface XxxRepository extends CrudRepository<String, String> { }

I've been through http://docs.spring.io/spring-data/redis/docs/current/reference/html/ , there's nothing new for me. I wonder what did I miss and I'll appreciate any inputs.

I use Spring Data Redis 1.7.2.RELEASE, Spring Boot 1.3.6.RELEASE

like image 970
max_dev Avatar asked Jul 14 '16 19:07

max_dev


People also ask

What is nosuchbeandefinitionexception in Spring Boot?

When we encounter the org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type xxxx available , we should consider whether there is a spring bean named xxx exists in the runtime. What is a Spring bean? (quote from by baeldung)

Why is there no qualifying Bean of type in Spring Boot?

No qualifying bean of type in Spring or Spring Boot. Reason 1: You forgot to declare the bean itself. Reason 2: You have not added package name to @ComponentScan. In this post, we will see about an exception: No qualifying bean of type.

Why is beanb not defined in the current Spring context?

If beans are not automatically scanned but instead defined manually, then BeanB is simply not defined in the current Spring Context. 3. Cause: Field […] in […] Required a Bean of Type […] That Could Not Be Found In a Spring Boot application for the above scenario, we get a different message.

Why is my bean not qualifying for dependency?

2. Cause: No Qualifying Bean of Type […] Found for Dependency The most common cause of this exception is simply trying to inject a bean that isn't defined. For example, BeanB is wiring in a collaborator, BeanA:


3 Answers

I was having this same issue, and realized it was a version problem.

I was using spring-boot v1.3.8, and was pulling in spring-data-redis v1.7.5 as a dependency. I was getting the error posted in the question above when trying to autowire a spring-data-redis repository with these versions.

I tried upgrading to spring-boot v.1.4.2. This version of spring-boot comes with a starter called "spring-boot-starter-data-redis" that pulls down spring-data-redis v1.7.5 and jedis v2.8.2. I followed the same configuration as provided in the docs, and I finally got it to work!

I'm guessing there are some compatibility issues with spring-boot v1.3.8 and spring-data-redis v1.7.x. This was somewhat confirmed, by the fact that spring-boot 1.3.8 came with a starter called spring-boot-starter-redis that pulled down v1.6.5 of spring-data-redis. Since the @EnableRedisRepositories annotation was only included in 1.7.+, it looks like an upgrade is required to get the repositories functionality to work properly.

tl;dr try upgrading spring boot to version.latest, and pull in spring-data-redis from spring-boot-starter-data-redis.

like image 104
Brian Ambielli Avatar answered Sep 20 '22 23:09

Brian Ambielli


Bit late but for anyone else having this issue:

I just sat pulling my hair out with this. Downloaded the GIT examples and noticed the entity was annotated with @RedisHash("hash_name_here"):

@RedisHash("MyDataThingies")
public class MyDataThingy{
    @Id
    public String id
}

Now it has connection issues but I know why :)

like image 27
RabidDog5150 Avatar answered Sep 17 '22 23:09

RabidDog5150


I had a similar problem. In my case the versions were not the problem but the annotation needed an explicit basePackages like below:

@EnableRedisRepositories(basePackages = "my.base.pkg")

  • Using springboot - 1.5.11 and spring-data-redis-1.8.11
like image 20
Developer Avatar answered Sep 18 '22 23:09

Developer