2016-08-21 73 views
0

我試圖實現與註釋MongoDB的一個CRUD例子,但我收到此錯誤信息:org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有類型[]的排位豆發現依賴

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

}

我錯過了什麼?

+0

您需要'@EnableMongoRepositories(...)'在您的cofig類。請參閱http://docs.spring.io/spring-data/data-mongo/docs/1.9.2.RELEASE/reference/html/#repositories.create-instances.java-config –

回答

2

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

相關問題