简体   繁体   English

需要一个无法找到的 Repository 类型的 bean

[英]Required a bean of type Repository that could not be found

I am setting up a new Spring project for some fun learning/tutorial at home and I seem to run into a fairly common issue but I have tried all possible solutions I found on here but with no luck.我正在建立一个新的 Spring 项目,以便在家中进行一些有趣的学习/教程,我似乎遇到了一个相当普遍的问题,但我已经尝试了我在这里找到的所有可能的解决方案,但没有运气。 Basically what I have is as follows:基本上我所拥有的如下:

Controller class: Controller class:

@RestController
@RequestMapping(value = "/shop")
public class ShopController {

    @Autowired
    ShopService shopService;

    @GetMapping(value = "/{id}")
    public @ResponseBody Shop getTestData(@PathVariable String id) {
        return shopService.getShopBasedOnId(id);
    }

}

Service class:服务 class:

@Service
public class ShopService {

    @Autowired
    private ShopRepository shopRepository;

    public ShopService(ShopRepository shopRepository){
        this.shopRepository = shopRepository;
    }

    public Shop getShopBasedOnId(String id) {
        return shopRepository.findByShopId(id);
    }
}

Repository class:存储库 class:

@Repository
public interface ShopRepository extends PagingAndSortingRepository<Shop, String> {

        Shop findByShopId(String shopId);
}

Application class:应用 class:

@SpringBootApplication
@EnableJpaRepositories("com.example.reservations.repository")
public class ReservationsApplication {

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

}

Last but not least my pom.xml with the dependencies:最后但并非最不重要的是我的 pom.xml 与依赖项:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-couchbase</artifactId>
            <version>4.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.2.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>2.3.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.2.Final</version>
        </dependency>

So the error code I am getting is as follows:所以我得到的错误代码如下:

Description:

Parameter 0 of constructor in com.example.reservations.services.ShopService required a bean of type 'com.example.reservations.repository.ShopRepository' 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.example.reservations.repository.ShopRepository' in your configuration.

My structure of folders is:我的文件夹结构是:

main
 |_java
    |_com.example.reservations
         |_controllers
              |_ShopController.java
         |_repository
              |_ShopRepository.java
         |_services
              |_ShopService.java
         |_ReservationsApplication.java

The problem could be @Autowired in ShopService问题可能是 ShopService 中的@Autowired

@Service

public class ShopService {

@Autowired
private ShopRepository shopRepository;

public ShopService(ShopRepository shopRepository){
    this.shopRepository = shopRepository;
}

public Shop getShopBasedOnId(String id) {
    return shopRepository.findByShopId(id);
}
 }` 

should be @Service public class ShopService {应该是@Service public class ShopService {

private ShopRepository shopRepository;

public ShopService(ShopRepository shopRepository){
    this.shopRepository = shopRepository;
}

public Shop getShopBasedOnId(String id) {
    return shopRepository.findByShopId(id);
}
}

You can also add @Autowired just above the constructor but it is no longer required if there is only one constructor您也可以在构造函数上方添加@Autowired ,但如果只有一个构造函数,则不再需要

Turns out I had to have a config file for couchbase repository.原来我必须有一个 couchbase 存储库的配置文件。 What I did was follow the following link and now it all compiles fine!我所做的是按照以下链接进行操作,现在一切正常!

In my situation, I had spring boot with multiple modules.在我的情况下,我用多个模块启动了 spring。 So I had to the below to make it work.所以我不得不在下面让它工作。

@SpringBootApplication (scanBasePackages={"gov.usda.fsa.fcao.flp.ola"})
@EnableJdbcRepositories(basePackages={"gov.usda.fsa.fcao.flp.ola"})
public class TestOLAApplication {

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

    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NoSuchBeanDefinitionException 与响应式 mongo 存储库:需要找不到类型的 bean - NoSuchBeanDefinitionException with reactive mongo repository: required a bean of type that could not be found 在...中的字段存储库需要一个类型为...的bean,无法找到的NoteRepository - Field repository in …NoteController required a bean of type …NoteRepository that could not be found 服务中构造函数的参数 0 需要无法找到类型存储库的 bean - Parameter 0 of constructor in service required a bean of type repository that could not be found “服务”中构造函数的参数 0 需要找不到类型为“存储库”的 bean - Parameter 0 of constructor in 'Service' required a bean of type 'Repository' that could not be found *Service 中构造函数的参数 0 需要找不到类型为“*Repository”的 bean - Parameter 0 of constructor in *Service required a bean of type '*Repository' that could not be found 需要一个找不到类型的 bean - Required a bean of type that could not be found Controller.Controller 中的错误字段存储库需要找不到类型为“Controller.Repository”的 bean - Error Field Repository in Controller.Controller required a bean of type 'Controller.Repository' that could not be found 存储库字段需要一个无法找到的类型的 bean - repository field required a bean of type that cannot be found SpringBoot需要找不到类型为&#39;boolean&#39;的bean - SpringBoot required a bean of type 'boolean' that could not be found 需要一个无法通过Spring Boot找到的类型的bean - Required a bean of type that could not be found Spring Boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM