2017-02-14 4 views
0

ich das Problem bin unten:SpringBoot 1.2.1 MongoRepository

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ANACDetailManager': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public org.springframework.data.mongodb.repository.MongoRepository com.almundo.anac.core.ANACDetailManager._repository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.data.mongodb.repository.MongoRepository] found for dependency: 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.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1202) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:762) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:321) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:961) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:950) 
    at com.almundo.anac.service.AppInitializer.main(AppInitializer.java:12) 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public org.springframework.data.mongodb.repository.MongoRepository com.almundo.anac.core.ANACDetailManager._repository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.data.mongodb.repository.MongoRepository] found for dependency: 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:561) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) 
    ... 16 common frames omitted 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.data.mongodb.repository.MongoRepository] found for dependency: 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.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1308) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533) 

SpringConfig.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:mongo="http://www.springframework.org/schema/data/mongo" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/data/mongo 
    http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd"> 

    <mongo:mongo id="mongo" /> 

    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> 
    <constructor-arg ref="mongo" /> 
    <constructor-arg value="databaseName" /> 
    </bean> 

    <mongo:repositories base-package="com.almundo.anac.core" mongo-template-ref="mongoTemplate" /> 

</beans> 

AnacDetailManager.java:

package com.almundo.anac.core; 

import com.almundo.anac.domain.ANACDelay; 
import com.almundo.anac.core.ANACDelayRepository; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.data.mongodb.repository.MongoRepository; 
import org.springframework.stereotype.Service; 

@Service 
public class ANACDetailManager { 

    @Autowired 
    public MongoRepository<ANACDelay, String> _repository; 

    public ANACDelay FindDelay(String flightNumber, String airCia, String origin, String destination) { 
     String key = airCia + flightNumber + origin + destination; 
     ANACDelay anacDelay = _repository.findOne(key); 

     return anacDelay; 
    } 
} 

AnacServiceController.java:

package com.almundo.anac.service.controller; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; 
import org.springframework.http.HttpStatus; 
import org.springframework.http.ResponseEntity; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.ResponseBody; 

import com.almundo.anac.core.ANACDetailManager; 
import com.almundo.anac.domain.ANACDelay; 

@Controller 
//@RequestMapping("anacservice") 
//@ComponentScan("com.almundo.anac.core") 
@EnableMongoRepositories("com.almundo.anac.core") 
public class AnacServiceController { 

    @Autowired 
    final ANACDetailManager _manager; 

    @Autowired 
    public AnacServiceController(ANACDetailManager manager) { 
     this._manager = manager; 
    } 

    @ResponseBody 
    @RequestMapping(value = "{flightNumber}/{airCia}/{origin}/{destination}", method = RequestMethod.GET) 
    public ResponseEntity<ANACDelay> getAnacDelay(@PathVariable String flightNumber, @PathVariable String airCia, 
      @PathVariable String origin, @PathVariable String destination) { 

     ANACDelay anacDelay = _manager.FindDelay(flightNumber, airCia, origin, destination); 

     return new ResponseEntity<ANACDelay>(anacDelay, HttpStatus.OK); 
    } 

} 

Nur um zu erklären, ich baue eine Rest-API, die mein Repository aufrufen wird, um Daten aus der Datenbank zu lesen.

Vielen Dank im Voraus!

EDIT:

das Problem lösen. Ich habe in der AppInitializer.java setzen @EnableMongoRepository:

package com.almundo.anac.service; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; 

@SpringBootApplication 
@ComponentScan({"com.almundo.anac.core", "com.almundo.anac.service"}) 
@EnableMongoRepositories("com.almundo.anac.core") 
public class AppInitializer{ 

    public static void main(String[] args) throws Exception { 
     SpringApplication.run(AppInitializer.class, args); 
    } 

} 
+0

ich das Problem gelöst: änderte ich den Ort der Anmerkung @EnableMongoRepository zum AppInitializer.java. – arturcosta88

Antwort

0

Sieht aus wie Sie die Repository-Schnittstelle für Ihre Domain sind vermisst.

public interface YourRepository extends MongoRepository<ANACDelay, String> {} 

und dieses Repository in den Manager autowired.

+0

Ich habe diese Art versucht, aber das Problem passiert immer noch. Siehe unten stehenden Code: 'code' Paket com.almundo.anac.core; import org.springframework.data.mongodb.repository.MongoRepository; importieren com.almundo.anac.domain.ANACDelay; öffentliche Schnittstelle ANACDelayRepository erweitert MongoRepository { \t } 'code' – arturcosta88

+0

Versuchen Sie, die EnableMongoRepositories Anmerkung zu entfernen. Es ist überflüssig, da es bereits in der XML-Konfiguration erwähnt wird. Alles andere sieht gut aus für mich. Stellen Sie sicher, dass dieses Repository im Manager automatisch eingerichtet wird. – Veeram

+0

Vielen Dank für Ihre Hilfe! – arturcosta88

1

Sie benötigen eine Schnittstelle innerhalb des Pakets com.almundo.anac.core

package com.almundo.anac.core; 

public interface ANACDelayRepository extends MongoRepository<ANACDelay, String> { 

} 

Dann sollten ANACDetailManager so sein zu erstellen:

@Service 
public class ANACDetailManager { 

    @Autowired 
    public ANACDelayRepository _repository; 

    public ANACDelay FindDelay(String flightNumber, String airCia, String origin, String destination) { 
     String key = airCia + flightNumber + origin + destination; 
     ANACDelay anacDelay = _repository.findOne(key); 

     return anacDelay; 
    } 
} 
+0

Ich habe diese Art und Weise versucht, aber das Problem passiert immer noch. Siehe unten stehenden Code: 'code' Paket com.almundo.anac.core; import org.springframework.data.mongodb.repository.MongoRepository; importieren com.almundo.anac.domain.ANACDelay; öffentliche Schnittstelle ANACDelayRepository erweitert MongoRepository { \t } 'code' – arturcosta88

+0

Haben Sie in Ihrem applicationContext.xml hinzufügen? Oder wenn Sie Java-Konfiguration verwenden, fügen Sie @ImportResource hinzu ("classpath: SpringConfig.xml ") in Ihrer Konfigurationsklasse. Achten Sie darauf, den Pfad zu ändern, wenn er sich in Ihrer Projektverzeichnisstruktur unterscheidet. – mhshimul

+0

Vielen Dank für Ihre Hilfe! – arturcosta88