简体   繁体   English

Spring Boot似乎没有获得存储库

[英]Spring-boot just doesn't seem to get the repository

I can't use my CrudRepository in my Service class. 我不能在Service类中使用CrudRepository。 I can create the repository, but when I Autowire it to my Service class I get this error: 我可以创建存储库,但是当我将其自动连接到Service类时,会出现以下错误:

Parameter 0 of constructor in com.test.service.testService required a bean of type 'com.test.repository.TestRepository' that could not be found. com.test.service.testService中的构造函数的参数0需要找不到“ com.test.repository.TestRepository”类型的Bean。

Action: 行动:

Consider defining a bean of type 'com.test.repository.TestRepository' in your configuration. 考虑在配置中定义类型为“ com.test.repository.TestRepository”的bean。

This seems to be a big issue for many people. 对于许多人来说,这似乎是一个大问题。 I've tried all kinds of things like @ComponentScan, @EnableAutoConfiguration, @EnableJpaRepositories but none of that worked. 我已经尝试了诸如@ ComponentScan,@ EnableAutoConfiguration,@ EnableJpaRepositories之类的所有方法,但是这些都不起作用。

Main App: 主要应用程式:

@ComponentScan ({"com.test.repository", "com.test.controller","com.test.service","com.test.model"})
@EnableJpaRepositories
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class Application {
    public static void main(String[] args){
        SpringApplication.run(Application.class);
    }
}

Service: 服务:


    public testService(TestRepository testRepository) {
        this.testRepository= testRepository;
    }

Repository 资料库

package com.test.TestRepository;


import com.test.model.Item;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface TestRepository extends CrudRepository<Item, Long> {
}

POM.xml POM.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.1.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>2.1.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.8</version>
    </dependency>
</dependencies>

I expect the repository to be Autowired and fully functional. 我希望该存储库可以自动装配并且可以正常运行。

尝试这个:

@EnableJpaRepositories(basePackages = {"com.test"})

Try remove the @Repository annotation above your TestRepository interface. 尝试删除TestRepository界面上方的@Repository批注。

"Spring @Repository annotation is used to indicate that the class provides the mechanism for storage, ... " see: https://www.journaldev.com/21460/spring-repository-annotation “ Spring @Repository批注用于指示该类提供了存储机制,...”请参阅: https : //www.journaldev.com/21460/spring-repository-annotation

Suggestion: Modify your TestRepository interface to extend the JpaRepository. 建议:修改您的TestRepository接口以扩展JpaRepository。 It has more functionality. 它具有更多功能。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM