【问题标题】:SpringBoot 1.2.1 MongoRepositorySpringBoot 1.2.1 MongoRepository
【发布时间】:2017-02-14 18:15:25
【问题描述】:

我遇到了以下问题:

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

}

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

提前致谢!

编辑:

解决问题。我已经将@EnableMongoRepository 放在 AppInitializer.java 中:

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

}

【问题讨论】:

  • 我解决了问题:我把注解@EnableMongoRepository的地方改成了AppInitializer.java。
  • 您可以(并且应该)为您的问题添加答案,而不是编辑带有解决方案的问题。奖励:您也可以接受自己的答案!

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


【解决方案1】:

你需要在包 com.almundo.anac.core 中创建一个接口

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

【讨论】:

  • 我已经尝试过这种方式,但问题仍然存在。见以下代码:code package com.almundo.anac.core;导入 org.springframework.data.mongodb.repository.MongoRepository;导入 com.almundo.anac.domain.ANACDelay;公共接口 ANACDelayRepository 扩展 MongoRepository { } code
  • 您是否在 applicationContext.xml 中添加了 ?或者,如果您使用 java 配置,请在配置类中添加 @ImportResource("classpath:SpringConfig.xml")。如果项目目录结构不同,请确保更改路径。
  • 感谢您的帮助!
【解决方案2】:

您的域似乎缺少存储库接口。

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

并将此存储库自动连接到管理器中。

【讨论】:

  • 我已经尝试过这种方式,但问题仍然存在。见以下代码:code package com.almundo.anac.core;导入 org.springframework.data.mongodb.repository.MongoRepository;导入 com.almundo.anac.domain.ANACDelay;公共接口 ANACDelayRepository 扩展 MongoRepository { } code
  • 尝试删除 EnableMongoRepositories 注释。这是多余的,因为它已经在 xml 配置中提到。在我看来,其他一切都很好。确保在管理器中自动装配此存储库。
  • 感谢您的帮助!
猜你喜欢
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 2020-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-11
  • 2013-12-29
相关资源
最近更新 更多