Я пытаюсь реализовать пример Crud для MongoDB с аннотацией, но я получаю сообщение об ошибке: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");
}
}
сделал я что-то пропустил?
Вам нужно '@EnableMongoRepositories (...)' в вашем классе cofig. См. Http://docs.spring.io/spring-data/data-mongo/docs/1.9.2.RELEASE/reference/html/#repositories.create-instances.java-config –