org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为[]的限定bean用于依赖

时间:2016-08-21 02:05:38

标签: spring-mvc spring-data spring-data-mongodb mongodb-java

我正在尝试使用注释为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$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

我试图在stackoverflow中查看不同的示例,但我无法获得解决方案。

这是我的代码:

@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);
}
.....
}

和主要课程

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();

}

}

并在这里完成我的配置类

@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");
}

}

我想念Something吗?

1 个答案:

答案 0 :(得分:2)

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