当前位置: 首页 > 知识库问答 >
问题:

Spring启动 MVC - 无法自动连接服务类中的存储库

孟洋
2023-03-14

我已经用@Autowired注释为相应的存储库定义了服务类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StoreService {

    @Autowired
    private StoreRepository repository;

存储库接口定义为从JpaReepository扩展

import org.springframework.data.jpa.repository.JpaRepository;

public interface StoreRepository extends JpaRepository<Store, String> {
}

应用程序自动编译服务类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@RestController
public class StoreController {

    @Autowired
    private StoreService service;

运行时,我得到以下错误

***************************
APPLICATION FAILED TO START
***************************

Description:

Field repository in com.mypackage.service.StoreService required a bean of type 'com.mypackage.respository.StoreRepository' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.mypackage.respository.StoreRepository' in your configuration.

共有2个答案

上官恩
2023-03-14

只需将@Repository注释添加到StoreRepository中,它就会正常工作。

以下是Spring识别的最常用的注释,可以用于自动连接,而无需显式地将其声明为bean:

段干安和
2023-03-14

我认为您应该用@repository注释您的存储库,然后它将由Spring Framework自动启用。

@Repository
public interface StoreRepository extends JpaRepository<Store, String> {
}
 类似资料:
  • 我正在尝试创建一个Spring Boot测试类,它应该创建Spring上下文,并自动连接服务类以供我测试。 这就是我得到的错误: 原因:org。springframework。豆。工厂NoSuchBeanDefinitionException:没有“com”类型的合格bean。目瞪口呆。戈布斯。基础服务FileImportService'可用:至少需要1个符合autowire候选资格的bean。依

  • 我正在为我的应用程序使用模拟存储库 以下是服务的外观片段: 以下是存储库代码: 当我使用执行main()时,它工作正常。 但是,当我想运行测试类: 它会因以下stacktrace而失败: 原因:org。springframework。豆。工厂NoSuchBean定义异常:没有类型为“edu”的合格bean。勒勒亚克。存储库。WeatherStationRepositoryMock’可用:至少需要1

  • 我在Spring Boot上还是新手,我在mongoDB数据库中使用Spring-Boot添加了一个名为文章的文档,我想在该文章中添加注释。但是Spring-boot不能在我的应用程序中自动连接我的存储库。 下面是我的Repository类,它实现了ArticleRepositoryCustom接口,该接口包含一个OuterComment方法。 文章库 这是我的Springboot应用程序课程 当

  • 我有一个到oracle DB的jdbc连接; 谢谢!

  • 我试图从spring主页模拟RESTful Web服务”https://spring.io/guides/gs/rest-service/“。我能够获得给定示例的预期输出,因此我做了一些增强,即尝试从oracle数据库获取一些计数,并将其显示为响应的一部分,但没有成功。 Maven编译工作正常,但是当我运行Spring引导时,我得到了下面的错误,我不知道是什么原因。我初来乍到,有人能帮帮我吗? 我

  • 问题内容: 如果Service类使用Validated注释进行注释,则同一类无法自动装配自身。 这是在Spring Context尝试加载时引发的异常: 同样,当您有很多依赖于类的自身时,就会发生这种情况(当某个服务使用使用第一个服务的其他服务时)。我想知道@Validated注解,但是我总是在bean上遇到同样的错误。 有人知道我该怎么解决吗? 问题答案: 在这种情况下,注释与错误的自动装配无关