我无法在 Service 类中使用 CrudRepository。我可以创建存储库,但是当我将其自动连接到我的服务类时,我收到此错误:
com.test.service.testService 中的构造函数的参数 0 需要一个类型为“com.test.repository.TestRepository”的 bean,但无法找到。
行动:
考虑在配置中定义“com.test.repository.TestRepository”类型的 bean。
这对于很多人来说似乎是一个大问题。我尝试过各种方法,例如@ComponentScan、@EnableAutoConfiguration、@EnableJpaRepositories,但都不起作用。
主应用程序:
@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);
}
}
服务:
public testService(TestRepository testRepository) {
this.testRepository= testRepository;
}
存储库
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
<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>
我希望存储库能够自动连接并且功能齐全。
最佳答案
试试这个:
@EnableJpaRepositories(basePackages = {"com.test"})
关于java - Spring-boot 似乎没有获取存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56760931/