Search code examples
javaspringspring-bootspring-mongodb

how to solve No qualifying bean of type 'com.example.test.repository.ConfigRepository' available: expected at least 1 bean which qualifies autowire


Following is my directory structure

  1. com.example
  2. com.example.common
  3. com.example.test
  4. com.example.test.repository

my main spring boot class is as follow

package com.example.test;

@Import({ AutoConfig.class })
@SpringBootApplication
public class testApplication {
  public static void main(String[] args) {
    SpringApplication.run(testApplication.class, args);
  }
}

my repository clas

package com.example.test.repository.ConfigRepository;

 @Repository
 public interface ConfigRepository extends MongoRepository<Config, String>, QuerydslPredicateExecutor<Config> {

}

This is the error i am getting in debug mode

DEBUG o.s.c.a.ClassPathBeanDefinitionScanner - Ignored because not a concrete top-level class: file [/opt/<folder_name>/<folder_name>/target/classes/com/example/test/repository/ConfigRepository.class]

AutoConfig class used in @import is as below

package com.example.common;

@Configuration
@EnableFeignClients
@ComponentScan({ "com.example.common" })
public class AutoConfig {

Solution

  • Your ConfigRepository class in in com.example.test.repository this package.

    and while providing @ComponentScan, you are passing this path com.example.common.

    So instead of that you just tried with this com.example.test path as below.

    And also on your SpringBootApplication file or on your Config file you can provide EnableMongoRepositories and setting basePackages attributes.

    package com.example.test;
    
    @Import({ AutoConfig.class })
    @EnableMongoRepositories(basePackages = {"com.example.test.repository"})
    @SpringBootApplication
    public class testApplication {
      public static void main(String[] args) {
        SpringApplication.run(testApplication.class, args);
      }
    }
    
    @Configuration
    @EnableFeignClients
    @ComponentScan({ "com.example.test" })
    public class AutoConfig {
    

    More about @EnableMongoRepositories you will get an idea from here. This will help you.