org.springframework.beans.factory.NoSuchBeanDefinitionException: 没有为依赖项找到 [ ] 类型的合格 bean

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

我正在尝试为 mongodb 实现带有注释的 crud 示例,但我收到此错误消息:

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.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

我试图查看 Whosebug 中的不同示例,但无法找到解决方案。

下面是我的代码:

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

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

和存储库

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

Location findByLocationNumber (int locationNumber);

}

和服务

@Service
@Transactional
public class LevelServices {

@Autowired
private LevelRepository levelRepository;

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

和主要 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();

}

}

最后是我的配置 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");
}

}

我错过了什么吗?

您需要将 @EnableMongoRepositories("package name") 添加到您的配置文件中,其中 package name 是您使用 mongorepository 的包的名称。