有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

一位助理经理。爪哇:

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

一个服务控制器。爪哇:

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

}

只是为了解释一下,我正在构建一个Rest API,它将调用我的存储库从数据库中读取数据

提前谢谢

编辑:

解决问题。我已经在AppInitializer中添加了@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);
    }

}

共 (2) 个答案

  1. # 1 楼答案

    看起来您缺少域的存储库接口

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

    并将此存储库自动连接到manager

  2. # 2 楼答案

    您需要在包com中创建一个接口。阿尔蒙多。阿纳克。核心

    package com.almundo.anac.core;
    
    public interface ANACDelayRepository extends MongoRepository<ANACDelay, String> {
    
    }
    

    那么ANACDetailManager应该是这样的:

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