Search code examples
spring-mvcspring-dataspring-data-mongodbmongodb-java

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [ ] found for dependency


I'm trying to implement a crud example for mongodb with annotation but I'm getting this error message:

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'levelServices': Unsatisfied dependency expressed through field 'levelRepository': No qualifying bean of type [com.thegarage.repositories.LevelRepository] found for dependency [com.thegarage.repositories.LevelRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.thegarage.repositories.LevelRepository] found for dependency [com.thegarage.repositories.LevelRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:569)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:776)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
at com.thegarage.services.App.main(App.java:24)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.thegarage.repositories.LevelRepository] found for dependency [com.thegarage.repositories.LevelRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1406)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1057)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1019)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566)
... 14 more

I tried to look to different example in stackoverflow but I wasn't able to get the solution.

here is my code below:

@Document(collection = "parking_spaces")
public class Location {

@Id
private String id;
private int locationNumber;
private boolean reserved;
.....
}

and the repository

@Repository
public interface LocationRepository extends MongoRepository<Location, String>{

Location findByLocationNumber (int locationNumber);

}

and the service

@Service
@Transactional
public class LevelServices {

@Autowired
private LevelRepository levelRepository;

@SuppressWarnings("unused")
private void addNewLevel(Level level) {
    levelRepository.save(level);
}
.....
}

and the main class

public class App {
/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    // For Annotation
    AbstractApplicationContext  context = new AnnotationConfigApplicationContext(AppConfig.class);
//      MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");
    LocationService locationService = context.getBean(LocationService.class);

    Location location = new Location("1",1,true);


    // save
    locationService.addNewLocations(location);

    System.out.println("1. location : " + location);

    context.close();

}

}

and to finish here is my config class

@Configuration
@ComponentScan(basePackages={"com.thest.repositories", "com.thest.services", "com.thest.config"})
public class MongoConfig{

@Autowired
private Mongo mongo;    
/*
 * Factory bean that creates the com.mongodb.Mongo instance
 */
@Bean
 public MongoClientFactoryBean mongo() {
      MongoClientFactoryBean factoryBean  = new MongoClientFactoryBean();
      factoryBean.setHost("localhost");
      return factoryBean ;
 }

@Bean
public MongoTemplate mongoTemplate() throws Exception{
    return new MongoTemplate(mongo, "thegaragedb");
}

}

did I miss Something?


Solution

  • you need to add @EnableMongoRepositories("package name") to your config file, where package name is the name of package where you used mongorepository.