java - Spring-boot 似乎没有获取存储库
问题描述
我不能在我的服务类中使用我的 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"})
推荐阅读
- keras - Keras 的密集层上的形状不兼容
- python - 我该如何解决这个错误 TypeError: 'StringVar' 类型的对象没有 len()
- karate - 空手道 V1.0.1 * 配置 charset = null 不起作用
- jestjs - 在单元测试中调度操作时未选择 Ngrx 效果
- javascript - SyntaxError:无法在模块外使用 import 语句,如何使用 react-native-svg 为 Web 运行测试单元?
- groovy - 为什么我在 Jenkins 管道 groovy 中看到这种行为
- mongodb - MongoDB 聚合用例
- amazon-emr - 当我使用 zeppelin 时,Amazon EMR 集群可能停止响应
- php - 为什么我的随机函数总是返回相同的数字?
- r - 安装 rstan/brms 后的浮点异常