其他模块中的 Spring Boot MongoDB 存储库不是 @Autowired

Posted

技术标签:

【中文标题】其他模块中的 Spring Boot MongoDB 存储库不是 @Autowired【英文标题】:Spring Boot MongoDB Repositories in other Module Are not @Autowired 【发布时间】:2015-02-22 09:40:32 【问题描述】:

我试图通过创建单独的 Eclipse 模块将我的主/Web 应用程序与数据层分开。运行主应用项目时spring boot报错:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'application': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: it.vertyze.platform.data.models.LocationRepository it.vertyze.platform.web.main.Application.locationRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [it.vertyze.platform.data.models.LocationRepository] 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:301)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706)
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:109)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
at it.vertyze.platform.web.main.Application.main(Application.java:25)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: it.vertyze.platform.data.models.LocationRepository it.vertyze.platform.web.main.Application.locationRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [it.vertyze.platform.data.models.LocationRepository] 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:522)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:298)
... 16 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [it.vertyze.platform.data.models.LocationRepository] 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:1118)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:967)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:862)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:494)
... 18 common frames omitted

我的主要应用在这里:

package it.vertyze.platform.web.main;

import it.vertyze.platform.data.models.LocationRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;

 @Configuration
 @ComponentScan
 @EnableAutoConfiguration
 @EnableMongoRepositories
 @Import(RepositoryRestMvcConfiguration.class)
 public class Application 

    @Autowired
    LocationRepository locationRepository;

    public static void main(String[] args) 
        SpringApplication.run(Application.class, args);
    


而我的 LocationRepository 位于不同模块中的不同包中:

package it.vertyze.platform.data.models;

import java.util.List;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.stereotype.Component;

@Component
@RepositoryRestResource(collectionResourceRel = "location", path = "location")
public interface LocationRepository extends MongoRepository<Location, String> 

    List<Location> findByLocationIdentifier(@Param("locationIdentifier") String locationIdentifier);


为了完整起见,这里是我要存储的实体的代码:

package it.vertyze.platform.data.models;

import org.springframework.data.annotation.Id;

public class Location
    @Id 
    private String id;

    private String locationName;
    private String locationIdentifier;
    public String getLocationName() 
        return locationName;
    
    public void setLocationName(String locationName) 
        this.locationName = locationName;
    
    public String getLocationIdentifier() 
        return locationIdentifier;
    
    public void setLocationIdentifier(String locationIdentifier) 
        this.locationIdentifier = locationIdentifier;
    

有什么帮助吗?我认为这将是一件很常见的事情。然而,也许我对 Spring 太陌生了。

【问题讨论】:

您没有扫描包含存储库的包。没有额外配置的@ComponentScan 以当前包为起点。我建议将应用程序类移动到it.vertyze.platform 包或将其放入组件扫描@ComponentScan("it.vertyze.platform")。您应该能够删除 @EnableMongoRepositories@Import(RepositoryRestMvcConfiguration.class),因为 Spring Boot 会处理这些问题。 谢谢!在提出问题后,我确实将包放入注释中......愚蠢的我没有意识到那个包实际上必须存在...... 【参考方案1】:

M. Deinums 的评论做到了。很抱歉打扰 ***。原来我太笨了,不能在我的后备箱里放一个带有我的 main 方法的父包

【讨论】:

以上是关于其他模块中的 Spring Boot MongoDB 存储库不是 @Autowired的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot源码中模块详解

Spring Boot 的 10 个核心模块

多模块 Spring Boot 项目中的 Gradle 依赖插件

如何确保 Spring Boot 额外的 Jackson 模块具有相同的版本?

spring boot通过@Bean注解定义一个Controller

多模块 Gradle 项目 - 从 Spring-Boot 1.5 迁移到 2.1